Skip to content

Commit

Permalink
fix: allow loading plugins with additional JavaScript syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
eventualbuddha committed Jan 30, 2019
1 parent a42235a commit 9deb26d
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 2,122 deletions.
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"@babel/core": "^7.1.6",
"@babel/generator": "^7.1.6",
"@babel/parser": "^7.1.6",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/polyfill": "^7.2.5",
"@babel/preset-env": "^7.1.6",
"@babel/preset-typescript": "^7.1.0",
"@babel/traverse": "^7.1.6",
Expand Down
20 changes: 15 additions & 5 deletions packages/cli/src/transpile-requires.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformSync, TransformOptions } from '@babel/core';
import { transformSync, TransformOptions, PluginItem } from '@babel/core';
import { extname } from 'path';
import { addHook } from 'pirates';
import buildAllSyntaxPlugin from './AllSyntaxPlugin';
Expand All @@ -14,15 +14,21 @@ export function hook(code: string, filename: string): string {
throw new Error(`cannot load file type '${ext}': ${filename}`);
}

let presets: Array<string> = [];
let plugins: Array<PluginItem> = [];
let presets: Array<PluginItem> = [];
let options: TransformOptions = {
filename,
babelrc: useBabelConfig,
presets: presets,
plugins: [buildAllSyntaxPlugin('module')],
presets,
plugins,
sourceMaps: 'inline'
};

plugins.push(
buildAllSyntaxPlugin('module'),
require.resolve('@babel/plugin-proposal-class-properties')
);

if (!useBabelConfig) {
options.configFile = useBabelConfig;
}
Expand All @@ -32,7 +38,10 @@ export function hook(code: string, filename: string): string {
presets.push(require.resolve('@babel/preset-typescript'));
}

presets.push(require.resolve('@babel/preset-env'));
presets.push([
require.resolve('@babel/preset-env'),
{ useBuiltIns: 'entry' }
]);
}

let result = transformSync(code, options);
Expand All @@ -47,6 +56,7 @@ export function hook(code: string, filename: string): string {
export function enable(shouldUseBabelConfig: boolean = false): void {
disable();
useBabelConfig = shouldUseBabelConfig;
require('@babel/polyfill');
revert = addHook(hook, {
exts: Array.from(PluginExtensions),
ignoreNodeModules: true
Expand Down
32 changes: 32 additions & 0 deletions packages/cli/test/cli/CLITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,4 +629,36 @@ describe('CLI', function() {
}
);
});

it('can load a plugin that uses class properties', async function() {
let { status, stdout, stderr } = await runCodemodCLI(
['--plugin', plugin('class-properties', '.ts'), '--stdio'],
''
);

deepEqual(
{ status, stdout, stderr },
{
status: 0,
stdout: '',
stderr: ''
}
);
});

it('can load a plugin that uses generators', async function() {
let { status, stdout, stderr } = await runCodemodCLI(
['--plugin', plugin('generators', '.ts'), '--stdio'],
''
);

deepEqual(
{ status, stdout, stderr },
{
status: 0,
stdout: '',
stderr: ''
}
);
});
});
22 changes: 22 additions & 0 deletions packages/cli/test/fixtures/plugin/class-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { PluginObj } from '@babel/core';

class Count {
// This file exists to verify that class properties like ↓ can be loaded.
count: number = 0;

incr(): void {
this.count++;
}
}

export default function(): PluginObj {
const debuggerCount = new Count();

return {
visitor: {
DebuggerStatement(): void {
debuggerCount.incr();
}
}
};
}
22 changes: 22 additions & 0 deletions packages/cli/test/fixtures/plugin/generators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { PluginObj } from '@babel/core';

// This file exists to verify that generator functions like ↓ can be loaded.
function* counter(): IterableIterator<number> {
let i = 0;

while (true) {
yield i++;
}
}

export default function(): PluginObj {
const identifiers = counter();

return {
visitor: {
Identifier(): void {
console.log(identifiers.next());
}
}
};
}
Loading

0 comments on commit 9deb26d

Please sign in to comment.