-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
plugin-options.js
77 lines (66 loc) 路 2.53 KB
/
plugin-options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { parse } from "../lib/index.js";
function getParser(code, plugins) {
return () => parse(code, { plugins, sourceType: "module" });
}
describe("plugin options", function () {
describe("the first options are used", function () {
// NOTE: This test is not specific about decorators, it can be applied
// to any plugin with options.
it("when they aren't specified", function () {
const WITHOUT_FLAG = "flow";
const WITH_FLAG = ["flow", { all: true }];
const CODE = "new Foo<x>(y)";
const AST_WITHOUT_FLAG = {
type: "BinaryExpression",
operator: ">",
left: {
type: "BinaryExpression",
operator: "<",
left: { type: "NewExpression" },
right: { type: "Identifier" },
},
right: { type: "Identifier", extra: { parenthesized: true } },
};
const AST_WITH_FLAG = {
type: "NewExpression",
callee: { type: "Identifier" },
arguments: [{ type: "Identifier" }],
typeArguments: {
type: "TypeParameterInstantiation",
params: [
{ type: "GenericTypeAnnotation", id: { type: "Identifier" } },
],
},
};
expect(
getParser(CODE, [WITHOUT_FLAG, WITH_FLAG])().program.body[0].expression,
).toMatchObject(AST_WITHOUT_FLAG);
expect(
getParser(CODE, [WITHOUT_FLAG])().program.body[0].expression,
).toMatchObject(AST_WITHOUT_FLAG);
expect(
getParser(CODE, [WITH_FLAG])().program.body[0].expression,
).toMatchObject(AST_WITH_FLAG);
});
it("when they are specified", function () {
const NAME = "decorators";
const OPT_1 = [NAME, { decoratorsBeforeExport: true }];
const OPT_2 = [NAME, { decoratorsBeforeExport: false }];
const SYNTAX_1 = "@dec export class C {}";
const SYNTAX_2 = "export @dec class C {}";
expect(getParser(SYNTAX_1, [OPT_1, OPT_2])).not.toThrow();
expect(getParser(SYNTAX_2, [OPT_2, OPT_1])).not.toThrow();
expect(getParser(SYNTAX_1, [OPT_2, OPT_1])).toThrow();
expect(getParser(SYNTAX_2, [OPT_1, OPT_2])).toThrow();
});
});
describe("'moduleAttributes' plugin", () => {
(process.env.BABEL_8_BREAKING ? it : it.skip)("removed in Babel 8", () => {
expect(
getParser("", ["moduleAttributes"]),
).toThrowErrorMatchingInlineSnapshot(
`"\`moduleAttributes\` has been removed in Babel 8, please use \`importAssertions\` parser plugin, or \`@babel/plugin-syntax-import-assertions\`."`,
);
});
});
});