mirror of
https://github.com/hanzoai/babel.git
synced 2026-09-24 16:56:45 +00:00
118 lines
3.2 KiB
JavaScript
118 lines
3.2 KiB
JavaScript
import { parse } from "../lib/index.js";
|
|
|
|
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 });
|
|
});
|
|
});
|
|
|
|
describe("locations", () => {
|
|
it("false", () => {
|
|
expect(parse(CODE, { locations: false })).toMatchInlineSnapshot(`
|
|
Node {
|
|
"comments": [],
|
|
"end": 19,
|
|
"errors": [],
|
|
"program": Node {
|
|
"body": [
|
|
Node {
|
|
"async": false,
|
|
"body": Node {
|
|
"body": [],
|
|
"directives": [],
|
|
"end": 19,
|
|
"start": 17,
|
|
"type": "BlockStatement",
|
|
},
|
|
"end": 19,
|
|
"generator": false,
|
|
"id": Node {
|
|
"end": 10,
|
|
"name": "f",
|
|
"start": 9,
|
|
"type": "Identifier",
|
|
},
|
|
"params": [
|
|
Node {
|
|
"end": 12,
|
|
"name": "x",
|
|
"start": 11,
|
|
"type": "Identifier",
|
|
},
|
|
Node {
|
|
"end": 15,
|
|
"name": "x",
|
|
"start": 14,
|
|
"type": "Identifier",
|
|
},
|
|
],
|
|
"start": 0,
|
|
"type": "FunctionDeclaration",
|
|
},
|
|
],
|
|
"directives": [],
|
|
"end": 19,
|
|
"interpreter": null,
|
|
"sourceType": "script",
|
|
"start": 0,
|
|
"type": "Program",
|
|
},
|
|
"start": 0,
|
|
"type": "File",
|
|
}
|
|
`);
|
|
});
|
|
});
|
|
});
|
|
});
|