fix: Exclude catch clause from let identifier error (#10559)
* Exclude catch clause from let identifier error * Disallow let binding based on parameter * Add test * Remove unused getter * Update packages/babel-parser/src/parser/statement.js Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
parent
487f10f84d
commit
095f28a913
@ -17,7 +17,7 @@ import type {
|
||||
import type { Pos, Position } from "../util/location";
|
||||
import { isStrictBindReservedWord } from "../util/identifier";
|
||||
import { NodeUtils } from "./node";
|
||||
import { type BindingTypes, BIND_NONE, BIND_LEXICAL } from "../util/scopeflags";
|
||||
import { type BindingTypes, BIND_NONE } from "../util/scopeflags";
|
||||
|
||||
export default class LValParser extends NodeUtils {
|
||||
// Forward-declaration: defined in expression.js
|
||||
@ -348,6 +348,7 @@ export default class LValParser extends NodeUtils {
|
||||
bindingType: BindingTypes = BIND_NONE,
|
||||
checkClashes: ?{ [key: string]: boolean },
|
||||
contextDescription: string,
|
||||
disallowLetBinding?: boolean,
|
||||
): void {
|
||||
switch (expr.type) {
|
||||
case "Identifier":
|
||||
@ -383,7 +384,7 @@ export default class LValParser extends NodeUtils {
|
||||
checkClashes[key] = true;
|
||||
}
|
||||
}
|
||||
if (bindingType === BIND_LEXICAL && expr.name === "let") {
|
||||
if (disallowLetBinding && expr.name === "let") {
|
||||
this.raise(
|
||||
expr.start,
|
||||
"'let' is not allowed to be used as a name in 'let' or 'const' declarations.",
|
||||
@ -408,6 +409,7 @@ export default class LValParser extends NodeUtils {
|
||||
bindingType,
|
||||
checkClashes,
|
||||
"object destructuring pattern",
|
||||
disallowLetBinding,
|
||||
);
|
||||
}
|
||||
break;
|
||||
@ -420,6 +422,7 @@ export default class LValParser extends NodeUtils {
|
||||
bindingType,
|
||||
checkClashes,
|
||||
"array destructuring pattern",
|
||||
disallowLetBinding,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1021,6 +1021,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
kind === "var" ? BIND_VAR : BIND_LEXICAL,
|
||||
undefined,
|
||||
"variable declaration",
|
||||
kind !== "var",
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -108,6 +108,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
bindingType: BindingTypes = BIND_NONE,
|
||||
checkClashes: ?{ [key: string]: boolean },
|
||||
contextDescription: string,
|
||||
disallowLetBinding?: boolean,
|
||||
): void {
|
||||
switch (expr.type) {
|
||||
case "ObjectPattern":
|
||||
@ -117,11 +118,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
bindingType,
|
||||
checkClashes,
|
||||
"object destructuring pattern",
|
||||
disallowLetBinding,
|
||||
);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
super.checkLVal(expr, bindingType, checkClashes, contextDescription);
|
||||
super.checkLVal(
|
||||
expr,
|
||||
bindingType,
|
||||
checkClashes,
|
||||
contextDescription,
|
||||
disallowLetBinding,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
3
packages/babel-parser/test/fixtures/es2015/let/let-at-catch-block/input.js
vendored
Normal file
3
packages/babel-parser/test/fixtures/es2015/let/let-at-catch-block/input.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
try {} catch (err) {
|
||||
let let;
|
||||
}
|
||||
3
packages/babel-parser/test/fixtures/es2015/let/let-at-catch-block/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/es2015/let/let-at-catch-block/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "'let' is not allowed to be used as a name in 'let' or 'const' declarations. (2:6)"
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/es2015/let/try-catch-let/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/es2015/let/try-catch-let/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
try {} catch (let) {}
|
||||
117
packages/babel-parser/test/fixtures/es2015/let/try-catch-let/output.json
vendored
Normal file
117
packages/babel-parser/test/fixtures/es2015/let/try-catch-let/output.json
vendored
Normal file
@ -0,0 +1,117 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 21,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 21
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 21,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 21
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "TryStatement",
|
||||
"start": 0,
|
||||
"end": 21,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 21
|
||||
}
|
||||
},
|
||||
"block": {
|
||||
"type": "BlockStatement",
|
||||
"start": 4,
|
||||
"end": 6,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
}
|
||||
},
|
||||
"body": [],
|
||||
"directives": []
|
||||
},
|
||||
"handler": {
|
||||
"type": "CatchClause",
|
||||
"start": 7,
|
||||
"end": 21,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 21
|
||||
}
|
||||
},
|
||||
"param": {
|
||||
"type": "Identifier",
|
||||
"start": 14,
|
||||
"end": 17,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 14
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 17
|
||||
},
|
||||
"identifierName": "let"
|
||||
},
|
||||
"name": "let"
|
||||
},
|
||||
"body": {
|
||||
"type": "BlockStatement",
|
||||
"start": 19,
|
||||
"end": 21,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 19
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 21
|
||||
}
|
||||
},
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
},
|
||||
"finalizer": null
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user