Comparing version 1.10.0 to 1.10.1
{ | ||
"name": "esnext", | ||
"version": "1.10.0", | ||
"version": "1.10.1", | ||
"description": "Update your project to the latest ECMAScript syntax.", | ||
@@ -5,0 +5,0 @@ "main": "dist/esnext.umd.js", |
@@ -12,3 +12,3 @@ import Module from './module'; | ||
type PluginBookendCallback = (m: Module) => ?Object; | ||
type PluginTraversalCallback = (node: Object, parent: Object, module: Module, context: ?Object) => ?VisitorOption; | ||
type PluginTraversalCallback = (node: Object, module: Module, context: ?Object) => ?VisitorOption; | ||
@@ -49,10 +49,15 @@ type Plugin = { | ||
enter(node, parent) { | ||
Object.defineProperty(node, 'parentNode', { | ||
value: parent, | ||
configurable: true, | ||
enumerable: false | ||
}); | ||
if (enter) { | ||
return enter(node, parent, module, context); | ||
return enter(node, module, context); | ||
} | ||
}, | ||
leave(node, parent) { | ||
leave(node) { | ||
if (leave) { | ||
return leave(node, parent, module, context); | ||
return leave(node, module, context); | ||
} | ||
@@ -59,0 +64,0 @@ } |
@@ -25,3 +25,3 @@ import BaseContext from '../context'; | ||
export function enter(node: Object, parent: Object, module: Module, context: Context): ?VisitorOption { | ||
export function enter(node: Object, module: Module, context: Context): ?VisitorOption { | ||
if (node.type !== Syntax.VariableDeclaration) { | ||
@@ -28,0 +28,0 @@ return null; |
@@ -23,5 +23,5 @@ import BaseContext from '../context'; | ||
rewrite(node: Object, parent: Object): boolean { | ||
rewrite(node: Object): boolean { | ||
return ( | ||
this.rewriteFunctionExpression(node, parent) || | ||
this.rewriteFunctionExpression(node) || | ||
this.rewriteCallExpression(node) | ||
@@ -31,3 +31,3 @@ ); | ||
rewriteFunctionExpression(node: Object, parent: Object): boolean { | ||
rewriteFunctionExpression(node: Object): boolean { | ||
if (node.type !== Syntax.FunctionExpression) { | ||
@@ -49,3 +49,5 @@ return false; | ||
if (parent.type === Syntax.Property && parent.method) { | ||
const { parentNode } = node; | ||
if (parentNode.type === Syntax.Property && parentNode.method) { | ||
return false; | ||
@@ -64,3 +66,3 @@ } | ||
this._rewriteBlocklessArrowFunction(node, parent); | ||
this._rewriteBlocklessArrowFunction(node); | ||
@@ -114,3 +116,3 @@ return true; | ||
_rewriteBlocklessArrowFunction(node: Object, parent: Object) { | ||
_rewriteBlocklessArrowFunction(node: Object) { | ||
const [ statement ] = node.body.body; | ||
@@ -185,3 +187,3 @@ | ||
if (needsParens(node, parent) && !hasParens(node, this.module)) { | ||
if (needsParens(node) && !hasParens(node, this.module)) { | ||
this.insert(node.range[0], '('); | ||
@@ -238,4 +240,4 @@ this.insert(node.range[1], ')'); | ||
export function enter(node: Object, parent: Object, module: Module, context: Context): ?VisitorOption { | ||
context.rewrite(node, parent); | ||
export function enter(node: Object, module: Module, context: Context): ?VisitorOption { | ||
context.rewrite(node); | ||
return null; | ||
@@ -242,0 +244,0 @@ } |
@@ -48,7 +48,7 @@ import BaseContext from '../context'; | ||
rewrite(node: Object, parent: Object): boolean { | ||
rewrite(node: Object): boolean { | ||
return ( | ||
this.rewriteRequire(node, parent) || | ||
this.rewriteExport(node, parent) || | ||
this.removeUseStrictDirective(node, parent) | ||
this.rewriteRequire(node) || | ||
this.rewriteExport(node) || | ||
this.removeUseStrictDirective(node) | ||
); | ||
@@ -60,9 +60,9 @@ } | ||
*/ | ||
rewriteRequire(node: Object, parent: Object): boolean { | ||
rewriteRequire(node: Object): boolean { | ||
return ( | ||
this.rewriteSingleExportRequire(node, parent) || | ||
this.rewriteNamedExportRequire(node, parent) || | ||
this.rewriteDeconstructedImportRequire(node, parent) || | ||
this.rewriteSideEffectRequire(node, parent) || | ||
this.warnAboutUnsupportedRequire(node, parent) | ||
this.rewriteSingleExportRequire(node) || | ||
this.rewriteNamedExportRequire(node) || | ||
this.rewriteDeconstructedImportRequire(node) || | ||
this.rewriteSideEffectRequire(node) || | ||
this.warnAboutUnsupportedRequire(node) | ||
); | ||
@@ -74,4 +74,5 @@ } | ||
*/ | ||
rewriteSingleExportRequire(node: Object, parent: Object): boolean { | ||
if (!parent || parent.type !== Syntax.Program) { | ||
rewriteSingleExportRequire(node: Object): boolean { | ||
const { parentNode } = node; | ||
if (!parentNode || parentNode.type !== Syntax.Program) { | ||
return false; | ||
@@ -120,4 +121,5 @@ } | ||
*/ | ||
rewriteNamedExportRequire(node: Object, parent: Object): boolean { | ||
if (!parent || parent.type !== Syntax.Program) { | ||
rewriteNamedExportRequire(node: Object): boolean { | ||
const { parentNode } = node; | ||
if (!parentNode || parentNode.type !== Syntax.Program) { | ||
return false; | ||
@@ -167,4 +169,5 @@ } | ||
*/ | ||
rewriteDeconstructedImportRequire(node: Object, parent: Object): boolean { | ||
if (!parent || parent.type !== Syntax.Program) { | ||
rewriteDeconstructedImportRequire(node: Object): boolean { | ||
const { parentNode } = node; | ||
if (!parentNode || parentNode.type !== Syntax.Program) { | ||
return false; | ||
@@ -224,4 +227,5 @@ } | ||
*/ | ||
rewriteSideEffectRequire(node: Object, parent: Object): boolean { | ||
if (!parent || parent.type !== Syntax.Program) { | ||
rewriteSideEffectRequire(node: Object): boolean { | ||
const { parentNode } = node; | ||
if (!parentNode || parentNode.type !== Syntax.Program) { | ||
return false; | ||
@@ -300,6 +304,7 @@ } | ||
*/ | ||
rewriteExport(node: Object, parent: Object): boolean { | ||
rewriteExport(node: Object): boolean { | ||
return ( | ||
this.rewriteNamedExport(node, parent) || | ||
this.rewriteSingleExportAsDefaultExport(node, parent) | ||
this.rewriteNamespaceExport(node) || | ||
this.rewriteNamedExport(node) || | ||
this.rewriteSingleExportAsDefaultExport(node) | ||
); | ||
@@ -311,3 +316,39 @@ } | ||
*/ | ||
rewriteNamedExport(node: Object, parent: Object): boolean { | ||
rewriteNamespaceExport(node: Object): boolean { | ||
const right = extractModuleExportsSet(node); | ||
if (!right) { | ||
return false; | ||
} | ||
const pathNode = extractRequirePathNode(right); | ||
if (!pathNode) { | ||
return false; | ||
} | ||
this.overwrite( | ||
...node.expression.range, | ||
`export * from ${this.slice(...pathNode.range)}` | ||
); | ||
this.metadata.exports.push({ | ||
type: 'namespace-export', | ||
bindings: [], | ||
node: clone(node) | ||
}); | ||
replace( | ||
node, | ||
{ | ||
type: Syntax.ExportAllDeclaration, | ||
source: pathNode | ||
} | ||
); | ||
} | ||
/** | ||
* @private | ||
*/ | ||
rewriteNamedExport(node: Object): boolean { | ||
if (node.type !== Syntax.ExpressionStatement) { | ||
@@ -331,3 +372,3 @@ return false; | ||
if (parent.type !== Syntax.Program) { | ||
if (node.parentNode.type !== Syntax.Program) { | ||
this.module.warn( | ||
@@ -346,2 +387,3 @@ node, | ||
this.metadata.exports.push({ | ||
type: 'named-export', | ||
bindings: [ | ||
@@ -470,28 +512,8 @@ { | ||
rewriteSingleExportAsDefaultExport(node: Object): boolean { | ||
if (node.type !== Syntax.ExpressionStatement) { | ||
return false; | ||
} | ||
const right = extractModuleExportsSet(node); | ||
const { expression } = node; | ||
if (expression.type !== Syntax.AssignmentExpression) { | ||
if (right === null) { | ||
return false; | ||
} | ||
const { left, right } = expression; | ||
if (left.type !== Syntax.MemberExpression || left.computed) { | ||
return false; | ||
} | ||
const { object, property } = left; | ||
if (object.type !== Syntax.Identifier || object.name !== 'module') { | ||
return false; | ||
} | ||
if (property.type !== Syntax.Identifier || property.name !== 'exports') { | ||
return false; | ||
} | ||
if (right.type === Syntax.ObjectExpression) { | ||
@@ -541,3 +563,3 @@ const bindings = []; | ||
*/ | ||
removeUseStrictDirective(node: Object, parent: Object): boolean { | ||
removeUseStrictDirective(node: Object): boolean { | ||
if (node.type !== Syntax.ExpressionStatement) { | ||
@@ -557,3 +579,3 @@ return false; | ||
if (parent.body[0] !== node) { | ||
if (node.parentNode.body[0] !== node) { | ||
return false; | ||
@@ -573,3 +595,3 @@ } | ||
parent.body.splice(0, 1); | ||
node.parentNode.body.splice(0, 1); | ||
@@ -585,4 +607,4 @@ this.remove(start, end); | ||
export function enter(node: Object, parent: Object, module: Module, context: Context): ?VisitorOption { | ||
if (/Function/.test(node.type) || context.rewrite(node, parent)) { | ||
export function enter(node: Object, module: Module, context: Context): ?VisitorOption { | ||
if (/Function/.test(node.type) || context.rewrite(node)) { | ||
return VisitorOption.Skip; | ||
@@ -625,1 +647,34 @@ } | ||
} | ||
/** | ||
* @private | ||
*/ | ||
function extractModuleExportsSet(node: Object): ?Object { | ||
if (node.type !== Syntax.ExpressionStatement) { | ||
return null; | ||
} | ||
const { expression } = node; | ||
if (expression.type !== Syntax.AssignmentExpression) { | ||
return null; | ||
} | ||
const { left, right } = expression; | ||
if (left.type !== Syntax.MemberExpression || left.computed) { | ||
return null; | ||
} | ||
const { object, property } = left; | ||
if (object.type !== Syntax.Identifier || object.name !== 'module') { | ||
return null; | ||
} | ||
if (property.type !== Syntax.Identifier || property.name !== 'exports') { | ||
return null; | ||
} | ||
return right; | ||
} |
@@ -54,5 +54,5 @@ import BaseContext from '../context'; | ||
export function enter(node: Object, parent: Object, module: Module, context: Context): ?VisitorOption { | ||
export function enter(node: Object, module: Module, context: Context): ?VisitorOption { | ||
context.collapsePropertyToConcise(node); | ||
return null; | ||
} |
@@ -93,3 +93,3 @@ import BaseContext from '../context'; | ||
rewriteAssignment(node: Object, parent: Object): boolean { | ||
rewriteAssignment(node: Object): boolean { | ||
const assignments = this._extractSequentialDestructurableElements([node]); | ||
@@ -101,3 +101,3 @@ | ||
if (parent.type === Syntax.ExpressionStatement) { | ||
if (node.parentNode.type === Syntax.ExpressionStatement) { | ||
// `a = obj.a;` -> `(a = obj.a);` | ||
@@ -252,4 +252,4 @@ // ^ ^ | ||
export function enter(node: Object, parent: Object, module: Module, context: Context): ?VisitorOption { | ||
context.rewriteVariableDeclaration(node) || context.rewriteSequenceExpression(node, parent) || context.rewriteAssignment(node, parent); | ||
export function enter(node: Object, module: Module, context: Context): ?VisitorOption { | ||
context.rewriteVariableDeclaration(node) || context.rewriteSequenceExpression(node) || context.rewriteAssignment(node); | ||
return null; | ||
@@ -256,0 +256,0 @@ } |
@@ -69,5 +69,5 @@ import BaseContext from '../context'; | ||
export function enter(node: Object, parent: Object, module: Module, context: Context): ?VisitorOption { | ||
export function enter(node: Object, module: Module, context: Context): ?VisitorOption { | ||
context.collapsePropertyToConcise(node); | ||
return null; | ||
} |
@@ -167,3 +167,3 @@ import BaseContext from '../context'; | ||
export function enter(node: Object, parent: Object, module: Module, context: Context): ?VisitorOption { | ||
export function enter(node: Object, module: Module, context: Context): ?VisitorOption { | ||
const parts = context.flatten(node); | ||
@@ -170,0 +170,0 @@ |
@@ -0,3 +1,6 @@ | ||
import estraverse from 'estraverse'; | ||
import type { Reference, ScopeManager } from 'escope'; | ||
const { Syntax } = estraverse; | ||
type DeclarationKind = 'var' | 'let' | 'const'; | ||
@@ -29,2 +32,6 @@ | ||
function referenceCouldBeBlockScope(reference: Reference): boolean { | ||
return referenceAfterDefinition(reference) && referenceInDefinitionParentBlock(reference); | ||
} | ||
function referenceAfterDefinition(reference: Reference): boolean { | ||
const referenceIndex = reference.identifier.range[0]; | ||
@@ -34,1 +41,26 @@ const definitionIndex = reference.resolved.identifiers[0].range[0]; | ||
} | ||
function referenceInDefinitionParentBlock(reference: Reference): boolean { | ||
let definitionName = reference.resolved.defs[0].name; | ||
let defBlock = definitionName; | ||
while (defBlock && defBlock.type !== Syntax.BlockStatement && defBlock.type !== Syntax.Program) { | ||
defBlock = defBlock.parentNode; | ||
} | ||
if (!defBlock) { | ||
const { line, column } = definitionName.loc.start; | ||
throw new Error( | ||
`BUG: Expected a block containing '${definitionName.name}'` + | ||
`(${line}:${column + 1}) but did not find one.` | ||
); | ||
} | ||
let refBlock = reference.identifier; | ||
while (refBlock && refBlock !== defBlock) { | ||
refBlock = refBlock.parentNode; | ||
} | ||
return refBlock === defBlock; | ||
} |
@@ -5,8 +5,8 @@ import estraverse from 'estraverse'; | ||
export default function needsParens(node: Object, parent: Object): boolean { | ||
export default function needsParens(node: Object): boolean { | ||
switch (node.type) { | ||
case Syntax.ArrowFunctionExpression: | ||
switch (parent.type) { | ||
switch (node.parentNode.type) { | ||
case Syntax.MemberExpression: | ||
return parent.object === node; | ||
return node.parentNode.object === node; | ||
@@ -13,0 +13,0 @@ case Syntax.BinaryExpression: |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
217918
6480