Skip to content

Commit 672b81b

Browse files
chore(types): use typings for babel packages as much as possible
Also upgrades most development dependencies.
1 parent 751790e commit 672b81b

10 files changed

+263
-233
lines changed

package.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,36 @@
3232
"author": "Brian Donovan <[email protected]>",
3333
"license": "Apache-2.0",
3434
"devDependencies": {
35-
"@commitlint/cli": "^7.0.0",
36-
"@commitlint/config-conventional": "^7.0.1",
37-
"@types/babel-traverse": "^6.25.3",
38-
"@types/babylon": "^6.16.2",
35+
"@commitlint/cli": "^7.2.1",
36+
"@commitlint/config-conventional": "^7.1.2",
37+
"@types/babel__core": "^7.0.2",
38+
"@types/babel__generator": "^7.0.1",
39+
"@types/babel__traverse": "^7.0.1",
3940
"@types/get-port": "^4.0.0",
4041
"@types/get-stream": "^3.0.1",
4142
"@types/glob": "^7.1.0",
4243
"@types/got": "^8.3.1",
4344
"@types/make-dir": "^1.0.3",
4445
"@types/mocha": "^5.2.1",
4546
"@types/mz": "0.0.32",
46-
"@types/node": "^10.3.0",
47-
"@types/prettier": "^1.12.4",
47+
"@types/node": "^10.12.9",
48+
"@types/prettier": "^1.15.1",
4849
"@types/rimraf": "2.0.2",
4950
"@types/semver": "^5.5.0",
5051
"@types/source-map-support": "^0.4.1",
5152
"@types/tmp": "^0.0.33",
52-
"commitlint": "^7.0.0",
53+
"commitlint": "^7.2.1",
5354
"get-port": "^4.0.0",
54-
"husky": "^1.0.0",
55+
"husky": "^1.1.4",
5556
"lint-staged": "^8.0.5",
5657
"make-dir": "^1.3.0",
5758
"mocha": "^5.2.0",
5859
"prettier-check": "^2.0.0",
5960
"rimraf": "2.6.2",
60-
"semver": "^5.5.0",
61-
"tslint": "^5.10.0",
62-
"tslint-config-prettier": "^1.13.0",
63-
"typescript": "^2.9.1",
61+
"semver": "^5.6.0",
62+
"tslint": "^5.11.0",
63+
"tslint-config-prettier": "^1.16.0",
64+
"typescript": "^3.1.6",
6465
"yarnhook": "^0.3.0"
6566
},
6667
"dependencies": {

src/AllSyntaxPlugin.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as Babel from '@babel/core';
2+
import { ParserPlugin } from '@babel/parser';
23
import { extname } from 'path';
3-
import { BabelOptions, ParseOptions } from './BabelPluginTypes';
4+
import { PluginObj } from './BabelPluginTypes';
45
import { TypeScriptExtensions } from './extensions';
56

6-
const BASIC_PLUGINS: Array<string | [string, object]> = [
7+
const BASIC_PLUGINS: Array<ParserPlugin | [ParserPlugin, object]> = [
78
'jsx',
89
'asyncGenerators',
910
'classProperties',
1011
'doExpressions',
11-
'exportExtensions',
1212
'functionBind',
1313
'functionSent',
1414
'objectRestSpread',
@@ -18,25 +18,29 @@ const BASIC_PLUGINS: Array<string | [string, object]> = [
1818

1919
function pluginsForFilename(
2020
filename: string
21-
): Array<string | [string, object]> {
21+
): Array<ParserPlugin | [ParserPlugin, object]> {
2222
let isTypeScript = TypeScriptExtensions.has(extname(filename));
2323

2424
return isTypeScript
2525
? [...BASIC_PLUGINS, 'typescript']
2626
: [...BASIC_PLUGINS, 'flow'];
2727
}
2828

29-
export default function(babel: typeof Babel) {
29+
export default function(babel: typeof Babel): PluginObj {
3030
return {
31-
manipulateOptions(opts: BabelOptions, parserOpts: ParseOptions) {
31+
manipulateOptions(
32+
opts: Babel.TransformOptions,
33+
parserOpts: Babel.ParserOptions
34+
): void {
3235
parserOpts.sourceType = 'module';
3336
parserOpts.allowImportExportEverywhere = true;
3437
parserOpts.allowReturnOutsideFunction = true;
3538
parserOpts.allowSuperOutsideMethod = true;
39+
// Cast this because @babel/types typings don't allow plugin options.
3640
parserOpts.plugins = [
3741
...(parserOpts.plugins || []),
38-
...pluginsForFilename(opts.filename)
39-
];
42+
...pluginsForFilename(opts.filename as string)
43+
] as Array<ParserPlugin>;
4044
}
4145
};
4246
}

src/BabelPluginTypes.ts

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,25 @@
11
import * as Babel from '@babel/core';
2-
import { GeneratorOptions } from '@babel/generator';
3-
import { Visitor } from '@babel/traverse';
2+
import { File } from '@babel/types';
43

5-
export interface BabelOptions {
6-
filename: string;
7-
}
8-
export interface ParseOptions {
9-
sourceType?: 'module' | 'script';
10-
allowImportExportEverywhere?: boolean;
11-
allowReturnOutsideFunction?: boolean;
12-
allowSuperOutsideMethod?: boolean;
13-
plugins?: Array<string | [string, object]>;
14-
tokens?: boolean;
15-
}
16-
export type AST = object;
17-
18-
export type RawBabelPlugin = (
19-
babel: typeof Babel
20-
) => {
21-
name?: string;
22-
visitor?: Visitor;
23-
manipulateOptions?: (opts: object, parserOpts: ParseOptions) => void;
24-
parserOverride?: (
4+
/**
5+
* Fixes the `PluginObj` type from `@babel/core` by making all fields optional
6+
* and adding parser and generator override methods.
7+
*/
8+
export interface PluginObj<S = File> extends Partial<Babel.PluginObj<S>> {
9+
parserOverride?(
2510
code: string,
26-
options: ParseOptions,
27-
parse: (code: string, options: ParseOptions) => AST
28-
) => AST;
29-
generatorOverride?: (
30-
ast: AST,
31-
options: GeneratorOptions,
11+
options: Babel.ParserOptions,
12+
parse: (code: string, options: Babel.ParserOptions) => File
13+
): File;
14+
15+
generatorOverride?(
16+
ast: File,
17+
options: Babel.GeneratorOptions,
3218
code: string,
33-
generate: (ast: AST, options: GeneratorOptions) => string
34-
) => { code: string; map?: object };
35-
};
19+
generate: (ast: File, options: Babel.GeneratorOptions) => string
20+
): { code: string; map?: object };
21+
}
22+
23+
export type RawBabelPlugin = (babel: typeof Babel) => PluginObj;
3624
export type RawBabelPluginWithOptions = [RawBabelPlugin, object];
3725
export type BabelPlugin = RawBabelPlugin | RawBabelPluginWithOptions;

src/InlineTransformer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
import { transform } from '@babel/core';
1+
import { transformAsync } from '@babel/core';
22
import { BabelPlugin } from './BabelPluginTypes';
33
import Transformer from './Transformer';
44

55
export default class InlineTransformer implements Transformer {
66
constructor(private readonly plugins: Array<BabelPlugin>) {}
77

88
async transform(filepath: string, content: string): Promise<string> {
9-
return transform(content, {
9+
let result = await transformAsync(content, {
1010
filename: filepath,
1111
babelrc: false,
1212
plugins: this.plugins
13-
} as {}).code as string;
13+
});
14+
15+
if (!result) {
16+
throw new Error(`[${filepath}] babel transform returned null`);
17+
}
18+
19+
return result.code as string;
1420
}
1521
}

src/PrettierPrinterPlugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as Babel from '@babel/core';
22
import { GeneratorOptions } from '@babel/generator';
3+
import { File } from '@babel/types';
34
import * as Prettier from 'prettier';
45
import { sync as resolveSync } from 'resolve';
5-
import { AST } from './BabelPluginTypes';
66
import { generate, parse } from './RecastPlugin';
77

88
function loadPrettier(): typeof Prettier {
@@ -26,15 +26,15 @@ export default function(babel: typeof Babel) {
2626
return {
2727
parserOverride: parse,
2828
generatorOverride(
29-
ast: AST,
29+
ast: File,
3030
options: GeneratorOptions,
3131
code: string,
32-
_generate: (ast: AST, options: GeneratorOptions) => string
32+
_generate: (ast: File, options: GeneratorOptions) => string
3333
): { code: string; map?: object } {
3434
return {
3535
code: prettier.format(
3636
generate(ast, options, code, _generate).code,
37-
resolvePrettierConfig(options.filename)
37+
resolvePrettierConfig(options.filename as string)
3838
)
3939
};
4040
}

src/RecastPlugin.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as Babel from '@babel/core';
2-
import { GeneratorOptions } from '@babel/generator';
2+
import { File } from '@babel/types';
33
import * as recast from 'recast';
4-
import { AST, ParseOptions } from './BabelPluginTypes';
4+
import { PluginObj } from './BabelPluginTypes';
55

66
export function parse(
77
code: string,
8-
options: ParseOptions,
9-
parse: (code: string, options: ParseOptions) => AST
10-
): AST {
8+
options: Babel.ParserOptions,
9+
parse: (code: string, options: Babel.ParserOptions) => File
10+
): File {
1111
return recast.parse(code, {
1212
parser: {
1313
parse(code: string) {
@@ -18,15 +18,15 @@ export function parse(
1818
}
1919

2020
export function generate(
21-
ast: AST,
22-
options: GeneratorOptions,
21+
ast: File,
22+
options: Babel.GeneratorOptions,
2323
code: string,
24-
generate: (ast: AST, options: GeneratorOptions) => string
24+
generate: (ast: File, options: Babel.GeneratorOptions) => string
2525
): { code: string; map?: object } {
2626
return recast.print(ast);
2727
}
2828

29-
export default function(babel: typeof Babel) {
29+
export default function(babel: typeof Babel): PluginObj {
3030
return {
3131
parserOverride: parse,
3232
generatorOverride: generate

src/transpile-requires.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { transform } from '@babel/core';
1+
import { transformSync, TransformOptions } from '@babel/core';
22
import { extname } from 'path';
33
import { addHook } from 'pirates';
44
import AllSyntaxPlugin from './AllSyntaxPlugin';
@@ -14,23 +14,30 @@ export function hook(code: string, filename: string): string {
1414
throw new Error(`cannot load file type '${ext}': ${filename}`);
1515
}
1616

17-
let options = {
17+
let presets: Array<string> = [];
18+
let options: TransformOptions = {
1819
filename,
1920
babelrc: useBabelrc,
20-
presets: [] as Array<string>,
21+
presets: presets,
2122
plugins: [AllSyntaxPlugin],
2223
sourceMaps: 'inline'
2324
};
2425

2526
if (!useBabelrc) {
2627
if (TypeScriptExtensions.has(ext)) {
27-
options.presets.push(require.resolve('@babel/preset-typescript'));
28+
presets.push(require.resolve('@babel/preset-typescript'));
2829
}
2930

30-
options.presets.push(require.resolve('@babel/preset-env'));
31+
presets.push(require.resolve('@babel/preset-env'));
3132
}
3233

33-
return transform(code, options).code as string;
34+
let result = transformSync(code, options);
35+
36+
if (!result) {
37+
throw new Error(`[${filename}] babel transform returned null`);
38+
}
39+
40+
return result.code as string;
3441
}
3542

3643
export function enable(babelrc: boolean = false) {

0 commit comments

Comments
 (0)