Skip to content

Commit 02f5365

Browse files
committed
improve error message and update testcase
1 parent 813f28a commit 02f5365

11 files changed

+172
-97
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28717,16 +28717,7 @@ namespace ts {
2871728717
}
2871828718

2871928719
function getNonSimpleParameters(parameters: ReadonlyArray<ParameterDeclaration>): ReadonlyArray<ParameterDeclaration> {
28720-
// ECMA-262 14.1.13
28721-
if (parameters.length === 0) return [];
28722-
28723-
const last = lastOrUndefined(parameters);
28724-
if (last && isRestParameter(last)) return [last];
28725-
28726-
return filter(parameters, parameter => {
28727-
// ECMA-262 13.3.3.4
28728-
return !!parameter.initializer || isBindingPattern(parameter.name);
28729-
});
28720+
return filter(parameters, parameter => !!parameter.initializer || isBindingPattern(parameter.name) || isRestParameter(parameter));
2873028721
}
2873128722

2873228723
function checkGrammarForUseStrictSimpleParameterList(node: FunctionLikeDeclaration): boolean {
@@ -28738,12 +28729,12 @@ namespace ts {
2873828729
forEach(nonSimpleParameters, parameter => {
2873928730
addRelatedInfo(
2874028731
error(parameter, Diagnostics.This_parameter_is_not_allowed_with_use_strict_directive),
28741-
createDiagnosticForNode(useStrictDirective, Diagnostics._0_is_here, "use strict directive")
28732+
createDiagnosticForNode(useStrictDirective, Diagnostics.use_strict_directive_used_here)
2874228733
);
2874328734
});
2874428735

2874528736
const diagnostics = nonSimpleParameters.map((parameter, index) => (
28746-
index === 0 ? createDiagnosticForNode(parameter, Diagnostics._0_is_here, "parameter") : createDiagnosticForNode(parameter, Diagnostics.and_here)
28737+
index === 0 ? createDiagnosticForNode(parameter, Diagnostics.Non_simple_parameter_declared_here) : createDiagnosticForNode(parameter, Diagnostics.and_here)
2874728738
)) as [DiagnosticWithLocation, ...DiagnosticWithLocation[]];
2874828739
addRelatedInfo(error(useStrictDirective, Diagnostics.use_strict_directive_cannot_be_used_with_non_simple_parameter_list), ...diagnostics);
2874928740
return true;

src/compiler/diagnosticMessages.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,14 +991,18 @@
991991
"category": "Error",
992992
"code": 1345
993993
},
994-
"'use strict' directive cannot be used with non simple parameter list.": {
994+
"'use strict' directive cannot be used with non-simple parameter list.": {
995995
"category": "Error",
996996
"code": 1346
997997
},
998-
"{0} is here.": {
998+
"Non-simple parameter declared here.": {
999999
"category": "Error",
10001000
"code": 1347
10011001
},
1002+
"'use strict' directive used here.": {
1003+
"category": "Error",
1004+
"code": 1348
1005+
},
10021006

10031007
"Duplicate identifier '{0}'.": {
10041008
"category": "Error",

tests/baselines/reference/functionWithUseStrictAndSimpleParameterList.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ function rest(...args: any[]) {
1717
'use strict';
1818
}
1919

20+
function rest1(a = 1, ...args) {
21+
'use strict';
22+
}
23+
2024
function paramDefault(param = 1) {
2125
'use strict';
2226
}
@@ -69,6 +73,14 @@ function rest() {
6973
args[_i] = arguments[_i];
7074
}
7175
}
76+
function rest1(a) {
77+
'use strict';
78+
if (a === void 0) { a = 1; }
79+
var args = [];
80+
for (var _i = 1; _i < arguments.length; _i++) {
81+
args[_i - 1] = arguments[_i];
82+
}
83+
}
7284
function paramDefault(param) {
7385
'use strict';
7486
if (param === void 0) { param = 1; }

tests/baselines/reference/functionWithUseStrictAndSimpleParameterList.symbols

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,52 +31,60 @@ function rest(...args: any[]) {
3131
'use strict';
3232
}
3333

34+
function rest1(a = 1, ...args) {
35+
>rest1 : Symbol(rest1, Decl(functionWithUseStrictAndSimpleParameterList.ts, 16, 1))
36+
>a : Symbol(a, Decl(functionWithUseStrictAndSimpleParameterList.ts, 18, 15))
37+
>args : Symbol(args, Decl(functionWithUseStrictAndSimpleParameterList.ts, 18, 21))
38+
39+
'use strict';
40+
}
41+
3442
function paramDefault(param = 1) {
35-
>paramDefault : Symbol(paramDefault, Decl(functionWithUseStrictAndSimpleParameterList.ts, 16, 1))
36-
>param : Symbol(param, Decl(functionWithUseStrictAndSimpleParameterList.ts, 18, 22))
43+
>paramDefault : Symbol(paramDefault, Decl(functionWithUseStrictAndSimpleParameterList.ts, 20, 1))
44+
>param : Symbol(param, Decl(functionWithUseStrictAndSimpleParameterList.ts, 22, 22))
3745

3846
'use strict';
3947
}
4048

4149
function objectBindingPattern({foo}: any) {
42-
>objectBindingPattern : Symbol(objectBindingPattern, Decl(functionWithUseStrictAndSimpleParameterList.ts, 20, 1))
43-
>foo : Symbol(foo, Decl(functionWithUseStrictAndSimpleParameterList.ts, 22, 31))
50+
>objectBindingPattern : Symbol(objectBindingPattern, Decl(functionWithUseStrictAndSimpleParameterList.ts, 24, 1))
51+
>foo : Symbol(foo, Decl(functionWithUseStrictAndSimpleParameterList.ts, 26, 31))
4452

4553
'use strict';
4654
}
4755

4856
function arrayBindingPattern([foo]: any[]) {
49-
>arrayBindingPattern : Symbol(arrayBindingPattern, Decl(functionWithUseStrictAndSimpleParameterList.ts, 24, 1))
50-
>foo : Symbol(foo, Decl(functionWithUseStrictAndSimpleParameterList.ts, 26, 30))
57+
>arrayBindingPattern : Symbol(arrayBindingPattern, Decl(functionWithUseStrictAndSimpleParameterList.ts, 28, 1))
58+
>foo : Symbol(foo, Decl(functionWithUseStrictAndSimpleParameterList.ts, 30, 30))
5159

5260
'use strict';
5361
}
5462

5563
function manyParameter(a = 10, b = 20) {
56-
>manyParameter : Symbol(manyParameter, Decl(functionWithUseStrictAndSimpleParameterList.ts, 28, 1))
57-
>a : Symbol(a, Decl(functionWithUseStrictAndSimpleParameterList.ts, 30, 23))
58-
>b : Symbol(b, Decl(functionWithUseStrictAndSimpleParameterList.ts, 30, 30))
64+
>manyParameter : Symbol(manyParameter, Decl(functionWithUseStrictAndSimpleParameterList.ts, 32, 1))
65+
>a : Symbol(a, Decl(functionWithUseStrictAndSimpleParameterList.ts, 34, 23))
66+
>b : Symbol(b, Decl(functionWithUseStrictAndSimpleParameterList.ts, 34, 30))
5967

6068
"use strict";
6169
}
6270

6371
function manyPrologue(a = 10, b = 20) {
64-
>manyPrologue : Symbol(manyPrologue, Decl(functionWithUseStrictAndSimpleParameterList.ts, 32, 1))
65-
>a : Symbol(a, Decl(functionWithUseStrictAndSimpleParameterList.ts, 34, 22))
66-
>b : Symbol(b, Decl(functionWithUseStrictAndSimpleParameterList.ts, 34, 29))
72+
>manyPrologue : Symbol(manyPrologue, Decl(functionWithUseStrictAndSimpleParameterList.ts, 36, 1))
73+
>a : Symbol(a, Decl(functionWithUseStrictAndSimpleParameterList.ts, 38, 22))
74+
>b : Symbol(b, Decl(functionWithUseStrictAndSimpleParameterList.ts, 38, 29))
6775

6876
"foo";
6977
"use strict";
7078
}
7179

7280
function invalidPrologue(a = 10, b = 20) {
73-
>invalidPrologue : Symbol(invalidPrologue, Decl(functionWithUseStrictAndSimpleParameterList.ts, 37, 1))
74-
>a : Symbol(a, Decl(functionWithUseStrictAndSimpleParameterList.ts, 39, 25))
75-
>b : Symbol(b, Decl(functionWithUseStrictAndSimpleParameterList.ts, 39, 32))
81+
>invalidPrologue : Symbol(invalidPrologue, Decl(functionWithUseStrictAndSimpleParameterList.ts, 41, 1))
82+
>a : Symbol(a, Decl(functionWithUseStrictAndSimpleParameterList.ts, 43, 25))
83+
>b : Symbol(b, Decl(functionWithUseStrictAndSimpleParameterList.ts, 43, 32))
7684

7785
"foo";
7886
const c = 1;
79-
>c : Symbol(c, Decl(functionWithUseStrictAndSimpleParameterList.ts, 41, 9))
87+
>c : Symbol(c, Decl(functionWithUseStrictAndSimpleParameterList.ts, 45, 9))
8088

8189
"use strict";
8290
}

tests/baselines/reference/functionWithUseStrictAndSimpleParameterList.types

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ function rest(...args: any[]) {
3939
>'use strict' : "use strict"
4040
}
4141

42+
function rest1(a = 1, ...args) {
43+
>rest1 : (a?: number, ...args: any[]) => void
44+
>a : number
45+
>1 : 1
46+
>args : any[]
47+
48+
'use strict';
49+
>'use strict' : "use strict"
50+
}
51+
4252
function paramDefault(param = 1) {
4353
>paramDefault : (param?: number) => void
4454
>param : number

0 commit comments

Comments
 (0)