fix(index): add many more Babel parser plugins
We should be as tolerant as possible of new syntax, rather than
failing the parse.
This change makes it possible to parse, e.g. private class member
syntax.
MODIFY
src/index.js
src/index.js
@@ -5,11 +5,43 @@ import makeModulePlugins from './babelPlugin';
const makeModuleTransformer = (babelCore, importer) => {
function transformSource(source, sourceOptions = {}) {
// Transform the script/expression source for import expressions.
- const parserPlugins = ['dynamicImport'];
+ const parserPlugins = [];
if (sourceOptions.sourceType === 'module') {
- parserPlugins.push('importMeta');
+ parserPlugins.push(
+ 'exportDefaultFrom',
+ 'exportNamespaceFrom',
+ 'importMeta',
+ );
}
+ // This list taken and amended from:
+ // https://github.com/prettier/prettier/tree/master/src/language-js/parser-babylon.js#L21
+ parserPlugins.push(
+ 'doExpressions',
+ 'objectRestSpread',
+ 'classProperties',
+ // 'exportDefaultFrom', // only for modules, above
+ // 'exportNamespaceFrom', // only for modules, above
+ 'asyncGenerators',
+ 'functionBind',
+ 'functionSent',
+ 'dynamicImport', // needed for our rewrite
+ 'numericSeparator',
+ // 'importMeta', // only for modules, above
+ 'optionalCatchBinding',
+ 'optionalChaining',
+ 'classPrivateProperties',
+ ['pipelineOperator', { proposal: 'minimal' }],
+ 'nullishCoalescingOperator',
+ 'bigInt',
+ 'throwExpressions',
+ 'logicalAssignment',
+ 'classPrivateMethods',
+ // 'v8intrinsic', // we really don't want people to rely on platform powers
+ 'partialApplication',
+ ['decorators', { decoratorsBeforeExport: false }],
+ );
+
// console.log(`transforming`, sourceOptions, source);
const modulePlugins = makeModulePlugins(sourceOptions);
const output = babelCore.transformSync(source, {

MODIFY
test/test-sanity.js
test/test-sanity.js
@@ -33,6 +33,20 @@ test('sanity', async t => {
'zero width joiner non-reserved works',
);
+ t.equal(
+ evaluateModule(`\
+class outer {
+ #x = 42;
+ f() {
+ return this.#x;
+ }
+}
+new outer().f();
+`),
+ 42,
+ 'private member syntax works',
+ );
+
t.equal(evaluateModule('123; 456'), 456, 'program evaluates');
t.equal(evaluateExpr('123'), 123, 'expression evaluates');
t.throws(

fix(index): add many more Babel parser plugins

We should be as tolerant as possible of new syntax, rather than
failing the parse.

This change makes it possible to parse, e.g. private class member
syntax.