-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (36 loc) · 1.23 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
const babel = require("@babel/core");
const thisPreset = require("..");
const fixtures = require("./fixtures.json");
const VERSION_RE = /^v?(?<major>\d+)(?:\.(?<minor>\d+))?(?:\.(?<patch>\d+))?/;
const currentVersion = parseVersion(process.version);
console.log(`INFO - Running on node ${process.version}`);
for (const [name, [version, code, parser]] of Object.entries(fixtures)) {
if (name.startsWith("#")) continue; // "JSON comments"
const shouldThrow = parseVersion(version) > currentVersion;
let didThrow = false;
try {
babel.parseSync(code, {
configFile: false,
presets: [thisPreset],
plugins: [selectParser(parser)],
});
} catch {
didThrow = true;
}
const msg = `${name} (${version}) ${didThrow ? "threw" : "didn't throw"}`;
if (didThrow === shouldThrow) {
console.log(`OK - ${msg}, as expected.`);
} else {
console.log(`FAIL - ${msg}, unexpectedly.`);
process.exitCode = 1;
}
}
function selectParser(version = "@babel/parser-7.0.0") {
return () => ({
parserOverride: require(version).parse
});
}
function parseVersion(v) {
const { major, minor = 0, patch = 0 } = String(v).match(VERSION_RE).groups;
return Number(major) * 1e8 + Number(minor) * 1e4 + Number(patch);
}