Comparing version 6.0.0 to 7.0.0-canary-1
@@ -1,286 +0,190 @@ | ||
'use strict'; | ||
exports.__esModule = true; | ||
var _keys = require('babel-runtime/core-js/object/keys'); | ||
var _keys2 = _interopRequireDefault(_keys); | ||
var _create = require('babel-runtime/core-js/object/create'); | ||
var _create2 = _interopRequireDefault(_create); | ||
var _getIterator2 = require('babel-runtime/core-js/get-iterator'); | ||
var _getIterator3 = _interopRequireDefault(_getIterator2); | ||
var _set = require('babel-runtime/core-js/set'); | ||
var _set2 = _interopRequireDefault(_set); | ||
var _symbol = require('babel-runtime/core-js/symbol'); | ||
var _symbol2 = _interopRequireDefault(_symbol); | ||
exports.default = findGlobals; | ||
var _babylonWalk = require('babylon-walk'); | ||
var _babelTypes = require('babel-types'); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
var _reference = require('./reference.js'); | ||
var _reference2 = _interopRequireDefault(_reference); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var isScope = t.isFunctionParent; | ||
var isBlockScope = function isBlockScope(node) { | ||
return t.isBlockStatement(node) || isScope(node); | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var declaresArguments = function declaresArguments(node) { | ||
return t.isFunction(node) && !t.isArrowFunctionExpression(node); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var declaresThis = declaresArguments; | ||
var LOCALS_SYMBOL = (0, _symbol2.default)('locals'); | ||
var declareLocal = function declareLocal(node) { | ||
return node[LOCALS_SYMBOL] = node[LOCALS_SYMBOL] || new _set2.default(); | ||
}; | ||
var setLocal = function setLocal(node, name) { | ||
return declareLocal(node).add(name); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const assert_never_1 = __importDefault(require("assert-never")); | ||
const babel_walk_1 = require("babel-walk"); | ||
const t = __importStar(require("@babel/types")); | ||
const reference_1 = __importDefault(require("./reference")); | ||
const isScope = t.isFunctionParent; | ||
const isBlockScope = (node) => t.isBlockStatement(node) || isScope(node); | ||
const declaresArguments = (node) => t.isFunction(node) && !t.isArrowFunctionExpression(node); | ||
const declaresThis = declaresArguments; | ||
const LOCALS_SYMBOL = Symbol('locals'); | ||
const getLocals = (node) => node[LOCALS_SYMBOL]; | ||
const declareLocals = (node) => (node[LOCALS_SYMBOL] = node[LOCALS_SYMBOL] || new Set()); | ||
const setLocal = (node, name) => declareLocals(node).add(name); | ||
// First pass | ||
function declareFunction(node) { | ||
for (var _iterator = node.params, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) { | ||
var _ref; | ||
if (_isArray) { | ||
if (_i >= _iterator.length) break; | ||
_ref = _iterator[_i++]; | ||
} else { | ||
_i = _iterator.next(); | ||
if (_i.done) break; | ||
_ref = _i.value; | ||
for (const param of node.params) { | ||
declarePattern(param, node); | ||
} | ||
var param = _ref; | ||
declarePattern(param, node); | ||
} | ||
if (node.id) { | ||
setLocal(node, node.id.name); | ||
} | ||
const id = node.id; | ||
if (id) { | ||
setLocal(node, id.name); | ||
} | ||
} | ||
function declarePattern(node, parent) { | ||
switch (node.type) { | ||
case 'Identifier': | ||
setLocal(parent, node.name); | ||
break; | ||
case 'ObjectPattern': | ||
for (var _iterator2 = node.properties, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) { | ||
var _ref2; | ||
if (_isArray2) { | ||
if (_i2 >= _iterator2.length) break; | ||
_ref2 = _iterator2[_i2++]; | ||
} else { | ||
_i2 = _iterator2.next(); | ||
if (_i2.done) break; | ||
_ref2 = _i2.value; | ||
switch (node.type) { | ||
case 'Identifier': | ||
setLocal(parent, node.name); | ||
break; | ||
case 'ObjectPattern': | ||
for (const prop of node.properties) { | ||
switch (prop.type) { | ||
case 'RestElement': | ||
declarePattern(prop.argument, parent); | ||
break; | ||
case 'ObjectProperty': | ||
declarePattern(prop.value, parent); | ||
break; | ||
default: | ||
assert_never_1.default(prop); | ||
break; | ||
} | ||
} | ||
break; | ||
case 'ArrayPattern': | ||
for (const element of node.elements) { | ||
if (element) | ||
declarePattern(element, parent); | ||
} | ||
break; | ||
case 'RestElement': | ||
declarePattern(node.argument, parent); | ||
break; | ||
case 'AssignmentPattern': | ||
declarePattern(node.left, parent); | ||
break; | ||
// istanbul ignore next | ||
default: | ||
throw new Error('Unrecognized pattern type: ' + node.type); | ||
} | ||
} | ||
function declareModuleSpecifier(node, _state, parents) { | ||
for (let i = parents.length - 2; i >= 0; i--) { | ||
if (isScope(parents[i])) { | ||
setLocal(parents[i], node.local.name); | ||
return; | ||
} | ||
var prop = _ref2; | ||
declarePattern(prop.value, parent); | ||
} | ||
break; | ||
case 'ArrayPattern': | ||
for (var _iterator3 = node.elements, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) { | ||
var _ref3; | ||
if (_isArray3) { | ||
if (_i3 >= _iterator3.length) break; | ||
_ref3 = _iterator3[_i3++]; | ||
} else { | ||
_i3 = _iterator3.next(); | ||
if (_i3.done) break; | ||
_ref3 = _i3.value; | ||
} | ||
} | ||
const firstPass = babel_walk_1.ancestor({ | ||
VariableDeclaration(node, _state, parents) { | ||
for (let i = parents.length - 2; i >= 0; i--) { | ||
if (node.kind === 'var' | ||
? t.isFunctionParent(parents[i]) | ||
: isBlockScope(parents[i])) { | ||
for (const declaration of node.declarations) { | ||
declarePattern(declaration.id, parents[i]); | ||
} | ||
return; | ||
} | ||
} | ||
var element = _ref3; | ||
if (element) declarePattern(element, parent); | ||
} | ||
break; | ||
case 'RestElement': | ||
declarePattern(node.argument, parent); | ||
break; | ||
case 'AssignmentPattern': | ||
declarePattern(node.left, parent); | ||
break; | ||
// istanbul ignore next | ||
default: | ||
throw new Error('Unrecognized pattern type: ' + node.type); | ||
} | ||
} | ||
function declareModuleSpecifier(node, state, parents) { | ||
setLocal(parents[1], node.local.name); | ||
} | ||
var firstPass = { | ||
VariableDeclaration: function VariableDeclaration(node, state, parents) { | ||
var parent = void 0; | ||
for (var i = parents.length - 2; i >= 0 && !parent; i--) { | ||
if (node.kind === 'var' ? t.isFunctionParent(parents[i]) : isBlockScope(parents[i])) { | ||
parent = parents[i]; | ||
} | ||
} | ||
for (var _iterator4 = node.declarations, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : (0, _getIterator3.default)(_iterator4);;) { | ||
var _ref4; | ||
if (_isArray4) { | ||
if (_i4 >= _iterator4.length) break; | ||
_ref4 = _iterator4[_i4++]; | ||
} else { | ||
_i4 = _iterator4.next(); | ||
if (_i4.done) break; | ||
_ref4 = _i4.value; | ||
} | ||
var declaration = _ref4; | ||
declarePattern(declaration.id, parent); | ||
} | ||
}, | ||
FunctionDeclaration: function FunctionDeclaration(node, state, parents) { | ||
var parent = void 0; | ||
for (var i = parents.length - 2; i >= 0 && !parent; i--) { | ||
if (isScope(parents[i])) { | ||
parent = parents[i]; | ||
} | ||
} | ||
setLocal(parent, node.id.name); | ||
declareFunction(node); | ||
}, | ||
Function: declareFunction, | ||
ClassDeclaration: function ClassDeclaration(node, state, parents) { | ||
var parent = void 0; | ||
for (var i = parents.length - 2; i >= 0 && !parent; i--) { | ||
if (isScope(parents[i])) { | ||
parent = parents[i]; | ||
} | ||
} | ||
setLocal(parent, node.id.name); | ||
}, | ||
TryStatement: function TryStatement(node) { | ||
if (node.handler === null) return; | ||
setLocal(node.handler, node.handler.param.name); | ||
}, | ||
ImportDefaultSpecifier: declareModuleSpecifier, | ||
ImportSpecifier: declareModuleSpecifier, | ||
ImportNamespaceSpecifier: declareModuleSpecifier | ||
}; | ||
}, | ||
FunctionDeclaration(node, _state, parents) { | ||
if (node.id) { | ||
for (let i = parents.length - 2; i >= 0; i--) { | ||
if (isScope(parents[i])) { | ||
setLocal(parents[i], node.id.name); | ||
return; | ||
} | ||
} | ||
} | ||
}, | ||
Function: declareFunction, | ||
ClassDeclaration(node, _state, parents) { | ||
for (let i = parents.length - 2; i >= 0; i--) { | ||
if (isScope(parents[i])) { | ||
setLocal(parents[i], node.id.name); | ||
return; | ||
} | ||
} | ||
}, | ||
TryStatement(node) { | ||
if (node.handler === null) | ||
return; | ||
if (node.handler.param === null) | ||
return; | ||
declarePattern(node.handler.param, node.handler); | ||
}, | ||
ImportDefaultSpecifier: declareModuleSpecifier, | ||
ImportSpecifier: declareModuleSpecifier, | ||
ImportNamespaceSpecifier: declareModuleSpecifier, | ||
}); | ||
// Second pass | ||
var secondPass = { | ||
Identifier: function Identifier(node, state, parents) { | ||
var name = node.name; | ||
if (name === 'undefined') return; | ||
var lastParent = parents[parents.length - 2]; | ||
if (lastParent) { | ||
if (!(0, _reference2.default)(node, lastParent)) return; | ||
var parent = void 0; | ||
for (var _iterator5 = parents, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : (0, _getIterator3.default)(_iterator5);;) { | ||
var _ref5; | ||
if (_isArray5) { | ||
if (_i5 >= _iterator5.length) break; | ||
_ref5 = _iterator5[_i5++]; | ||
} else { | ||
_i5 = _iterator5.next(); | ||
if (_i5.done) break; | ||
_ref5 = _i5.value; | ||
const secondPass = babel_walk_1.ancestor({ | ||
Identifier(node, state, parents) { | ||
var _a; | ||
const name = node.name; | ||
if (name === 'undefined') | ||
return; | ||
const lastParent = parents[parents.length - 2]; | ||
if (lastParent) { | ||
if (!reference_1.default(node, lastParent)) | ||
return; | ||
for (const parent of parents) { | ||
if (name === 'arguments' && declaresArguments(parent)) { | ||
return; | ||
} | ||
if ((_a = getLocals(parent)) === null || _a === void 0 ? void 0 : _a.has(name)) { | ||
return; | ||
} | ||
} | ||
} | ||
var _parent = _ref5; | ||
if (name === 'arguments' && declaresArguments(_parent)) { | ||
return; | ||
state.globals.push(node); | ||
}, | ||
ThisExpression(node, state, parents) { | ||
for (const parent of parents) { | ||
if (declaresThis(parent)) { | ||
return; | ||
} | ||
} | ||
if (_parent[LOCALS_SYMBOL] && _parent[LOCALS_SYMBOL].has(name)) { | ||
return; | ||
state.globals.push(node); | ||
}, | ||
}); | ||
function findGlobals(ast) { | ||
const globals = []; | ||
// istanbul ignore if | ||
if (!t.isNode(ast)) { | ||
throw new TypeError('Source must be a Babylon AST'); | ||
} | ||
firstPass(ast, undefined); | ||
secondPass(ast, { globals }); | ||
const groupedGlobals = new Map(); | ||
for (const node of globals) { | ||
const name = node.type === 'ThisExpression' ? 'this' : node.name; | ||
const existing = groupedGlobals.get(name); | ||
if (existing) { | ||
existing.push(node); | ||
} | ||
} | ||
else { | ||
groupedGlobals.set(name, [node]); | ||
} | ||
} | ||
state.globals.push(node); | ||
}, | ||
ThisExpression: function ThisExpression(node, state, parents) { | ||
for (var _iterator6 = parents, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : (0, _getIterator3.default)(_iterator6);;) { | ||
var _ref6; | ||
if (_isArray6) { | ||
if (_i6 >= _iterator6.length) break; | ||
_ref6 = _iterator6[_i6++]; | ||
} else { | ||
_i6 = _iterator6.next(); | ||
if (_i6.done) break; | ||
_ref6 = _i6.value; | ||
} | ||
var parent = _ref6; | ||
if (declaresThis(parents)) { | ||
return; | ||
} | ||
} | ||
state.globals.push(node); | ||
} | ||
}; | ||
function findGlobals(ast) { | ||
var globals = []; | ||
// istanbul ignore if | ||
if (!t.isNode(ast)) { | ||
throw new TypeError('Source must be a Babylon AST'); | ||
} | ||
(0, _babylonWalk.ancestor)(ast, firstPass); | ||
(0, _babylonWalk.ancestor)(ast, secondPass, { globals: globals }); | ||
var groupedGlobals = (0, _create2.default)(null); | ||
for (var _iterator7 = globals, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : (0, _getIterator3.default)(_iterator7);;) { | ||
var _ref7; | ||
if (_isArray7) { | ||
if (_i7 >= _iterator7.length) break; | ||
_ref7 = _iterator7[_i7++]; | ||
} else { | ||
_i7 = _iterator7.next(); | ||
if (_i7.done) break; | ||
_ref7 = _i7.value; | ||
} | ||
var node = _ref7; | ||
var name = node.type === 'ThisExpression' ? 'this' : node.name; | ||
groupedGlobals[name] = groupedGlobals[name] || []; | ||
groupedGlobals[name].push(node); | ||
} | ||
return (0, _keys2.default)(groupedGlobals).sort().map(function (name) { | ||
return { name: name, nodes: groupedGlobals[name] }; | ||
}); | ||
return [...groupedGlobals] | ||
.map(([name, nodes]) => ({ name, nodes })) | ||
.sort((a, b) => (a.name < b.name ? -1 : 1)); | ||
} | ||
module.exports = exports['default']; | ||
exports.default = findGlobals; | ||
//# sourceMappingURL=globals.js.map |
276
lib/index.js
@@ -1,129 +0,120 @@ | ||
'use strict'; | ||
exports.__esModule = true; | ||
var _stringify = require('babel-runtime/core-js/json/stringify'); | ||
var _stringify2 = _interopRequireDefault(_stringify); | ||
var _from = require('babel-runtime/core-js/array/from'); | ||
var _from2 = _interopRequireDefault(_from); | ||
var _set = require('babel-runtime/core-js/set'); | ||
var _set2 = _interopRequireDefault(_set); | ||
var _assign = require('babel-runtime/core-js/object/assign'); | ||
var _assign2 = _interopRequireDefault(_assign); | ||
exports.default = addWith; | ||
var _babylon = require('babylon'); | ||
var _babylonWalk = require('babylon-walk'); | ||
var _babelTypes = require('babel-types'); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
var _globals = require('./globals.js'); | ||
var _globals2 = _interopRequireDefault(_globals); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var includes = function includes(array, searchElement, fromIndex) { | ||
return Array.prototype.includes.call(array, searchElement, fromIndex); | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var parseOptions = { | ||
allowReturnOutsideFunction: true, | ||
allowImportExportEverywhere: true | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const parser_1 = require("@babel/parser"); | ||
const babel_walk_1 = require("babel-walk"); | ||
const t = __importStar(require("@babel/types")); | ||
const globals_1 = __importDefault(require("./globals")); | ||
const parseOptions = { | ||
allowReturnOutsideFunction: true, | ||
allowImportExportEverywhere: true, | ||
}; | ||
/** | ||
* Mimic `with` as far as possible but at compile time | ||
* | ||
* @param {String} obj The object part of a with expression | ||
* @param {String} src The body of the with expression | ||
* @param {Array.<String>} exclude A list of variable names to explicitly exclude | ||
* @param obj The object part of a with expression | ||
* @param src The body of the with expression | ||
* @param exclude A list of variable names to explicitly exclude | ||
*/ | ||
function addWith(obj, src) { | ||
var exclude = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
obj = obj + ''; | ||
src = src + ''; | ||
var ast = void 0; | ||
try { | ||
ast = (0, _babylon.parse)(src, parseOptions); | ||
} catch (e) { | ||
throw (0, _assign2.default)(new Error('Error parsing body of the with expression'), { | ||
component: 'src', | ||
babylonError: e | ||
}); | ||
} | ||
var objAst = void 0; | ||
try { | ||
objAst = (0, _babylon.parse)(obj, parseOptions); | ||
} catch (e) { | ||
throw (0, _assign2.default)(new Error('Error parsing object part of the with expression'), { | ||
component: 'obj', | ||
babylonError: e | ||
}); | ||
} | ||
exclude = new _set2.default(['undefined', 'this'].concat(exclude, (0, _globals2.default)(objAst).map(function (g) { | ||
return g.name; | ||
}))); | ||
var vars = new _set2.default((0, _globals2.default)(ast).map(function (global) { | ||
return global.name; | ||
}).filter(function (v) { | ||
return !exclude.has(v); | ||
})); | ||
if (vars.size === 0) return src; | ||
var declareLocal = ''; | ||
var local = 'locals_for_with'; | ||
var result = 'result_of_with'; | ||
if (t.isValidIdentifier(obj)) { | ||
local = obj; | ||
} else { | ||
while (vars.has(local) || exclude.has(local)) { | ||
local += '_'; | ||
function addWith(obj, src, exclude = []) { | ||
// tslint:disable-next-line: no-parameter-reassignment | ||
obj = obj + ''; | ||
// tslint:disable-next-line: no-parameter-reassignment | ||
src = src + ''; | ||
let ast; | ||
try { | ||
ast = parser_1.parse(src, parseOptions); | ||
} | ||
declareLocal = 'var ' + local + ' = (' + obj + ');'; | ||
} | ||
while (vars.has(result) || exclude.has(result)) { | ||
result += '_'; | ||
} | ||
var args = ['this'].concat((0, _from2.default)(vars).map(function (v) { | ||
return (0, _stringify2.default)(v) + ' in ' + local + ' ?\n ' + local + '.' + v + ' :\n typeof ' + v + ' !== \'undefined\' ? ' + v + ' : undefined'; | ||
})); | ||
var unwrapped = unwrapReturns(ast, src, result); | ||
return ';\n ' + declareLocal + '\n ' + unwrapped.before + '\n (function (' + (0, _from2.default)(vars).join(', ') + ') {\n ' + unwrapped.body + '\n }.call(' + args.join(', ') + '));\n ' + unwrapped.after + ';'; | ||
catch (e) { | ||
throw Object.assign(new Error('Error parsing body of the with expression'), { | ||
component: 'src', | ||
babylonError: e, | ||
}); | ||
} | ||
let objAst; | ||
try { | ||
objAst = parser_1.parse(obj, parseOptions); | ||
} | ||
catch (e) { | ||
throw Object.assign(new Error('Error parsing object part of the with expression'), { | ||
component: 'obj', | ||
babylonError: e, | ||
}); | ||
} | ||
const excludeSet = new Set([ | ||
'undefined', | ||
'this', | ||
...exclude, | ||
...globals_1.default(objAst).map((g) => g.name), | ||
]); | ||
const vars = new Set(globals_1.default(ast) | ||
.map((global) => global.name) | ||
.filter((v) => !excludeSet.has(v))); | ||
if (vars.size === 0) | ||
return src; | ||
let declareLocal = ''; | ||
let local = 'locals_for_with'; | ||
let result = 'result_of_with'; | ||
if (t.isValidIdentifier(obj)) { | ||
local = obj; | ||
} | ||
else { | ||
while (vars.has(local) || excludeSet.has(local)) { | ||
local += '_'; | ||
} | ||
declareLocal = `var ${local} = (${obj});`; | ||
} | ||
while (vars.has(result) || excludeSet.has(result)) { | ||
result += '_'; | ||
} | ||
const args = [ | ||
'this', | ||
...Array.from(vars).map((v) => `${JSON.stringify(v)} in ${local} ? | ||
${local}.${v} : | ||
typeof ${v} !== 'undefined' ? ${v} : undefined`), | ||
]; | ||
const unwrapped = unwrapReturns(ast, src, result); | ||
return `; | ||
${declareLocal} | ||
${unwrapped.before} | ||
(function (${Array.from(vars).join(', ')}) { | ||
${unwrapped.body} | ||
}.call(${args.join(', ')})); | ||
${unwrapped.after};`; | ||
} | ||
var unwrapReturnsVisitors = { | ||
Function: function Function(node, state, c) { | ||
// returns in these functions are not applicable | ||
}, | ||
ReturnStatement: function ReturnStatement(node, state) { | ||
state.hasReturn = true; | ||
var value = ''; | ||
if (node.argument) { | ||
value = 'value: (' + state.source(node.argument) + ')'; | ||
} | ||
state.replace(node, 'return {' + value + '};'); | ||
} | ||
}; | ||
exports.default = addWith; | ||
const unwrapReturnsVisitors = babel_walk_1.recursive({ | ||
Function(_node, _state, _c) { | ||
// returns in these functions are not applicable | ||
}, | ||
ReturnStatement(node, state) { | ||
state.hasReturn = true; | ||
let value = ''; | ||
if (node.argument) { | ||
value = `value: (${state.source(node.argument)})`; | ||
} | ||
state.replace(node, `return {${value}};`); | ||
}, | ||
}); | ||
/** | ||
@@ -133,27 +124,26 @@ * Take a self calling function, and unwrap it such that return inside the function | ||
* | ||
* @param {String} src Some JavaScript code representing a self-calling function | ||
* @param {String} result A temporary variable to store the result in | ||
* @param src Some JavaScript code representing a self-calling function | ||
* @param result A temporary variable to store the result in | ||
*/ | ||
function unwrapReturns(ast, src, result) { | ||
var charArray = src.split(''); | ||
var state = { | ||
hasReturn: false, | ||
source: function source(node) { | ||
return src.slice(node.start, node.end); | ||
}, | ||
replace: function replace(node, str) { | ||
charArray.fill('', node.start, node.end); | ||
charArray[node.start] = str; | ||
} | ||
}; | ||
(0, _babylonWalk.recursive)(ast, unwrapReturnsVisitors, state); | ||
return { | ||
before: state.hasReturn ? 'var ' + result + ' = ' : '', | ||
body: charArray.join(''), | ||
after: state.hasReturn ? ';if (' + result + ') return ' + result + '.value' : '' | ||
}; | ||
const charArray = src.split(''); | ||
const state = { | ||
hasReturn: false, | ||
source(node) { | ||
return src.slice(node.start, node.end); | ||
}, | ||
replace(node, str) { | ||
charArray.fill('', node.start, node.end); | ||
charArray[node.start] = str; | ||
}, | ||
}; | ||
unwrapReturnsVisitors(ast, state); | ||
return { | ||
before: state.hasReturn ? `var ${result} = ` : '', | ||
body: charArray.join(''), | ||
after: state.hasReturn ? `;if (${result}) return ${result}.value` : '', | ||
}; | ||
} | ||
module.exports = exports['default']; | ||
module.exports = addWith; | ||
module.exports.default = addWith; | ||
//# sourceMappingURL=index.js.map |
@@ -1,34 +0,43 @@ | ||
'use strict'; | ||
exports.__esModule = true; | ||
exports.default = isReferenced; | ||
var _babelTypes = require('babel-types'); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const t = __importStar(require("@babel/types")); | ||
function isReferenced(node, parent) { | ||
switch (parent.type) { | ||
// yes: { [NODE]: '' } | ||
// yes: { NODE } | ||
// no: { NODE: '' } | ||
case 'ObjectProperty': | ||
return parent.value === node || parent.computed; | ||
// no: break NODE; | ||
// no: continue NODE; | ||
case 'BreakStatement': | ||
case 'ContinueStatement': | ||
return false; | ||
// yes: left = NODE; | ||
// yes: NODE = right; | ||
case 'AssignmentExpression': | ||
return true; | ||
} | ||
return t.isReferenced(node, parent); | ||
switch (parent.type) { | ||
// yes: { [NODE]: '' } | ||
// yes: { NODE } | ||
// no: { NODE: '' } | ||
case 'ObjectProperty': | ||
return parent.value === node || parent.computed; | ||
// no: break NODE; | ||
// no: continue NODE; | ||
case 'BreakStatement': | ||
case 'ContinueStatement': | ||
return false; | ||
// yes: left = NODE; | ||
// yes: NODE = right; | ||
case 'AssignmentExpression': | ||
return true; | ||
} | ||
return t.isReferenced(node, parent); | ||
} | ||
module.exports = exports['default']; | ||
exports.default = isReferenced; | ||
//# sourceMappingURL=reference.js.map |
{ | ||
"name": "with", | ||
"version": "6.0.0", | ||
"version": "7.0.0-canary-1", | ||
"description": "Compile time `with` for strict mode JavaScript", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"prepublish": "babel -d lib src", | ||
"test": "babel-node node_modules/mocha/bin/_mocha test/index.js -R spec" | ||
"build": "tsc", | ||
"postbuild": "rimraf lib/**/__tests__", | ||
"lint": "tslint './src/**/*.{ts,tsx}' -t verbose -p .", | ||
"prettier:write": "prettier --ignore-path .gitignore --write './**/*.{md,json,yaml,js,jsx,ts,tsx}'", | ||
"prettier:check": "prettier --ignore-path .gitignore --list-different './**/*.{md,json,yaml,js,jsx,ts,tsx}'", | ||
"pretest": "yarn build", | ||
"test": "mocha test/index.js -R spec" | ||
}, | ||
@@ -17,15 +22,20 @@ "repository": { | ||
"dependencies": { | ||
"babel-runtime": "^6.11.6", | ||
"babel-types": "^6.15.0", | ||
"babylon": "^6.9.1", | ||
"babylon-walk": "^1.0.2" | ||
"@babel/parser": "^7.9.6", | ||
"@babel/types": "^7.9.6", | ||
"assert-never": "^1.2.1", | ||
"babel-walk": "3.0.0-canary-5" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.14.0", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-plugin-transform-runtime": "^6.15.0", | ||
"babel-preset-es2015": "^6.14.0", | ||
"@forbeslindesay/tsconfig": "^2.0.0", | ||
"@types/node": "^14.0.5", | ||
"mocha": "*", | ||
"prettier": "^2.0.5", | ||
"rimraf": "^3.0.2", | ||
"tslint": "^6.1.2", | ||
"typescript": "^3.9.3", | ||
"uglify-js": "^2.6.2" | ||
}, | ||
"engines": { | ||
"node": ">= 10.0.0" | ||
} | ||
} | ||
} |
@@ -5,5 +5,5 @@ # with | ||
[![build status](https://img.shields.io/travis/pugjs/with.svg)](http://travis-ci.org/pugjs/with) | ||
[![Dependency Status](https://img.shields.io/david/pugjs/with.svg)](https://david-dm.org/pugjs/with) | ||
[![NPM version](https://img.shields.io/npm/v/with.svg)](https://www.npmjs.com/package/with) | ||
[![Build Status](https://img.shields.io/github/workflow/status/pugjs/with/Publish%20Canary/master?style=for-the-badge)](https://github.com/pugjs/with/actions?query=workflow%3A%22Publish+Canary%22) | ||
[![Rolling Versions](https://img.shields.io/badge/Rolling%20Versions-Enabled-brightgreen?style=for-the-badge)](https://rollingversions.com/pugjs/with) | ||
[![NPM version](https://img.shields.io/npm/v/with?style=for-the-badge)](https://www.npmjs.com/package/with) | ||
@@ -17,5 +17,5 @@ ## Installation | ||
```js | ||
var addWith = require('with') | ||
var addWith = require('with'); | ||
addWith('obj', 'console.log(a)') | ||
addWith('obj', 'console.log(a)'); | ||
// => ';(function (console, a) { | ||
@@ -28,3 +28,3 @@ // console.log(a) | ||
addWith('obj', 'console.log(a)', ['console']) | ||
addWith('obj', 'console.log(a)', ['console']); | ||
// => ';(function (console, a) { | ||
@@ -44,7 +44,7 @@ // console.log(a) | ||
with (obj) { | ||
src | ||
src; | ||
} | ||
``` | ||
There are a few differences though. For starters, assignments to variables will always remain contained within the with block. | ||
There are a few differences though. For starters, assignments to variables will always remain contained within the with block. | ||
@@ -54,13 +54,13 @@ e.g. | ||
```js | ||
var foo = 'foo' | ||
var foo = 'foo'; | ||
with ({}) { | ||
foo = 'bar' | ||
foo = 'bar'; | ||
} | ||
assert(foo === 'bar')// => This fails for compile time with but passes for native with | ||
assert(foo === 'bar'); // => This fails for compile time with but passes for native with | ||
var obj = {foo: 'foo'} | ||
var obj = {foo: 'foo'}; | ||
with ({}) { | ||
foo = 'bar' | ||
foo = 'bar'; | ||
} | ||
assert(obj.foo === 'bar')// => This fails for compile time with but passes for native with | ||
assert(obj.foo === 'bar'); // => This fails for compile time with but passes for native with | ||
``` | ||
@@ -80,3 +80,3 @@ | ||
This is not the case if foo is in `exclude`. If a variable is excluded, we ignore it entirely. This is useful if you know a variable will be global as it can lead to efficiency improvements. | ||
This is not the case if foo is in `exclude`. If a variable is excluded, we ignore it entirely. This is useful if you know a variable will be global as it can lead to efficiency improvements. | ||
@@ -87,4 +87,4 @@ It is also safe to use in strict mode (unlike `with`) and it minifies properly (`with` disables virtually all minification). | ||
with internally uses babylon to parse code passed to `addWith`. If babylon throws an error, probably due to a syntax error, `addWith` returns an error wrapping the babylon error, so you can | ||
retrieve location information. `error.component` is `"src"` if the error is in the body or `"obj"` if it's in the object part of the with expression. `error.babylonError` is | ||
with internally uses babylon to parse code passed to `addWith`. If babylon throws an error, probably due to a syntax error, `addWith` returns an error wrapping the babylon error, so you can | ||
retrieve location information. `error.component` is `"src"` if the error is in the body or `"obj"` if it's in the object part of the with expression. `error.babylonError` is | ||
the error thrown from babylon. | ||
@@ -94,2 +94,2 @@ | ||
MIT | ||
MIT |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
124897
23
741
8
1
2
+ Added@babel/parser@^7.9.6
+ Added@babel/types@^7.9.6
+ Addedassert-never@^1.2.1
+ Addedbabel-walk@3.0.0-canary-5
+ Added@babel/helper-string-parser@7.24.1(transitive)
+ Added@babel/helper-validator-identifier@7.24.5(transitive)
+ Added@babel/parser@7.24.5(transitive)
+ Added@babel/types@7.24.5(transitive)
+ Addedassert-never@1.2.1(transitive)
+ Addedbabel-walk@3.0.0-canary-5(transitive)
+ Addedto-fast-properties@2.0.0(transitive)
- Removedbabel-runtime@^6.11.6
- Removedbabel-types@^6.15.0
- Removedbabylon@^6.9.1
- Removedbabylon-walk@^1.0.2
- Removedbabel-runtime@6.26.0(transitive)
- Removedbabel-types@6.26.0(transitive)
- Removedbabylon@6.18.0(transitive)
- Removedbabylon-walk@1.0.2(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedesutils@2.0.3(transitive)
- Removedlodash@4.17.21(transitive)
- Removedlodash.clone@4.5.0(transitive)
- Removedregenerator-runtime@0.11.1(transitive)
- Removedto-fast-properties@1.0.3(transitive)