Skip to content

Commit cc0b427

Browse files
committed
More robust handling of invalid script values
1 parent 98d2fd8 commit cc0b427

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

extensions/npm/src/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function runSelectedScript() {
2626
if (script) {
2727
runScript(script, document);
2828
} else {
29-
let message = localize('noScriptFound', 'Could not find an npm script at the selection.');
29+
let message = localize('noScriptFound', 'Could not find a valid npm script at the selection.');
3030
vscode.window.showErrorMessage(message);
3131
}
3232
}

extensions/npm/src/tasks.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ async function findAllScripts(buffer: string): Promise<StringMap> {
361361
},
362362
onLiteralValue(value: any, _offset: number, _length: number) {
363363
if (script) {
364-
scripts[script] = value;
364+
if (typeof value === 'string') {
365+
scripts[script] = value;
366+
}
365367
script = undefined;
366368
}
367369
},
@@ -419,6 +421,7 @@ export function findAllScriptRanges(buffer: string): Map<string, [number, number
419421

420422
export function findScriptAtPosition(buffer: string, offset: number): string | undefined {
421423
let script: string | undefined = undefined;
424+
let foundScript: string | undefined = undefined;
422425
let inScripts = false;
423426
let scriptStart: number | undefined;
424427
let visitor: JSONVisitor = {
@@ -432,9 +435,10 @@ export function findScriptAtPosition(buffer: string, offset: number): string | u
432435
},
433436
onLiteralValue(value: any, nodeOffset: number, nodeLength: number) {
434437
if (inScripts && scriptStart) {
435-
if (offset >= scriptStart && offset < nodeOffset + nodeLength) {
438+
if (typeof value === 'string' && offset >= scriptStart && offset < nodeOffset + nodeLength) {
436439
// found the script
437440
inScripts = false;
441+
foundScript = script;
438442
} else {
439443
script = undefined;
440444
}
@@ -447,11 +451,13 @@ export function findScriptAtPosition(buffer: string, offset: number): string | u
447451
else if (inScripts) {
448452
scriptStart = nodeOffset;
449453
script = property;
454+
} else { // nested object which is invalid, ignore the script
455+
script = undefined;
450456
}
451457
}
452458
};
453459
visit(buffer, visitor);
454-
return script;
460+
return foundScript;
455461
}
456462

457463
export async function getScripts(packageJsonUri: Uri): Promise<StringMap | undefined> {

0 commit comments

Comments
 (0)