diff --git a/examples/example.ts b/examples/example.ts new file mode 100644 index 0000000..dafbbfb --- /dev/null +++ b/examples/example.ts @@ -0,0 +1,26 @@ +let a = (a: string, b: Record, c?: number): void => {}; + +a("a", {}, 11); + +type A = a; + +function b( + a: string, + b: A, + c: Record, + d: number +): (() => void) | void {} + +b("aa", "aa", {}, 2); + +class B { + foo(a: (c: string, q: string) => void, c:number) {} + + bar: (a: string) => void; +} + +new B().foo(() => {}, 1); + +new B().bar("aa"); + +a.call(this, 1); diff --git a/src/drivers/abstract-javascript.ts b/src/drivers/abstract-javascript.ts index e3d2366..d1a0bb4 100644 --- a/src/drivers/abstract-javascript.ts +++ b/src/drivers/abstract-javascript.ts @@ -10,15 +10,47 @@ export function getParameterName(editor: vscode.TextEditor, position: vscode.Pos const shouldHideRedundantAnnotations = vscode.workspace.getConfiguration('inline-parameters').get('hideRedundantAnnotations') if (description && description.length > 0) { - try { - const functionDefinitionRegex = /[^ ](?!^)\((.*)\)\:/gm - let definition = description[0].contents[0].value.match(functionDefinitionRegex) + try { + let definition = description[0].contents[0].value; + // Find the bracket matching () => or () : + let pos = [0, -1]; + let bracketsCount = 0; + let isFindingBrackets = false; + for (let i = definition.length - 1; i >= 0; i--) { + const e = definition[i]; + switch (e) { + case ")": + if (bracketsCount === 0 && isFindingBrackets) pos[1] = i; + bracketsCount ++; + break; + case "(": + bracketsCount --; + if (bracketsCount === 0 && isFindingBrackets) { + pos[0] = i; + isFindingBrackets = false; + } + break; + case ":": + case "=": + if (bracketsCount === 0) + isFindingBrackets = true; + break; + case " ": + break; + default: + if (bracketsCount === 0) isFindingBrackets = false; + break; + } + } + definition = definition.slice(pos[0], pos[1] + 1); if (!definition || definition.length === 0) { return reject() } - definition = definition[0].slice(2, -2) + definition = definition.slice(1, -1) + .replace(/\<.*?\>/g,'') + .replace(/\(.*?\)/g,''); const jsParameterNameRegex = /^[a-zA-Z_$]([0-9a-zA-Z_$]+)?/g @@ -38,6 +70,11 @@ export function getParameterName(editor: vscode.TextEditor, position: vscode.Pos return parameter }) + + // Typescript allows "this" type + if (parameters[0] === "this") { + parameters.shift(); + } } catch (err) { console.error(err) } diff --git a/tsconfig.json b/tsconfig.json index 8937be0..a4b5818 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ }, "exclude": [ "node_modules", - ".vscode-test" + ".vscode-test", + "examples" ] }