update
This commit is contained in:
+65
-40
@@ -5,22 +5,30 @@ Object.defineProperty(exports, "__esModule", {
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _assert = _interopRequireDefault(require("assert"));
|
||||
var _assert = require("assert");
|
||||
|
||||
var t = _interopRequireWildcard(require("@babel/types"));
|
||||
var _t = require("@babel/types");
|
||||
|
||||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
||||
|
||||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
const {
|
||||
callExpression,
|
||||
cloneNode,
|
||||
expressionStatement,
|
||||
identifier,
|
||||
importDeclaration,
|
||||
importDefaultSpecifier,
|
||||
importNamespaceSpecifier,
|
||||
importSpecifier,
|
||||
memberExpression,
|
||||
stringLiteral,
|
||||
variableDeclaration,
|
||||
variableDeclarator
|
||||
} = _t;
|
||||
|
||||
class ImportBuilder {
|
||||
constructor(importedSource, scope, hub) {
|
||||
this._statements = [];
|
||||
this._resultName = null;
|
||||
this._scope = null;
|
||||
this._hub = null;
|
||||
this._importedSource = void 0;
|
||||
this._scope = scope;
|
||||
this._hub = hub;
|
||||
this._importedSource = importedSource;
|
||||
@@ -34,61 +42,76 @@ class ImportBuilder {
|
||||
}
|
||||
|
||||
import() {
|
||||
this._statements.push(t.importDeclaration([], t.stringLiteral(this._importedSource)));
|
||||
this._statements.push(importDeclaration([], stringLiteral(this._importedSource)));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
require() {
|
||||
this._statements.push(t.expressionStatement(t.callExpression(t.identifier("require"), [t.stringLiteral(this._importedSource)])));
|
||||
this._statements.push(expressionStatement(callExpression(identifier("require"), [stringLiteral(this._importedSource)])));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
namespace(name = "namespace") {
|
||||
name = this._scope.generateUidIdentifier(name);
|
||||
const local = this._scope.generateUidIdentifier(name);
|
||||
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
(0, _assert.default)(statement.type === "ImportDeclaration");
|
||||
(0, _assert.default)(statement.specifiers.length === 0);
|
||||
statement.specifiers = [t.importNamespaceSpecifier(name)];
|
||||
this._resultName = t.cloneNode(name);
|
||||
|
||||
_assert(statement.type === "ImportDeclaration");
|
||||
|
||||
_assert(statement.specifiers.length === 0);
|
||||
|
||||
statement.specifiers = [importNamespaceSpecifier(local)];
|
||||
this._resultName = cloneNode(local);
|
||||
return this;
|
||||
}
|
||||
|
||||
default(name) {
|
||||
name = this._scope.generateUidIdentifier(name);
|
||||
const id = this._scope.generateUidIdentifier(name);
|
||||
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
(0, _assert.default)(statement.type === "ImportDeclaration");
|
||||
(0, _assert.default)(statement.specifiers.length === 0);
|
||||
statement.specifiers = [t.importDefaultSpecifier(name)];
|
||||
this._resultName = t.cloneNode(name);
|
||||
|
||||
_assert(statement.type === "ImportDeclaration");
|
||||
|
||||
_assert(statement.specifiers.length === 0);
|
||||
|
||||
statement.specifiers = [importDefaultSpecifier(id)];
|
||||
this._resultName = cloneNode(id);
|
||||
return this;
|
||||
}
|
||||
|
||||
named(name, importName) {
|
||||
if (importName === "default") return this.default(name);
|
||||
name = this._scope.generateUidIdentifier(name);
|
||||
|
||||
const id = this._scope.generateUidIdentifier(name);
|
||||
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
(0, _assert.default)(statement.type === "ImportDeclaration");
|
||||
(0, _assert.default)(statement.specifiers.length === 0);
|
||||
statement.specifiers = [t.importSpecifier(name, t.identifier(importName))];
|
||||
this._resultName = t.cloneNode(name);
|
||||
|
||||
_assert(statement.type === "ImportDeclaration");
|
||||
|
||||
_assert(statement.specifiers.length === 0);
|
||||
|
||||
statement.specifiers = [importSpecifier(id, identifier(importName))];
|
||||
this._resultName = cloneNode(id);
|
||||
return this;
|
||||
}
|
||||
|
||||
var(name) {
|
||||
name = this._scope.generateUidIdentifier(name);
|
||||
const id = this._scope.generateUidIdentifier(name);
|
||||
|
||||
let statement = this._statements[this._statements.length - 1];
|
||||
|
||||
if (statement.type !== "ExpressionStatement") {
|
||||
(0, _assert.default)(this._resultName);
|
||||
statement = t.expressionStatement(this._resultName);
|
||||
_assert(this._resultName);
|
||||
|
||||
statement = expressionStatement(this._resultName);
|
||||
|
||||
this._statements.push(statement);
|
||||
}
|
||||
|
||||
this._statements[this._statements.length - 1] = t.variableDeclaration("var", [t.variableDeclarator(name, statement.expression)]);
|
||||
this._resultName = t.cloneNode(name);
|
||||
this._statements[this._statements.length - 1] = variableDeclaration("var", [variableDeclarator(id, statement.expression)]);
|
||||
this._resultName = cloneNode(id);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -104,12 +127,13 @@ class ImportBuilder {
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
|
||||
if (statement.type === "ExpressionStatement") {
|
||||
statement.expression = t.callExpression(callee, [statement.expression]);
|
||||
statement.expression = callExpression(callee, [statement.expression]);
|
||||
} else if (statement.type === "VariableDeclaration") {
|
||||
(0, _assert.default)(statement.declarations.length === 1);
|
||||
statement.declarations[0].init = t.callExpression(callee, [statement.declarations[0].init]);
|
||||
_assert(statement.declarations.length === 1);
|
||||
|
||||
statement.declarations[0].init = callExpression(callee, [statement.declarations[0].init]);
|
||||
} else {
|
||||
_assert.default.fail("Unexpected type.");
|
||||
_assert.fail("Unexpected type.");
|
||||
}
|
||||
|
||||
return this;
|
||||
@@ -119,19 +143,20 @@ class ImportBuilder {
|
||||
const statement = this._statements[this._statements.length - 1];
|
||||
|
||||
if (statement.type === "ExpressionStatement") {
|
||||
statement.expression = t.memberExpression(statement.expression, t.identifier(name));
|
||||
statement.expression = memberExpression(statement.expression, identifier(name));
|
||||
} else if (statement.type === "VariableDeclaration") {
|
||||
(0, _assert.default)(statement.declarations.length === 1);
|
||||
statement.declarations[0].init = t.memberExpression(statement.declarations[0].init, t.identifier(name));
|
||||
_assert(statement.declarations.length === 1);
|
||||
|
||||
statement.declarations[0].init = memberExpression(statement.declarations[0].init, identifier(name));
|
||||
} else {
|
||||
_assert.default.fail("Unexpected type:" + statement.type);
|
||||
_assert.fail("Unexpected type:" + statement.type);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
read(name) {
|
||||
this._resultName = t.memberExpression(this._resultName, t.identifier(name));
|
||||
this._resultName = memberExpression(this._resultName, identifier(name));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+53
-44
@@ -5,19 +5,18 @@ Object.defineProperty(exports, "__esModule", {
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _assert = _interopRequireDefault(require("assert"));
|
||||
var _assert = require("assert");
|
||||
|
||||
var t = _interopRequireWildcard(require("@babel/types"));
|
||||
var _t = require("@babel/types");
|
||||
|
||||
var _importBuilder = _interopRequireDefault(require("./import-builder"));
|
||||
var _importBuilder = require("./import-builder");
|
||||
|
||||
var _isModule = _interopRequireDefault(require("./is-module"));
|
||||
var _isModule = require("./is-module");
|
||||
|
||||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
||||
|
||||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
const {
|
||||
numericLiteral,
|
||||
sequenceExpression
|
||||
} = _t;
|
||||
|
||||
class ImportInjector {
|
||||
constructor(path, importedSource, opts) {
|
||||
@@ -27,7 +26,8 @@ class ImportInjector {
|
||||
importedInterop: "babel",
|
||||
importingInterop: "babel",
|
||||
ensureLiveReference: false,
|
||||
ensureNoContext: false
|
||||
ensureNoContext: false,
|
||||
importPosition: "before"
|
||||
};
|
||||
const programPath = path.find(p => p.isProgram());
|
||||
this._programPath = programPath;
|
||||
@@ -41,7 +41,8 @@ class ImportInjector {
|
||||
}
|
||||
|
||||
addNamed(importName, importedSourceIn, opts) {
|
||||
(0, _assert.default)(typeof importName === "string");
|
||||
_assert(typeof importName === "string");
|
||||
|
||||
return this._generateImport(this._applyDefaults(importedSourceIn, opts), importName);
|
||||
}
|
||||
|
||||
@@ -50,34 +51,25 @@ class ImportInjector {
|
||||
}
|
||||
|
||||
addSideEffect(importedSourceIn, opts) {
|
||||
return this._generateImport(this._applyDefaults(importedSourceIn, opts), false);
|
||||
return this._generateImport(this._applyDefaults(importedSourceIn, opts), void 0);
|
||||
}
|
||||
|
||||
_applyDefaults(importedSource, opts, isInit = false) {
|
||||
const optsList = [];
|
||||
let newOpts;
|
||||
|
||||
if (typeof importedSource === "string") {
|
||||
optsList.push({
|
||||
newOpts = Object.assign({}, this._defaultOpts, {
|
||||
importedSource
|
||||
});
|
||||
optsList.push(opts);
|
||||
}, opts);
|
||||
} else {
|
||||
(0, _assert.default)(!opts, "Unexpected secondary arguments.");
|
||||
optsList.push(importedSource);
|
||||
_assert(!opts, "Unexpected secondary arguments.");
|
||||
|
||||
newOpts = Object.assign({}, this._defaultOpts, importedSource);
|
||||
}
|
||||
|
||||
const newOpts = Object.assign({}, this._defaultOpts);
|
||||
|
||||
for (const opts of optsList) {
|
||||
if (!opts) continue;
|
||||
Object.keys(newOpts).forEach(key => {
|
||||
if (opts[key] !== undefined) newOpts[key] = opts[key];
|
||||
});
|
||||
|
||||
if (!isInit) {
|
||||
if (opts.nameHint !== undefined) newOpts.nameHint = opts.nameHint;
|
||||
if (opts.blockHoist !== undefined) newOpts.blockHoist = opts.blockHoist;
|
||||
}
|
||||
if (!isInit && opts) {
|
||||
if (opts.nameHint !== undefined) newOpts.nameHint = opts.nameHint;
|
||||
if (opts.blockHoist !== undefined) newOpts.blockHoist = opts.blockHoist;
|
||||
}
|
||||
|
||||
return newOpts;
|
||||
@@ -95,12 +87,18 @@ class ImportInjector {
|
||||
ensureLiveReference,
|
||||
ensureNoContext,
|
||||
nameHint,
|
||||
importPosition,
|
||||
blockHoist
|
||||
} = opts;
|
||||
let name = nameHint || importName;
|
||||
const isMod = (0, _isModule.default)(this._programPath);
|
||||
const isModuleForNode = isMod && importingInterop === "node";
|
||||
const isModuleForBabel = isMod && importingInterop === "babel";
|
||||
|
||||
if (importPosition === "after" && !isMod) {
|
||||
throw new Error(`"importPosition": "after" is only supported in modules`);
|
||||
}
|
||||
|
||||
const builder = new _importBuilder.default(importedSource, this._programScope, this._hub);
|
||||
|
||||
if (importedType === "es6") {
|
||||
@@ -240,30 +238,41 @@ class ImportInjector {
|
||||
resultName
|
||||
} = builder.done();
|
||||
|
||||
this._insertStatements(statements, blockHoist);
|
||||
this._insertStatements(statements, importPosition, blockHoist);
|
||||
|
||||
if ((isDefault || isNamed) && ensureNoContext && resultName.type !== "Identifier") {
|
||||
return t.sequenceExpression([t.numericLiteral(0), resultName]);
|
||||
return sequenceExpression([numericLiteral(0), resultName]);
|
||||
}
|
||||
|
||||
return resultName;
|
||||
}
|
||||
|
||||
_insertStatements(statements, blockHoist = 3) {
|
||||
statements.forEach(node => {
|
||||
node._blockHoist = blockHoist;
|
||||
});
|
||||
_insertStatements(statements, importPosition = "before", blockHoist = 3) {
|
||||
const body = this._programPath.get("body");
|
||||
|
||||
const targetPath = this._programPath.get("body").find(p => {
|
||||
const val = p.node._blockHoist;
|
||||
return Number.isFinite(val) && val < 4;
|
||||
});
|
||||
|
||||
if (targetPath) {
|
||||
targetPath.insertBefore(statements);
|
||||
if (importPosition === "after") {
|
||||
for (let i = body.length - 1; i >= 0; i--) {
|
||||
if (body[i].isImportDeclaration()) {
|
||||
body[i].insertAfter(statements);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this._programPath.unshiftContainer("body", statements);
|
||||
statements.forEach(node => {
|
||||
node._blockHoist = blockHoist;
|
||||
});
|
||||
const targetPath = body.find(p => {
|
||||
const val = p.node._blockHoist;
|
||||
return Number.isFinite(val) && val < 4;
|
||||
});
|
||||
|
||||
if (targetPath) {
|
||||
targetPath.insertBefore(statements);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this._programPath.unshiftContainer("body", statements);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+6
-8
@@ -3,16 +3,16 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.addDefault = addDefault;
|
||||
exports.addNamed = addNamed;
|
||||
exports.addNamespace = addNamespace;
|
||||
exports.addSideEffect = addSideEffect;
|
||||
Object.defineProperty(exports, "ImportInjector", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _importInjector.default;
|
||||
}
|
||||
});
|
||||
exports.addDefault = addDefault;
|
||||
exports.addNamed = addNamed;
|
||||
exports.addNamespace = addNamespace;
|
||||
exports.addSideEffect = addSideEffect;
|
||||
Object.defineProperty(exports, "isModule", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
@@ -20,11 +20,9 @@ Object.defineProperty(exports, "isModule", {
|
||||
}
|
||||
});
|
||||
|
||||
var _importInjector = _interopRequireDefault(require("./import-injector"));
|
||||
var _importInjector = require("./import-injector");
|
||||
|
||||
var _isModule = _interopRequireDefault(require("./is-module"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
var _isModule = require("./is-module");
|
||||
|
||||
function addDefault(path, importedSource, opts) {
|
||||
return new _importInjector.default(path).addDefault(importedSource, opts);
|
||||
|
||||
Reference in New Issue
Block a user