Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a promise-returning *Async version of the transform and parse fns #8023

Merged
merged 2 commits into from May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/babel-core/package.json
Expand Up @@ -27,8 +27,7 @@
},
"browser": {
"./lib/config/files/index.js": "./lib/config/files/index-browser.js",
"./lib/transform-file.js": "./lib/transform-file-browser.js",
"./lib/transform-file-sync.js": "./lib/transform-file-sync-browser.js"
"./lib/transform-file.js": "./lib/transform-file-browser.js"
},
"dependencies": {
"@babel/code-frame": "7.0.0-beta.47",
Expand Down
7 changes: 0 additions & 7 deletions packages/babel-core/src/config/index.js
Expand Up @@ -17,10 +17,3 @@ export function loadOptions(opts: {}): Object | null {

return config ? config.options : null;
}

// For easier backward-compatibility, provide an API like the one we exposed in Babel 6.
export class OptionManager {
init(opts: {}) {
return loadOptions(opts);
}
}
44 changes: 27 additions & 17 deletions packages/babel-core/src/index.js
Expand Up @@ -13,26 +13,22 @@ export * as types from "@babel/types";
export { default as traverse } from "@babel/traverse";
export { default as template } from "@babel/template";

export { loadPartialConfig, loadOptions, OptionManager } from "./config";

export { createConfigItem } from "./config/item";

export function Plugin(alias: string) {
throw new Error(
`The (${alias}) Babel 5 plugin is being run with an unsupported Babel version.`,
);
}

export { default as transform } from "./transform";
export { default as transformSync } from "./transform-sync";

export { default as transformFile } from "./transform-file";
export { default as transformFileSync } from "./transform-file-sync";
export { loadPartialConfig, loadOptions } from "./config";

export { default as transformFromAst } from "./transform-ast";
export { default as transformFromAstSync } from "./transform-ast-sync";

export { default as parse } from "./parse";
export { transform, transformSync, transformAsync } from "./transform";
export {
transformFile,
transformFileSync,
transformFileAsync,
} from "./transform-file";
export {
transformFromAst,
transformFromAstSync,
transformFromAstAsync,
} from "./transform-ast";
export { parse, parseSync, parseAsync } from "./parse";

/**
* Recommended set of compilable extensions. Not used in @babel/core directly, but meant as
Expand All @@ -45,3 +41,17 @@ export const DEFAULT_EXTENSIONS = Object.freeze([
".es",
".mjs",
]);

// For easier backward-compatibility, provide an API like the one we exposed in Babel 6.
import { loadOptions } from "./config";
export class OptionManager {
init(opts: {}) {
return loadOptions(opts);
}
}

export function Plugin(alias: string) {
throw new Error(
`The (${alias}) Babel 5 plugin is being run with an unsupported Babel version.`,
);
}
72 changes: 67 additions & 5 deletions packages/babel-core/src/parse.js
Expand Up @@ -6,17 +6,79 @@ import normalizeOptions from "./transformation/normalize-opts";

type AstRoot = BabelNodeFile | BabelNodeProgram;

export default function parse(
export type ParseResult = AstRoot;

export type FileParseCallback = {
(Error, null): any,
(null, ParseResult | null): any,
};

type Parse = {
(code: string, callback: FileParseCallback): void,
(code: string, opts: ?InputOptions, callback: FileParseCallback): void,

// Here for backward-compatibility. Ideally use ".parseSync" if you want
// a synchronous API.
(code: string, opts: ?InputOptions): ParseResult | null,
};

export const parse: Parse = (function parse(code, opts, callback) {
if (typeof opts === "function") {
opts = undefined;
callback = opts;
}

// For backward-compat with Babel 7's early betas, we allow sync parsing when
// no callback is given. Will be dropped in some future Babel major version.
if (callback === undefined) return parseSync(code, opts);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: If we wanted to introduce a warning, would it be a breaking change? Or we could do it in a minor/patch version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly added this to make lives easier for people, I actually thought about removing it in this PR, but since it's so trivial leaving it to 8.x isn't really that much of a problem.


const config = loadConfig(opts);

if (config === null) {
return null;
}

// Reassign to keep Flowtype happy.
const cb = callback;

// Just delaying the transform one tick for now to simulate async behavior
// but more async logic may land here eventually.
process.nextTick(() => {
let ast = null;
try {
const cfg = loadConfig(opts);
if (cfg === null) return cb(null, null);

ast = normalizeFile(cfg.passes, normalizeOptions(cfg), code).ast;
} catch (err) {
return cb(err);
}

cb(null, ast);
});
}: Function);

export function parseSync(
code: string,
opts: InputOptions,
): AstRoot | null {
opts?: InputOptions,
): ParseResult | null {
const config = loadConfig(opts);

if (config === null) {
return null;
}

const file = normalizeFile(config.passes, normalizeOptions(config), code);
return normalizeFile(config.passes, normalizeOptions(config), code).ast;
}

return file.ast;
export function parseAsync(
code: string,
opts?: InputOptions,
): Promise<ParseResult | null> {
return new Promise((res, rej) => {
parse(code, opts, (err, result) => {
if (err == null) res(result);
else rej(err);
});
});
}
18 changes: 0 additions & 18 deletions packages/babel-core/src/transform-ast-sync.js

This file was deleted.

42 changes: 36 additions & 6 deletions packages/babel-core/src/transform-ast.js
Expand Up @@ -2,16 +2,15 @@

import loadConfig, { type InputOptions } from "./config";
import {
runSync,
runAsync,
type FileResult,
type FileResultCallback,
} from "./transformation";

import transformAstSync from "./transform-ast-sync";

type AstRoot = BabelNodeFile | BabelNodeProgram;

type TransformAst = {
type TransformFromAst = {
(ast: AstRoot, code: string, callback: FileResultCallback): void,
(
ast: AstRoot,
Expand All @@ -25,15 +24,20 @@ type TransformAst = {
(ast: AstRoot, code: string, opts: ?InputOptions): FileResult | null,
};

export default ((function transformFromAst(ast, code, opts, callback) {
export const transformFromAst: TransformFromAst = (function transformFromAst(
ast,
code,
opts,
callback,
) {
if (typeof opts === "function") {
opts = undefined;
callback = opts;
}

// For backward-compat with Babel 6, we allow sync transformation when
// no callback is given. Will be dropped in some future Babel major version.
if (callback === undefined) return transformAstSync(ast, code, opts);
if (callback === undefined) return transformFromAstSync(ast, code, opts);

// Reassign to keep Flowtype happy.
const cb = callback;
Expand All @@ -53,4 +57,30 @@ export default ((function transformFromAst(ast, code, opts, callback) {

runAsync(cfg, code, ast, cb);
});
}: Function): TransformAst);
}: Function);

export function transformFromAstSync(
ast: AstRoot,
code: string,
opts: ?InputOptions,
): FileResult | null {
const config = loadConfig(opts);
if (config === null) return null;

if (!ast) throw new Error("No AST given");

return runSync(config, code, ast);
}

export function transformFromAstAsync(
ast: AstRoot,
code: string,
opts: ?InputOptions,
): Promise<FileResult | null> {
return new Promise((res, rej) => {
transformFromAst(ast, code, opts, (err, result) => {
if (err == null) res(result);
else rej(err);
});
});
}
25 changes: 0 additions & 25 deletions packages/babel-core/src/transform-file-sync.js

This file was deleted.

47 changes: 44 additions & 3 deletions packages/babel-core/src/transform-file.js
Expand Up @@ -2,14 +2,23 @@
import fs from "fs";

import loadConfig, { type InputOptions } from "./config";
import { runAsync, type FileResultCallback } from "./transformation";
import {
runSync,
runAsync,
type FileResult,
type FileResultCallback,
} from "./transformation";

type TransformFile = {
(filename: string, callback: FileResultCallback): void,
(filename: string, opts: ?InputOptions, callback: FileResultCallback): void,
};

export default ((function transformFile(filename, opts, callback) {
export const transformFile: TransformFile = (function transformFile(
filename,
opts,
callback,
) {
let options;
if (typeof opts === "function") {
callback = opts;
Expand Down Expand Up @@ -43,4 +52,36 @@ export default ((function transformFile(filename, opts, callback) {
runAsync(config, code, null, callback);
});
});
}: Function): TransformFile);
}: Function);

export function transformFileSync(
filename: string,
opts: ?InputOptions,
): FileResult | null {
let options;
if (opts == null) {
options = { filename };
} else if (opts && typeof opts === "object") {
options = {
...opts,
filename,
};
}

const config = loadConfig(options);
if (config === null) return null;

return runSync(config, fs.readFileSync(filename, "utf8"));
}

export function transformFileAsync(
filename: string,
opts: ?InputOptions,
): Promise<FileResult | null> {
return new Promise((res, rej) => {
transformFile(filename, opts, (err, result) => {
if (err == null) res(result);
else rej(err);
});
});
}
13 changes: 0 additions & 13 deletions packages/babel-core/src/transform-sync.js

This file was deleted.