add flow type annotations
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
/* @flow */
|
||||
|
||||
/* eslint no-new-func: 0 */
|
||||
|
||||
import { transform } from "./node";
|
||||
export * from "./node";
|
||||
|
||||
export function run(code, opts = {}) {
|
||||
export function run(code: string, opts: Object = {}): any {
|
||||
return new Function(transform(code, opts).code)();
|
||||
}
|
||||
|
||||
export function load(url, callback, opts = {}, hold) {
|
||||
export function load(url: string, callback: Function, opts: Object = {}, hold?: boolean) {
|
||||
opts.filename = opts.filename || url;
|
||||
|
||||
let xhr = global.ActiveXObject ? new global.ActiveXObject("Microsoft.XMLHTTP") : new global.XMLHttpRequest();
|
||||
@@ -31,7 +33,7 @@ export function load(url, callback, opts = {}, hold) {
|
||||
}
|
||||
|
||||
function runScripts() {
|
||||
let scripts = [];
|
||||
let scripts: Array<Array<any> | Object> = [];
|
||||
let types = ["text/ecmascript-6", "text/6to5", "text/babel", "module"];
|
||||
let index = 0;
|
||||
|
||||
@@ -42,7 +44,7 @@ function runScripts() {
|
||||
function exec() {
|
||||
let param = scripts[index];
|
||||
if (param instanceof Array) {
|
||||
run(param);
|
||||
run(param, index);
|
||||
index++;
|
||||
exec();
|
||||
}
|
||||
@@ -52,7 +54,7 @@ function runScripts() {
|
||||
* Load, transform, and execute all scripts.
|
||||
*/
|
||||
|
||||
function run(script, i) {
|
||||
function run(script: Object, i: number) {
|
||||
let opts = {};
|
||||
|
||||
if (script.src) {
|
||||
@@ -75,7 +77,7 @@ function runScripts() {
|
||||
if (types.indexOf(_script.type) >= 0) scripts.push(_script);
|
||||
}
|
||||
|
||||
for (i in scripts) {
|
||||
for (let i = 0; i < scripts.length; i++) {
|
||||
run(scripts[i], i);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* @flow */
|
||||
|
||||
import isFunction from "lodash/lang/isFunction";
|
||||
import transform from "../transformation";
|
||||
import * as babylon from "babylon";
|
||||
@@ -16,6 +18,9 @@ export { default as buildExternalHelpers } from "../tools/build-external-helpers
|
||||
export { default as template } from "babel-template";
|
||||
export { version } from "../../package";
|
||||
|
||||
import * as messages from "babel-messages";
|
||||
export { messages };
|
||||
|
||||
import * as t from "babel-types";
|
||||
export { t as types };
|
||||
|
||||
@@ -48,7 +53,7 @@ export function transformFile(filename: string, opts?: Object, callback: Functio
|
||||
});
|
||||
}
|
||||
|
||||
export function transformFileSync(filename: string, opts?: Object = {}) {
|
||||
export function transformFileSync(filename: string, opts?: Object = {}): string {
|
||||
opts.filename = filename;
|
||||
return transform(fs.readFileSync(filename, "utf8"), opts);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* @flow */
|
||||
|
||||
import sourceMapSupport from "source-map-support";
|
||||
import * as registerCache from "./cache";
|
||||
import OptionManager from "../../transformation/file/options/option-manager";
|
||||
@@ -52,23 +54,23 @@ let cwd = process.cwd();
|
||||
* Get path from `filename` relative to the current working directory.
|
||||
*/
|
||||
|
||||
let getRelativePath = function (filename){
|
||||
function getRelativePath(filename){
|
||||
return path.relative(cwd, filename);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last modified time for a `filename`.
|
||||
*/
|
||||
|
||||
let mtime = function (filename) {
|
||||
function mtime(filename) {
|
||||
return +fs.statSync(filename).mtime;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile a `filename` with optional `opts`.
|
||||
*/
|
||||
|
||||
let compile = function (filename, opts = {}) {
|
||||
function compile(filename, opts = {}) {
|
||||
let result;
|
||||
|
||||
opts.filename = filename;
|
||||
@@ -97,26 +99,26 @@ let compile = function (filename, opts = {}) {
|
||||
}
|
||||
|
||||
if (cache) {
|
||||
result.mtime = mtime(filename);
|
||||
cache[cacheKey] = result;
|
||||
result.mtime = mtime(filename);
|
||||
}
|
||||
|
||||
maps[filename] = result.map;
|
||||
|
||||
return result.code;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a `filename` should be ignored by Babel.
|
||||
*/
|
||||
|
||||
let shouldIgnore = function (filename) {
|
||||
function shouldIgnore(filename) {
|
||||
if (!ignore && !only) {
|
||||
return getRelativePath(filename).split(path.sep).indexOf("node_modules") >= 0;
|
||||
} else {
|
||||
return util.shouldIgnore(filename, ignore || [], only);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Monkey patch istanbul if it is running so that it works properly.
|
||||
@@ -147,24 +149,24 @@ if (process.env.running_under_istanbul) {
|
||||
* Replacement for the loader for istanbul.
|
||||
*/
|
||||
|
||||
let istanbulLoader = function (m, filename, old) {
|
||||
function istanbulLoader(m, filename, old) {
|
||||
istanbulMonkey[filename] = true;
|
||||
old(m, filename);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Default loader.
|
||||
*/
|
||||
|
||||
let normalLoader = function (m, filename) {
|
||||
function normalLoader(m, filename) {
|
||||
m._compile(compile(filename), filename);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a loader for an extension.
|
||||
*/
|
||||
|
||||
let registerExtension = function (ext) {
|
||||
function registerExtension(ext) {
|
||||
let old = oldHandlers[ext] || oldHandlers[".js"] || require.extensions[".js"];
|
||||
|
||||
let loader = normalLoader;
|
||||
@@ -177,13 +179,13 @@ let registerExtension = function (ext) {
|
||||
loader(m, filename, old);
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Register loader for given extensions.
|
||||
*/
|
||||
|
||||
let hookExtensions = function (_exts) {
|
||||
function hookExtensions(_exts) {
|
||||
each(oldHandlers, function (old, ext) {
|
||||
if (old === undefined) {
|
||||
delete require.extensions[ext];
|
||||
@@ -198,7 +200,7 @@ let hookExtensions = function (_exts) {
|
||||
oldHandlers[ext] = require.extensions[ext];
|
||||
registerExtension(ext);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Register loader for default extensions.
|
||||
@@ -210,7 +212,7 @@ hookExtensions(util.canCompile.EXTENSIONS);
|
||||
* Update options at runtime.
|
||||
*/
|
||||
|
||||
export default function (opts = {}) {
|
||||
export default function (opts?: Object = {}) {
|
||||
if (opts.only != null) only = util.arrayify(opts.only, util.regexify);
|
||||
if (opts.ignore != null) ignore = util.arrayify(opts.ignore, util.regexify);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user