Skip to content

Commit

Permalink
fix: use all syntax plugins
Browse files Browse the repository at this point in the history
When transpiling required files we need to be pretty liberal about what syntax we allow, otherwise we can end up in a situation where the current node version can actually parse more natively than babylon is allowed to by `@babel/preset-env`. This should fix that.

Fixes #102
  • Loading branch information
eventualbuddha committed Mar 20, 2018
1 parent f56ff4b commit e3f0341
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
28 changes: 28 additions & 0 deletions src/AllSyntaxPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as Babel from '@babel/core';
import { BabelOptions, ParseOptions } from './TransformRunner';

export const ALL_PLUGINS = [
'flow',
'jsx',
'asyncGenerators',
'classProperties',
'doExpressions',
'exportExtensions',
'functionBind',
'functionSent',
'objectRestSpread',
'dynamicImport',
'decorators'
];

export default function(babel: typeof Babel) {
return {
manipulateOptions(opts: BabelOptions, parserOpts: ParseOptions) {
for (let plugin of ALL_PLUGINS) {
if (plugin !== 'flow' || !opts.filename.endsWith('.ts')) {
parserOpts.plugins.push(plugin);
}
}
}
};
}
3 changes: 2 additions & 1 deletion src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { hasMagic as hasGlob, sync as globSync } from 'glob';
import { basename, extname, resolve } from 'path';
import { sync as resolveSync } from 'resolve';
import { install } from 'source-map-support';
import AllSyntaxPlugin from './AllSyntaxPlugin';
import { PathPredicate } from './iterateSources';
import PluginLoader from './PluginLoader';
import RecastPlugin from './RecastPlugin';
Expand Down Expand Up @@ -125,7 +126,7 @@ export default class Options {
}

async getBabelPlugins(): Promise<Array<BabelPlugin>> {
let result: Array<BabelPlugin> = [RecastPlugin];
let result: Array<BabelPlugin> = [AllSyntaxPlugin, RecastPlugin];

for (let plugin of await this.getPlugins()) {
let options =
Expand Down
17 changes: 3 additions & 14 deletions src/RecastPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import * as Babel from '@babel/core';
import { GeneratorOptions } from '@babel/generator';
import * as recast from 'recast';
import { ALL_PLUGINS } from './AllSyntaxPlugin';
import { AST, ParseOptions } from './TransformRunner';

const DEFAULT_OPTIONS = {
sourceType: 'module',
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
plugins: [
'flow',
'jsx',
'asyncGenerators',
'classProperties',
'doExpressions',
'exportExtensions',
'functionBind',
'functionSent',
'objectRestSpread',
'dynamicImport',
'decorators'
]
plugins: ALL_PLUGINS
};

export default function(babel: Babel) {
export default function(babel: typeof Babel) {
return {
parserOverride(
code: string,
Expand Down
8 changes: 7 additions & 1 deletion src/TransformRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ export class SourceTransformResult {
) {}
}

export type ParseOptions = object;
export interface BabelOptions {
filename: string;
}
export interface ParseOptions {
plugins: Array<string>;
}
export type AST = object;

export type RawBabelPlugin = (
babel: typeof Babel
) => {
name?: string;
visitor?: Visitor;
manipulateOptions?: (opts: object, parserOpts: ParseOptions) => void;
parserOverride?: (
code: string,
options: ParseOptions,
Expand Down
2 changes: 2 additions & 0 deletions src/transpile-requires.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { transform } from '@babel/core';
import { extname } from 'path';
import { addHook } from 'pirates';
import AllSyntaxPlugin from './AllSyntaxPlugin';

let useBabelrc = false;
let revert: (() => void) | null = null;
Expand All @@ -25,6 +26,7 @@ export function hook(code: string, filename: string): string {
filename,
babelrc: useBabelrc,
presets: [] as Array<string>,
plugins: [AllSyntaxPlugin],
sourceMaps: 'inline'
};

Expand Down

0 comments on commit e3f0341

Please sign in to comment.