Skip to content

Commit

Permalink
Enable V8 supported stage-3 syntax, as well as stage-4 syntax
Browse files Browse the repository at this point in the history
* Enable Babel parser options directly, rather than through Babel syntax plugins
* Enable stage-3 syntax supported in V8
* Enable stage-4 dynamic import syntax
  • Loading branch information
novemberborn committed Aug 18, 2019
1 parent 7fb0546 commit 9baca8c
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 74 deletions.
42 changes: 27 additions & 15 deletions lib/babel-pipeline.js
Expand Up @@ -58,6 +58,26 @@ function validate(conf) {
};
}

function enableParserPlugins(api) {
api.assertVersion(7);

return {
name: 'ava-babel-pipeline-enable-parser-plugins',
manipulateOptions(_, parserOpts) {
parserOpts.plugins.push(
'asyncGenerators',
'bigInt',
'classPrivateProperties',
'classProperties',
'dynamicImport',
'numericSeparator',
'objectRestSpread',
'optionalCatchBinding'
);
}
};
}

// Compare actual values rather than file paths, which should be
// more reliable.
function makeValueChecker(ref) {
Expand Down Expand Up @@ -118,7 +138,12 @@ function hashPartialTestConfig({babelrc, config, options: {plugins, presets}}, p
inputs.push(config, stripBomBuf(fs.readFileSync(config)));
}

for (const {file: {resolved: filename}} of [...plugins, ...presets]) {
for (const item of [...plugins, ...presets]) {
if (!item.file) {
continue;
}

const {file: {resolved: filename}} = item;
if (pluginAndPresetHashes.has(filename)) {
inputs.push(pluginAndPresetHashes.get(filename));
continue;
Expand Down Expand Up @@ -168,9 +193,6 @@ function build(projectDir, cacheDir, userOptions, compileEnhancements) {
const pluginAndPresetHashes = new Map();

const ensureStage4 = wantsStage4(userOptions, projectDir);
const containsAsyncGenerators = makeValueChecker('@babel/plugin-syntax-async-generators');
const containsObjectRestSpread = makeValueChecker('@babel/plugin-syntax-object-rest-spread');
const containsOptionalCatchBinding = makeValueChecker('@babel/plugin-syntax-optional-catch-binding');
const containsStage4 = makeValueChecker('../stage-4');
const containsTransformTestFiles = makeValueChecker('@ava/babel-preset-transform-test-files');

Expand All @@ -193,17 +215,7 @@ function build(projectDir, cacheDir, userOptions, compileEnhancements) {
}

const {options: testOptions} = partialTestConfig;
if (!testOptions.plugins.some(containsAsyncGenerators)) { // TODO: Remove once Babel can parse this syntax unaided.
testOptions.plugins.unshift(createConfigItem('@babel/plugin-syntax-async-generators', 'plugin'));
}

if (!testOptions.plugins.some(containsObjectRestSpread)) { // TODO: Remove once Babel can parse this syntax unaided.
testOptions.plugins.unshift(createConfigItem('@babel/plugin-syntax-object-rest-spread', 'plugin'));
}

if (!testOptions.plugins.some(containsOptionalCatchBinding)) { // TODO: Remove once Babel can parse this syntax unaided.
testOptions.plugins.unshift(createConfigItem('@babel/plugin-syntax-optional-catch-binding', 'plugin'));
}
testOptions.plugins.push(enableParserPlugins);

if (ensureStage4 && !testOptions.presets.some(containsStage4)) {
// Apply last.
Expand Down
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -61,9 +61,6 @@
"@ava/babel-preset-transform-test-files": "^6.0.0",
"@babel/core": "^7.5.5",
"@babel/generator": "^7.5.5",
"@babel/plugin-syntax-async-generators": "^7.2.0",
"@babel/plugin-syntax-object-rest-spread": "^7.2.0",
"@babel/plugin-syntax-optional-catch-binding": "^7.2.0",
"@concordance/react": "^2.0.0",
"ansi-escapes": "^4.2.1",
"ansi-styles": "^4.0.0",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions test/integration/babel.js
Expand Up @@ -46,12 +46,3 @@ test('includes relative paths in source map', t => {
t.end();
});
});

for (const plugin of ['async-generators', 'object-rest-spread', 'optional-catch-binding']) {
test(`avoids applying '@babel/plugin-syntax-${plugin}' if already in config`, t => {
execCli([], {dirname: `fixture/babel/with-explicit-syntax-plugins/${plugin}`}, err => {
t.ifError(err);
t.end();
});
});
}

0 comments on commit 9baca8c

Please sign in to comment.