Skip to content

Commit 3633f4b

Browse files
authored
fix: Adding duplicate identifier check in function signatures (#1865)
1 parent 3f70d11 commit 3633f4b

File tree

8 files changed

+43
-5
lines changed

8 files changed

+43
-5
lines changed

src/compiler.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,26 @@ export class Compiler extends DiagnosticEmitter {
14711471
}
14721472
}
14731473

1474+
// ensure the function hasn't duplicate parameters
1475+
var parameters = instance.prototype.functionTypeNode.parameters;
1476+
var numParameters = parameters.length;
1477+
if (numParameters >= 2) {
1478+
let visited = new Set<string>();
1479+
visited.add(parameters[0].name.text);
1480+
for (let i = 1; i < numParameters; i++) {
1481+
let paramIdentifier = parameters[i].name;
1482+
let paramName = paramIdentifier.text;
1483+
if (!visited.has(paramName)) {
1484+
visited.add(paramName);
1485+
} else {
1486+
this.error(
1487+
DiagnosticCode.Duplicate_identifier_0,
1488+
paramIdentifier.range, paramName
1489+
);
1490+
}
1491+
}
1492+
}
1493+
14741494
instance.set(CommonFlags.COMPILED);
14751495
var pendingElements = this.pendingElements;
14761496
pendingElements.add(instance);

src/parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,9 @@ export class Parser extends DiagnosticEmitter {
845845
return null;
846846
}
847847
this.tryParseSignatureIsSignature = true;
848+
848849
if (!parameters) parameters = [];
850+
849851
return Node.createFunctionType(
850852
parameters,
851853
returnType,
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"asc_flags": [
3+
],
4+
"stderr": [
5+
"TS2300: Duplicate identifier 'a'"
36
]
4-
}
7+
}

tests/compiler/function-expression.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,7 @@ export function semanticallyAnonymous(): void {
9393
assert(fnDecl != exprDecl);
9494
}
9595
semanticallyAnonymous();
96+
97+
var duplicateParams = (a: i32, a: i32): void => {};
98+
// TS2300: Duplicate identifier 'a'
99+
duplicateParams(1, 2);

tests/compiler/function.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"asc_flags": [
3+
],
4+
"stderr": [
5+
"TS2300: Duplicate identifier 'a'"
36
]
4-
}
7+
}

tests/compiler/function.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ iii(1, 2);
3939
jjj(1, 2);
4040
fff(1, 2);
4141
ddd(1, 2);
42+
43+
function duplicateParams(a: i32, a: i32): void {}
44+
// TS2300: Duplicate identifier 'a'
45+
duplicateParams(1, 2);

tests/parser/function-type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var a: () => void;
22
var b: (a: i32, b: i32) => void;
33
var c: (a: i32, b: i32) => (a: i32, b: i32) => void;
4-
var d: (a) => void; // TS1110
4+
var d: (a: i32, a: i32) => void; // NOTE: duplicates in type signatures doesn't in TypeScript
5+
var e: (a) => void; // TS1110
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var a: () => void;
22
var b: (a: i32, b: i32) => void;
33
var c: (a: i32, b: i32) => (a: i32, b: i32) => void;
4-
var d: (a) => void;
5-
// ERROR 1110: "Type expected." in function-type.ts(4,10+0)
4+
var d: (a: i32, a: i32) => void;
5+
var e: (a) => void;
6+
// ERROR 1110: "Type expected." in function-type.ts(5,10+0)

0 commit comments

Comments
 (0)