Skip to content

Commit 162c59c

Browse files
feat: add specific error message for reserved keywords (#5657)
* feat: add specific error message for reserved keywords * feat: updated the express.ts to do the isReservedES6Keyword only once
1 parent d1bda37 commit 162c59c

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

packages/@lwc/errors/src/compiler/error-info/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
77
/**
8-
* Next error code: 1211
8+
* Next error code: 1212
99
*/
1010

1111
export * from './compiler';

packages/@lwc/errors/src/compiler/error-info/template-transform.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,4 +1020,11 @@ export const ParserDiagnostics = {
10201020
level: DiagnosticLevel.Error,
10211021
url: '',
10221022
},
1023+
1024+
RESERVED_KEYWORD_AS_IDENTIFIER: {
1025+
code: 1211,
1026+
message: '{0} is a reserved keyword and cannot be used as an identifier.',
1027+
level: DiagnosticLevel.Error,
1028+
url: '',
1029+
},
10231030
} as const satisfies Record<string, LWCErrorInfo>;

packages/@lwc/template-compiler/src/__tests__/fixtures/expression/reverved-keyword/metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"url": ""
1414
},
1515
{
16-
"code": 1059,
17-
"message": "LWC1059: const is not a valid identifier",
16+
"code": 1211,
17+
"message": "LWC1211: const is a reserved keyword and cannot be used as an identifier.",
1818
"level": 1,
1919
"location": {
2020
"line": 3,

packages/@lwc/template-compiler/src/parser/expression.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ export function parseIdentifier(
177177
isValid = isIdentifierChar(source.charCodeAt(i));
178178
}
179179

180-
if (isValid && !isReservedES6Keyword(source)) {
180+
if (isValid) {
181+
if (isReservedES6Keyword(source)) {
182+
ctx.throwAtLocation(ParserDiagnostics.RESERVED_KEYWORD_AS_IDENTIFIER, location, [
183+
source,
184+
]);
185+
}
181186
return {
182187
...t.identifier(source),
183188
location,

0 commit comments

Comments
 (0)