Skip to content

fix: Adding duplicate identifier check in function signatures #1865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,26 @@ export class Compiler extends DiagnosticEmitter {
}
}

// ensure the function hasn't duplicate parameters
var parameters = instance.prototype.functionTypeNode.parameters;
var numParameters = parameters.length;
if (numParameters >= 2) {
let visited = new Set<string>();
visited.add(parameters[0].name.text);
for (let i = 1; i < numParameters; i++) {
let paramIdentifier = parameters[i].name;
let paramName = paramIdentifier.text;
if (!visited.has(paramName)) {
visited.add(paramName);
} else {
this.error(
DiagnosticCode.Duplicate_identifier_0,
paramIdentifier.range, paramName
);
}
}
}

instance.set(CommonFlags.COMPILED);
var pendingElements = this.pendingElements;
pendingElements.add(instance);
Expand Down
2 changes: 2 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,9 @@ export class Parser extends DiagnosticEmitter {
return null;
}
this.tryParseSignatureIsSignature = true;

if (!parameters) parameters = [];

return Node.createFunctionType(
parameters,
returnType,
Expand Down
5 changes: 4 additions & 1 deletion tests/compiler/function-expression.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"asc_flags": [
],
"stderr": [
"TS2300: Duplicate identifier 'a'"
]
}
}
4 changes: 4 additions & 0 deletions tests/compiler/function-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ export function semanticallyAnonymous(): void {
assert(fnDecl != exprDecl);
}
semanticallyAnonymous();

var duplicateParams = (a: i32, a: i32): void => {};
// TS2300: Duplicate identifier 'a'
duplicateParams(1, 2);
5 changes: 4 additions & 1 deletion tests/compiler/function.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"asc_flags": [
],
"stderr": [
"TS2300: Duplicate identifier 'a'"
]
}
}
4 changes: 4 additions & 0 deletions tests/compiler/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ iii(1, 2);
jjj(1, 2);
fff(1, 2);
ddd(1, 2);

function duplicateParams(a: i32, a: i32): void {}
// TS2300: Duplicate identifier 'a'
duplicateParams(1, 2);
3 changes: 2 additions & 1 deletion tests/parser/function-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var a: () => void;
var b: (a: i32, b: i32) => void;
var c: (a: i32, b: i32) => (a: i32, b: i32) => void;
var d: (a) => void; // TS1110
var d: (a: i32, a: i32) => void; // NOTE: duplicates in type signatures doesn't in TypeScript
var e: (a) => void; // TS1110
5 changes: 3 additions & 2 deletions tests/parser/function-type.ts.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var a: () => void;
var b: (a: i32, b: i32) => void;
var c: (a: i32, b: i32) => (a: i32, b: i32) => void;
var d: (a) => void;
// ERROR 1110: "Type expected." in function-type.ts(4,10+0)
var d: (a: i32, a: i32) => void;
var e: (a) => void;
// ERROR 1110: "Type expected." in function-type.ts(5,10+0)