Comparing version 0.3.1 to 0.3.2
@@ -98,6 +98,14 @@ var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; }; | ||
function setImportSource(result, node, importExpr) { | ||
if (importExpr.type === "MemberExpression") { | ||
importExpr = importExpr.object; | ||
} | ||
result.source = createSourceNode(node, importExpr.arguments[0]); | ||
return result; | ||
} | ||
function constructCJSImportNode(ast, node) { | ||
var result = constructImportNode(ast, node, "CJSImport"); | ||
var importExpr = undefined, | ||
isVariable = false; | ||
var importExpr = undefined; | ||
@@ -111,26 +119,24 @@ switch (node.type) { | ||
var specifier = createImportSpecifier(node.left, node.right, false); | ||
specifier.local.name = node.left.property.name; | ||
var _ref = node.left.property || node.left, | ||
name = _ref.name; | ||
specifier.local.name = name; | ||
result.specifiers.push(specifier); | ||
importExpr = node.right; | ||
break; | ||
case "VariableDeclaration": | ||
isVariable = true; | ||
/* falls through */ | ||
case "VariableDeclarator": | ||
// init for var, value for property | ||
importExpr = node.init; | ||
result.specifiers.push(createImportSpecifier(node.id, importExpr, true)); | ||
break; | ||
case "Property": | ||
{ | ||
var declaration = isVariable ? node.declarations[0] : node; | ||
// init for var, value for property | ||
var value = declaration.init || declaration.value; | ||
var source = isVariable ? declaration.id : declaration.key; | ||
importExpr = value; | ||
result.specifiers.push(createImportSpecifier(source, importExpr, isVariable)); | ||
importExpr = node.value; | ||
result.specifiers.push(createImportSpecifier(node.key, importExpr, false)); | ||
} | ||
} | ||
if (importExpr.type === "MemberExpression") { | ||
importExpr = importExpr.object; | ||
} | ||
result.source = createSourceNode(node, importExpr.arguments[0]); | ||
return result; | ||
return setImportSource(result, node, importExpr); | ||
} | ||
@@ -141,26 +147,29 @@ | ||
var requires = []; | ||
estraverse.traverse(ast, { | ||
enter: function enter(node) { | ||
var expr = undefined; | ||
function checkRequire(expr) { | ||
if (result(expr, "type") === "MemberExpression") { | ||
expr = expr.object; | ||
} | ||
if (expr && isRequireCallee(expr)) { | ||
requires.push(node); | ||
return true; | ||
} | ||
} | ||
switch (node.type) { | ||
case "MemberExpression": | ||
case "CallExpression": | ||
expr = node; | ||
checkRequire(node); | ||
break; | ||
case "AssignmentExpression": | ||
expr = node.right; | ||
checkRequire(node.right); | ||
break; | ||
case "Property": | ||
case "VariableDeclaration": | ||
var declaration = node.declarations ? node.declarations[0] : node; | ||
// init for var, value for property | ||
expr = declaration.init || declaration.value; | ||
checkRequire(node.value); | ||
break; | ||
case "VariableDeclarator": | ||
checkRequire(node.init); | ||
} | ||
// handle require('x').y; | ||
if (result(expr, "type") === "MemberExpression") { | ||
expr = expr.object; | ||
} | ||
if (expr && isRequireCallee(expr)) { | ||
requires.push(node); | ||
} | ||
} | ||
@@ -167,0 +176,0 @@ }); |
{ | ||
"name": "acorn-umd", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Parse acorn ast for AMD, CommonJS, and ES6 definitions", | ||
@@ -5,0 +5,0 @@ "main": "dist/acorn-umd.js", |
@@ -70,5 +70,14 @@ import {assign, clone, find, filter, matches, pluck, reject, result, sortBy, take, zip} from 'lodash'; | ||
function setImportSource(result, node, importExpr) { | ||
if (importExpr.type === 'MemberExpression') { | ||
importExpr = importExpr.object; | ||
} | ||
result.source = createSourceNode(node, importExpr.arguments[0]); | ||
return result; | ||
} | ||
function constructCJSImportNode(ast, node) { | ||
let result = constructImportNode(ast, node, 'CJSImport'); | ||
let importExpr, isVariable = false; | ||
let importExpr; | ||
@@ -82,25 +91,20 @@ switch (node.type) { | ||
let specifier = createImportSpecifier(node.left, node.right, false); | ||
specifier.local.name = node.left.property.name; | ||
let {name} = node.left.property || node.left; | ||
specifier.local.name = name; | ||
result.specifiers.push(specifier); | ||
importExpr = node.right; | ||
break; | ||
case 'VariableDeclaration': | ||
isVariable = true; | ||
/* falls through */ | ||
case 'VariableDeclarator': | ||
// init for var, value for property | ||
importExpr = node.init; | ||
result.specifiers.push(createImportSpecifier(node.id, importExpr, true)); | ||
break; | ||
case 'Property': { | ||
let declaration = isVariable ? node.declarations[0] : node; | ||
// init for var, value for property | ||
let value = declaration.init || declaration.value; | ||
let source = isVariable ? declaration.id : declaration.key; | ||
importExpr = value; | ||
result.specifiers.push(createImportSpecifier(source, importExpr, isVariable)); | ||
importExpr = node.value; | ||
result.specifiers.push(createImportSpecifier(node.key, importExpr, false)); | ||
} | ||
} | ||
if (importExpr.type === 'MemberExpression') { | ||
importExpr = importExpr.object; | ||
} | ||
result.source = createSourceNode(node, importExpr.arguments[0]); | ||
return result; | ||
return setImportSource(result, node, importExpr); | ||
} | ||
@@ -111,26 +115,29 @@ | ||
let requires = []; | ||
estraverse.traverse(ast, { | ||
enter(node) { | ||
let expr; | ||
function checkRequire(expr) { | ||
if (result(expr, 'type') === 'MemberExpression') { | ||
expr = expr.object; | ||
} | ||
if (expr && isRequireCallee(expr)) { | ||
requires.push(node); | ||
return true; | ||
} | ||
} | ||
switch (node.type) { | ||
case 'MemberExpression': | ||
case 'CallExpression': | ||
expr = node; | ||
checkRequire(node); | ||
break; | ||
case 'AssignmentExpression': | ||
expr = node.right; | ||
checkRequire(node.right); | ||
break; | ||
case 'Property': | ||
case 'VariableDeclaration': | ||
let declaration = node.declarations ? node.declarations[0] : node; | ||
// init for var, value for property | ||
expr = declaration.init || declaration.value; | ||
checkRequire(node.value); | ||
break; | ||
case 'VariableDeclarator': | ||
checkRequire(node.init); | ||
} | ||
// handle require('x').y; | ||
if (result(expr, 'type') === 'MemberExpression') { | ||
expr = expr.object; | ||
} | ||
if (expr && isRequireCallee(expr)) { | ||
requires.push(node); | ||
} | ||
} | ||
@@ -137,0 +144,0 @@ }); |
@@ -29,3 +29,3 @@ import {parse} from 'acorn'; | ||
.each(_.spread((a, b) => { | ||
expect(a.end - a.start).to.be.equal(b.end - b.start - 2); | ||
expect(a.end - a.start).to.be.equal(b.end - b.start - 1); | ||
})).value(); | ||
@@ -265,4 +265,2 @@ }); | ||
let test = imports[0]; | ||
expect(test).to.have.property('start', 13); | ||
expect(test).to.have.property('end', 46); | ||
expect(test.specifiers).to.be.deep.equal([{ | ||
@@ -291,2 +289,62 @@ type: 'ImportSpecifier', | ||
}); | ||
describe('global and comma assignments', function() { | ||
let code = ` | ||
x = require('global'); | ||
let x, y = require('sec'); | ||
`; | ||
let ast = parse(code, {ecmaVersion: 6}); | ||
let imports = umd(ast, { | ||
es6: false, amd: false, cjs: true | ||
}); | ||
it ('should identify require calls', function() { | ||
expect(imports).to.have.length(2); | ||
imports.forEach(node => { | ||
expect(node).to.have.property('source'); | ||
expect(node).to.have.property('specifiers'); | ||
expect(node).to.have.property('type', 'CJSImport'); | ||
expect(node).to.have.property('reference'); | ||
}); | ||
}); | ||
it('globals', function() { | ||
let test = imports[0]; | ||
expect(test.specifiers).to.be.deep.equal([{ | ||
type: 'ImportSpecifier', | ||
local: { | ||
name: 'x', | ||
start: 13, end: 14, | ||
type: 'Identifier' | ||
}, | ||
start: 13, end: 14, | ||
default: false | ||
}]); | ||
expect(_.omit(test.source, '_ast', 'reference')).to.be.deep.equal({ | ||
type: 'Literal', | ||
value: 'global', | ||
raw: '\'global\'', | ||
start: 25, end: 33 | ||
}); | ||
}); | ||
it('comma assign', function() { | ||
let test = imports[1]; | ||
expect(test.specifiers).to.be.deep.equal([{ | ||
type: 'ImportSpecifier', | ||
local: { | ||
name: 'y', | ||
start: 55, end: 56, | ||
type: 'Identifier' | ||
}, | ||
start: 55, end: 56, | ||
default: true | ||
}]); | ||
expect(_.omit(test.source, '_ast', 'reference')).to.be.deep.equal({ | ||
type: 'Literal', | ||
value: 'sec', | ||
raw: '\'sec\'', | ||
start: 67, end: 72 | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
49558
1067
1