@resugar/codemod-declarations-block-scope
Advanced tools
Comparing version
{ | ||
"name": "@resugar/codemod-declarations-block-scope", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Convert 'var' declarations to 'let' or 'const' as appropriate.", | ||
"keywords": [], | ||
"bugs": "https://github.com/resugar/resugar/issues", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/resugar/resugar.git" | ||
}, | ||
"license": "MIT", | ||
"author": "Brian Donovan", | ||
"main": "src/index.js", | ||
"files": [ | ||
"src/**/*.{js,d.ts,js.map}" | ||
], | ||
"scripts": { | ||
@@ -10,15 +21,2 @@ "prepare": "tsc", | ||
}, | ||
"files": [ | ||
"src/**/*.js", | ||
"src/**/*.d.ts", | ||
"src/**/*.js.map" | ||
], | ||
"keywords": [], | ||
"author": "Brian Donovan", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/resugar/resugar.git" | ||
}, | ||
"bugs": "https://github.com/resugar/resugar/issues", | ||
"devDependencies": { | ||
@@ -29,3 +27,4 @@ "@resugar/test-runner": "*" | ||
"access": "public" | ||
} | ||
}, | ||
"gitHead": "cf1f00a165f5c7976c9ee3680bdf449557845eb0" | ||
} |
@@ -8,2 +8,2 @@ import { NodePath } from '@babel/traverse'; | ||
} | ||
export default function ({ types: t }: typeof Babel): Babel.PluginObj; | ||
export default function (): Babel.PluginItem; |
@@ -7,3 +7,3 @@ "use strict"; | ||
const mostRestrictiveKindForDeclaration_1 = __importDefault(require("./mostRestrictiveKindForDeclaration")); | ||
function default_1({ types: t }) { | ||
function default_1() { | ||
return { | ||
@@ -29,4 +29,4 @@ name: '@resugar/codemod-declarations-block-scope', | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
@@ -33,0 +33,0 @@ } |
"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)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
@@ -19,6 +31,14 @@ }; | ||
let { scope } = path; | ||
let isConst = path.node.declarations.every(declaration => !!declaration.init); | ||
let isConst = path.node.declarations.every((declaration) => !!declaration.init); | ||
if (t.isSwitchCase(path.parent)) { | ||
return 'var'; | ||
} | ||
if (t.isConditional(path.parent) && | ||
(path.parent.consequent === path.node || | ||
path.parent.alternate === path.node)) { | ||
return 'var'; | ||
} | ||
if (t.isLoop(path.parent) && path.parent.body === path.node) { | ||
return 'var'; | ||
} | ||
for (let id in ids) { | ||
@@ -43,8 +63,8 @@ let binding = scope.getBinding(id); | ||
// Are there duplicate declarations? | ||
if (binding.constantViolations.some(path => t.isVariableDeclarator(path.node))) { | ||
if (binding.constantViolations.some((path) => t.isVariableDeclarator(path.node))) { | ||
return false; | ||
} | ||
let definition = binding.path; | ||
let definitionBlockParent = definition.findParent(path => path.isBlockParent()); | ||
if ([...binding.referencePaths, ...binding.constantViolations].some(reference => | ||
let definitionBlockParent = definition.findParent((path) => path.isBlockParent()); | ||
if ([...binding.referencePaths, ...binding.constantViolations].some((reference) => | ||
// Does this reference come before the definition? | ||
@@ -61,3 +81,3 @@ reference.node.start < definition.node.start || | ||
let functionParent = definition.getFunctionParent(); | ||
let loopParent = definition.findParent(path => path.isLoop()); | ||
let loopParent = definition.findParent((path) => path.isLoop()); | ||
// Is this declaration within a loop in the current function scope? | ||
@@ -67,3 +87,3 @@ if (loopParent !== null && | ||
// Is any reference within a closure? | ||
if (binding.referencePaths.some(reference => reference.getFunctionParent() !== functionParent)) { | ||
if (binding.referencePaths.some((reference) => reference.getFunctionParent() !== functionParent)) { | ||
return false; | ||
@@ -94,3 +114,3 @@ } | ||
// Loop assignees are always initialized before use. | ||
let loopParent = binding.path.findParent(path => path.isLoop()); | ||
let loopParent = binding.path.findParent((path) => path.isLoop()); | ||
if (loopParent !== null) { | ||
@@ -111,5 +131,5 @@ if (loopParent.isForOfStatement() || loopParent.isForInStatement()) { | ||
// code paths assign to this variable, but this should get the common case. | ||
let blockParent = binding.path.findParent(path => path.isBlockParent()); | ||
let earliestUsage = Math.min(...binding.referencePaths.map(reference => reference.node.start)); | ||
if (binding.constantViolations.some(path => path.node.end < earliestUsage && | ||
let blockParent = binding.path.findParent((path) => path.isBlockParent()); | ||
let earliestUsage = Math.min(...binding.referencePaths.map((reference) => reference.node.start)); | ||
if (binding.constantViolations.some((path) => path.node.end < earliestUsage && | ||
path.isAssignmentExpression() && | ||
@@ -116,0 +136,0 @@ path.parentPath.isExpressionStatement() && |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14573
8.95%197
11.3%