WIP - add extractor, generate snippet_data

This commit is contained in:
Stefan Fejes
2019-08-20 15:52:05 +02:00
parent 88084d3d30
commit cc8f1d8a7a
37396 changed files with 4588842 additions and 133 deletions

View File

@ -0,0 +1,56 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const MODULES = [
// Local Promise implementation.
'Promise',
];
/**
* Automatically imports a module if its identifier is in the AST.
*/
module.exports = function autoImporter(babel) {
const t = babel.types;
function isAppropriateModule(name, scope, state) {
const autoImported = state.autoImported;
return MODULES.indexOf(name) !== -1
&& !autoImported.hasOwnProperty(name)
&& !scope.hasBinding(name, /*skip globals*/true);
}
return {
pre: function() {
// Cache per file to avoid calling `scope.hasBinding` several
// times for the same module, which has already been auto-imported.
this.autoImported = {};
},
visitor: {
ReferencedIdentifier: function(path) {
const node = path.node;
const scope = path.scope;
if (!isAppropriateModule(node.name, scope, this)) {
return;
}
scope.getProgramParent().push({
id: t.identifier(node.name),
init: t.callExpression(
t.identifier('require'),
[t.stringLiteral(node.name)]
),
});
this.autoImported[node.name] = true;
},
},
};
};

View File

@ -0,0 +1,45 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = function(babel) {
const t = babel.types;
// We can't construct an identifier with a type annotation all in 1 fell swoop
// so we have to create & mutate, then pass along.
const DEV_IDENTIFIER = t.identifier('__DEV__');
DEV_IDENTIFIER.typeAnnotation = t.typeAnnotation(t.booleanTypeAnnotation());
const DEV_DECLARATION = t.declareVariable(
DEV_IDENTIFIER
);
return {
pre() {
this.usesDEV = false;
},
visitor: {
Identifier: {
enter(path, file) {
this.usesDEV = this.usesDEV || path.isIdentifier({name: '__DEV__'});
},
},
Program: {
exit(path, file) {
if (!this.usesDEV) {
return;
}
// Add the declaration at the front of the body if we've used __DEV__.
path.node.body.unshift(DEV_DECLARATION);
},
},
},
};
};

View File

@ -0,0 +1,129 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = function(babel) {
var t = babel.types;
var SEEN_SYMBOL = Symbol();
var DEV_EXPRESSION = t.binaryExpression(
'!==',
t.memberExpression(
t.memberExpression(
t.identifier('process'),
t.identifier('env'),
false
),
t.identifier('NODE_ENV'),
false
),
t.stringLiteral('production')
);
return {
visitor: {
Identifier: {
enter: function(path) {
// Do nothing when testing
if (process.env.NODE_ENV === 'test') {
return;
}
// replace __DEV__ with process.env.NODE_ENV !== 'production'
if (path.isIdentifier({name: '__DEV__'})) {
path.replaceWith(DEV_EXPRESSION);
}
},
},
CallExpression: {
exit: function(path) {
var node = path.node;
// Do nothing when testing
if (process.env.NODE_ENV === 'test') {
return;
}
// Ignore if it's already been processed
if (node[SEEN_SYMBOL]) {
return;
}
if (path.get('callee').isIdentifier({name: 'invariant'})) {
// Turns this code:
//
// invariant(condition, argument, argument);
//
// into this:
//
// if (!condition) {
// if ("production" !== process.env.NODE_ENV) {
// invariant(false, argument, argument);
// } else {
// invariant(false);
// }
// }
//
// Specifically this does 2 things:
// 1. Checks the condition first, preventing an extra function call.
// 2. Adds an environment check so that verbose error messages aren't
// shipped to production.
// The generated code is longer than the original code but will dead
// code removal in a minifier will strip that out.
var condition = node.arguments[0];
var devInvariant = t.callExpression(
node.callee,
[t.booleanLiteral(false)].concat(node.arguments.slice(1))
);
devInvariant[SEEN_SYMBOL] = true;
var prodInvariant = t.callExpression(
node.callee,
[t.booleanLiteral(false)]
);
prodInvariant[SEEN_SYMBOL] = true;
path.replaceWith(t.ifStatement(
t.unaryExpression('!', condition),
t.blockStatement([
t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([
t.expressionStatement(devInvariant),
]),
t.blockStatement([
t.expressionStatement(prodInvariant),
])
),
])
));
} else if (path.get('callee').isIdentifier({name: 'warning'})) {
// Turns this code:
//
// warning(condition, argument, argument);
//
// into this:
//
// if ("production" !== process.env.NODE_ENV) {
// warning(condition, argument, argument);
// }
//
// The goal is to strip out warning calls entirely in production. We
// don't need the same optimizations for conditions that we use for
// invariant because we don't care about an extra call in __DEV__
node[SEEN_SYMBOL] = true;
path.replaceWith(t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([
t.expressionStatement(
node
),
])
));
}
},
},
},
};
};

View File

@ -0,0 +1,171 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
/**
* This transform inlines top-level require(...) aliases with to enable lazy
* loading of dependencies. It is able to inline both single references and
* child property references.
*
* For instance:
* var Foo = require('foo');
* f(Foo);
*
* Will be transformed into:
* f(require('foo'));
*
* When the assigment expression has a property access, it will be inlined too,
* keeping the property. For instance:
* var Bar = require('foo').bar;
* g(Bar);
*
* Will be transformed into:
* g(require('foo').bar);
*
* Destructuring also works the same way. For instance:
* const {Baz} = require('foo');
* h(Baz);
*
* Is also successfully inlined into:
* g(require('foo').Baz);
*/
module.exports = babel => {
return {
name: 'inline-requires',
visitor: {
Program: {
exit(path, state) {
var ignoredRequires = {};
var inlineableCalls = {require: true};
if (state.opts) {
if (state.opts.ignoredRequires) {
state.opts.ignoredRequires.forEach(function(name) {
ignoredRequires[name] = true;
});
}
if (state.opts.inlineableCalls) {
state.opts.inlineableCalls.forEach(function(name) {
inlineableCalls[name] = true;
});
}
}
path.scope.crawl();
path.traverse(
{CallExpression: call.bind(null, babel)},
{
ignoredRequires: ignoredRequires,
inlineableCalls: inlineableCalls,
}
);
},
},
},
};
};
function call(babel, path, state) {
var declaratorPath =
inlineableAlias(path, state) || inlineableMemberAlias(path, state);
var declarator = declaratorPath && declaratorPath.node;
if (declarator) {
var init = declarator.init;
var name = declarator.id && declarator.id.name;
var binding = declaratorPath.scope.getBinding(name);
var constantViolations = binding.constantViolations;
var thrown = false;
if (!constantViolations.length) {
deleteLocation(init);
babel.traverse(init, {
noScope: true,
enter: path => deleteLocation(path.node),
});
binding.referencePaths.forEach(ref => {
try {
ref.replaceWith(init);
} catch (err) {
thrown = true;
}
});
// If an error was thrown, it's most likely due to an invalid replacement
// happening (e.g. trying to replace a type annotation). It would usually
// be OK to ignore it, but to be safe, we will avoid removing the initial
// require.
if (!thrown) {
declaratorPath.remove();
}
}
}
}
function deleteLocation(node) {
delete node.start;
delete node.end;
delete node.loc;
}
function inlineableAlias(path, state) {
const isValid =
isInlineableCall(path.node, state) &&
path.parent.type === 'VariableDeclarator' &&
path.parent.id.type === 'Identifier' &&
path.parentPath.parent.type === 'VariableDeclaration' &&
path.parentPath.parentPath.parent.type === 'Program';
return isValid ? path.parentPath : null;
}
function inlineableMemberAlias(path, state) {
const isValid =
isInlineableCall(path.node, state) &&
path.parent.type === 'MemberExpression' &&
path.parentPath.parent.type === 'VariableDeclarator' &&
path.parentPath.parent.id.type === 'Identifier' &&
path.parentPath.parentPath.parent.type === 'VariableDeclaration' &&
path.parentPath.parentPath.parentPath.parent.type === 'Program';
return isValid ? path.parentPath.parentPath : null;
}
function isInlineableCall(node, state) {
const isInlineable =
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
state.inlineableCalls.hasOwnProperty(node.callee.name) &&
node['arguments'].length >= 1;
// require('foo');
const isStandardCall =
isInlineable &&
node['arguments'][0].type === 'StringLiteral' &&
!state.ignoredRequires.hasOwnProperty(node['arguments'][0].value);
// require(require.resolve('foo'));
const isRequireResolveCall =
isInlineable &&
node['arguments'][0].type === 'CallExpression' &&
node['arguments'][0].callee.type === 'MemberExpression' &&
node['arguments'][0].callee.object.type === 'Identifier' &&
state.inlineableCalls.hasOwnProperty(node['arguments'][0].callee.object.name) &&
node['arguments'][0].callee.property.type === 'Identifier' &&
node['arguments'][0].callee.property.name === 'resolve' &&
node['arguments'][0]['arguments'].length >= 1 &&
node['arguments'][0]['arguments'][0].type === 'StringLiteral' &&
!state.ignoredRequires.hasOwnProperty(node['arguments'][0]['arguments'][0].value);
return isStandardCall || isRequireResolveCall;
}

View File

@ -0,0 +1,50 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = function autoImporter(babel) {
const t = babel.types;
function getAssignIdent(path, file, state) {
if (!state.id) {
state.id = path.scope.generateUidIdentifier('assign');
path.scope.getProgramParent().push({
id: state.id,
init: t.callExpression(
t.identifier('require'),
[t.stringLiteral('object-assign')]
),
});
}
return state.id;
}
return {
pre: function() {
// map from module to generated identifier
this.id = null;
},
visitor: {
CallExpression: function(path, file) {
if (path.get('callee').matchesPattern('Object.assign')) {
// generate identifier and require if it hasn't been already
var id = getAssignIdent(path, file, this);
path.node.callee = id;
}
},
MemberExpression: function(path, file) {
if (path.matchesPattern('Object.assign')) {
var id = getAssignIdent(path, file, this);
path.replaceWith(id);
}
},
},
};
};

View File

@ -0,0 +1,150 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* Rewrites module string literals according to the `map` and `prefix` options.
* This allows other npm packages to be published and used directly without
* being a part of the same build.
*/
function mapModule(state, module) {
var moduleMap = state.opts.map || {};
if (moduleMap.hasOwnProperty(module)) {
return moduleMap[module];
}
// Jest understands the haste module system, so leave modules intact.
if (process.env.NODE_ENV !== 'test') {
var modulePrefix = state.opts.prefix;
if (modulePrefix == null) {
modulePrefix = './';
}
return modulePrefix + module;
}
return null;
}
var jestMethods = [
'dontMock',
'genMockFromModule',
'mock',
'setMock',
'unmock',
];
function isJestProperty(t, property) {
return t.isIdentifier(property) && jestMethods.indexOf(property.name) !== -1;
}
module.exports = function(babel) {
var t = babel.types;
/**
* Transforms `require('Foo')` and `require.requireActual('Foo')`.
*/
function transformRequireCall(path, state) {
var calleePath = path.get('callee');
if (
!t.isIdentifier(calleePath.node, {name: 'require'}) &&
!(
t.isMemberExpression(calleePath.node) &&
t.isIdentifier(calleePath.node.object, {name: 'require'}) &&
t.isIdentifier(calleePath.node.property, {name: 'requireActual'})
)
) {
return;
}
var args = path.get('arguments');
if (!args.length) {
return;
}
var moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
var module = mapModule(state, moduleArg.node.value);
if (module) {
moduleArg.replaceWith(t.stringLiteral(module));
}
}
}
/**
* Transforms `import type Bar from 'foo'`
*/
function transformTypeImport(path, state) {
var source = path.get('source');
if (source.type === 'StringLiteral') {
var module = mapModule(state, source.node.value);
if (module) {
source.replaceWith(t.stringLiteral(module));
}
}
}
/**
* Transforms either individual or chained calls to `jest.dontMock('Foo')`,
* `jest.mock('Foo')`, and `jest.genMockFromModule('Foo')`.
*/
function transformJestHelper(path, state) {
var calleePath = path.get('callee');
var args = path.get('arguments');
if (!args.length) {
return;
}
var moduleArg = args[0];
if (
moduleArg.node.type === 'StringLiteral' &&
calleePath.node &&
isJestProperty(t, calleePath.node.property)
) {
var module = mapModule(state, moduleArg.node.value);
if (module) {
moduleArg.replaceWith(t.stringLiteral(module));
}
}
}
const jestIdentifier = {
Identifier(path) {
if (path.node.name === 'jest') {
this.isJest = true;
}
},
};
function transformJestCall(path, state) {
let params = {isJest: false};
path.traverse(jestIdentifier, params);
if (params.isJest) {
transformJestHelper(path, state);
}
}
return {
visitor: {
CallExpression: {
exit(path, state) {
if (path.node.seen) {
return;
}
transformRequireCall(path, state);
transformJestCall(path, state);
path.node.seen = true;
},
},
ImportDeclaration: {
exit(path, state) {
let importKind = path.node.importKind;
if (importKind === 'type' || importKind === 'typeof') {
transformTypeImport(path, state);
}
}
}
},
};
};