提交 cc0b4278 编写于 作者: E Erich Gamma

More robust handling of invalid script values

上级 98d2fd80
......@@ -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);
}
}
\ No newline at end of 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> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册