Skip to content

Commit

Permalink
Don't call FeatureUrl.parse directly if it could throw (#2912)
Browse files Browse the repository at this point in the history
  • Loading branch information
clenfest authored Jan 19, 2024
1 parent 20c07c3 commit 56a7816
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 7 additions & 5 deletions composition-js/src/merging/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2612,12 +2612,13 @@ class Merger {
const map = new Map<string, FeatureUrl>();
for (const linkDirective of schema.schemaDefinition.appliedDirectivesOf<LinkDirectiveArgs>('link')) {
const { url, import: imports } = linkDirective.arguments();
if (imports) {
const parsedUrl = FeatureUrl.maybeParse(url);
if (parsedUrl && imports) {
for (const i of imports) {
if (typeof i === 'string') {
map.set(i, FeatureUrl.parse(url));
map.set(i, parsedUrl);
} else {
map.set(i.as ?? i.name, FeatureUrl.parse(url));
map.set(i.as ?? i.name, parsedUrl);
}
}
}
Expand Down Expand Up @@ -2654,9 +2655,10 @@ class Merger {

if (directive.name === 'link') {
const { url } = directive.arguments();
if (typeof url === 'string') {
const parsedUrl = FeatureUrl.maybeParse(url);
if (typeof url === 'string' && parsedUrl) {
shouldIncludeAsJoinDirective =
this.shouldUseJoinDirectiveForURL(FeatureUrl.parse(url));
this.shouldUseJoinDirectiveForURL(parsedUrl);
}
} else {
// To be consistent with other code accessing
Expand Down
9 changes: 8 additions & 1 deletion internals-js/src/specs/coreSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,14 @@ export class FeatureUrl {
public readonly element?: string,
) { }

/// Parse a spec URL or throw
public static maybeParse(input: string, node?: ASTNode): FeatureUrl | undefined {
try {
return FeatureUrl.parse(input, node);
} catch (err) {
return undefined;
}
}
/// Parse a spec URL or throw
public static parse(input: string, node?: ASTNode): FeatureUrl {
const url = new URL(input)
if (!url.pathname || url.pathname === '/') {
Expand Down

0 comments on commit 56a7816

Please sign in to comment.