/
index.js
69 lines (57 loc) 路 1.82 KB
/
index.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
import { parse } from "@babel/core";
import syntaxDecorators from "../lib";
function makeParser(code, options) {
return () =>
parse(code, {
babelrc: false,
configFile: false,
plugins: [[syntaxDecorators, options]],
});
}
describe("'legacy' option", function() {
test("must be boolean", function() {
expect(makeParser("", { legacy: "legacy" })).toThrow();
});
test("'legacy': false", function() {
expect(makeParser("({ @dec fn() {} })", { legacy: false })).toThrow();
});
test("'legacy': true", function() {
expect(makeParser("({ @dec fn() {} })", { legacy: true })).not.toThrow();
});
test("defaults to 'false'", function() {
expect(makeParser("({ @dec fn() {} })", {})).toThrow();
});
});
describe("'decoratorsBeforeExport' option", function() {
test("must be boolean", function() {
expect(makeParser("", { decoratorsBeforeExport: "before" })).toThrow();
});
test("is required", function() {
expect(makeParser("", { legacy: false })).toThrow(/decoratorsBeforeExport/);
});
test("is incompatible with legacy", function() {
expect(
makeParser("", { decoratorsBeforeExport: false, legacy: true }),
).toThrow();
});
const BEFORE = "@dec export class Foo {}";
const AFTER = "export @dec class Foo {}";
// These are skipped
run(BEFORE, true, false);
run(AFTER, true, true);
run(BEFORE, false, true);
run(AFTER, false, false);
function run(code, before, throws) {
const name =
(before === undefined ? "default" : before) +
" - decorators " +
(code === BEFORE ? "before" : "after") +
"export";
test(name, function() {
const expectTheParser = expect(
makeParser(code, { decoratorsBeforeExport: before }),
);
throws ? expectTheParser.toThrow() : expectTheParser.not.toThrow();
});
}
});