fix: parser strictMode option (#13548)
This commit is contained in:
parent
2c6db56696
commit
dd942f92af
@ -33,7 +33,11 @@ export default class State {
|
|||||||
|
|
||||||
init(options: Options): void {
|
init(options: Options): void {
|
||||||
this.strict =
|
this.strict =
|
||||||
options.strictMode === false ? false : options.sourceType === "module";
|
options.strictMode === false
|
||||||
|
? false
|
||||||
|
: options.strictMode === true
|
||||||
|
? true
|
||||||
|
: options.sourceType === "module";
|
||||||
|
|
||||||
this.curLine = options.startLine;
|
this.curLine = options.startLine;
|
||||||
this.startLoc = this.endLoc = this.curPosition();
|
this.startLoc = this.endLoc = this.curPosition();
|
||||||
|
|||||||
59
packages/babel-parser/test/options.js
Normal file
59
packages/babel-parser/test/options.js
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import { parse } from "../lib";
|
||||||
|
|
||||||
|
describe("options", () => {
|
||||||
|
describe("strictMode", () => {
|
||||||
|
const CODE = "function f(x, x) {}";
|
||||||
|
|
||||||
|
function expectToSucceed(opts) {
|
||||||
|
expect(parse(CODE, opts).program.body[0]).toMatchObject({
|
||||||
|
type: "FunctionDeclaration",
|
||||||
|
id: { type: "Identifier", name: "f" },
|
||||||
|
generator: false,
|
||||||
|
async: false,
|
||||||
|
params: [
|
||||||
|
{ type: "Identifier", name: "x" },
|
||||||
|
{ type: "Identifier", name: "x" },
|
||||||
|
],
|
||||||
|
body: {
|
||||||
|
type: "BlockStatement",
|
||||||
|
body: [],
|
||||||
|
directives: [],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectToFail(opts) {
|
||||||
|
expect(() => parse(CODE, opts)).toThrow(
|
||||||
|
new SyntaxError("Argument name clash. (1:14)"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("sourceType module", () => {
|
||||||
|
it("default parses as strict mode", () => {
|
||||||
|
expectToFail({ sourceType: "module" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("false parses as sloppy mode", () => {
|
||||||
|
expectToSucceed({ sourceType: "module", strictMode: false });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("true parses as strict mode", () => {
|
||||||
|
expectToFail({ sourceType: "module", strictMode: true });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("sourceType script", () => {
|
||||||
|
it("default parses as sloppy mode", () => {
|
||||||
|
expectToSucceed({ sourceType: "script" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("false parses as sloppy mode", () => {
|
||||||
|
expectToSucceed({ sourceType: "script", strictMode: false });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("true parses as strict mode", () => {
|
||||||
|
expectToFail({ sourceType: "script", strictMode: true });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user