More robust handling of invalid script values

This commit is contained in:
Erich Gamma 2018-09-18 08:28:49 +02:00
parent 98d2fd80d2
commit cc0b42784c
2 changed files with 10 additions and 4 deletions

View file

@ -26,7 +26,7 @@ export function runSelectedScript() {
if (script) {
runScript(script, document);
} else {
let message = localize('noScriptFound', 'Could not find an npm script at the selection.');
let message = localize('noScriptFound', 'Could not find a valid npm script at the selection.');
vscode.window.showErrorMessage(message);
}
}

View file

@ -361,7 +361,9 @@ async function findAllScripts(buffer: string): Promise<StringMap> {
},
onLiteralValue(value: any, _offset: number, _length: number) {
if (script) {
scripts[script] = value;
if (typeof value === 'string') {
scripts[script] = value;
}
script = undefined;
}
},
@ -419,6 +421,7 @@ export function findAllScriptRanges(buffer: string): Map<string, [number, number
export function findScriptAtPosition(buffer: string, offset: number): string | undefined {
let script: string | undefined = undefined;
let foundScript: string | undefined = undefined;
let inScripts = false;
let scriptStart: number | undefined;
let visitor: JSONVisitor = {
@ -432,9 +435,10 @@ export function findScriptAtPosition(buffer: string, offset: number): string | u
},
onLiteralValue(value: any, nodeOffset: number, nodeLength: number) {
if (inScripts && scriptStart) {
if (offset >= scriptStart && offset < nodeOffset + nodeLength) {
if (typeof value === 'string' && offset >= scriptStart && offset < nodeOffset + nodeLength) {
// found the script
inScripts = false;
foundScript = script;
} else {
script = undefined;
}
@ -447,11 +451,13 @@ export function findScriptAtPosition(buffer: string, offset: number): string | u
else if (inScripts) {
scriptStart = nodeOffset;
script = property;
} else { // nested object which is invalid, ignore the script
script = undefined;
}
}
};
visit(buffer, visitor);
return script;
return foundScript;
}
export async function getScripts(packageJsonUri: Uri): Promise<StringMap | undefined> {