add-variable-declarations
Advanced tools
Comparing version
@@ -21,2 +21,6 @@ "use strict"; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
@@ -195,2 +199,6 @@ mod | ||
} | ||
/** | ||
* Declare that there is an assignment to a variable with this name in this | ||
* scope. | ||
*/ | ||
addBinding(name) { | ||
@@ -204,2 +212,6 @@ let searchResult = this.resolveName(name); | ||
} | ||
/** | ||
* Note that this identifier appears in this scope. This won't create | ||
* bindings, but might update the most specific scope for existing bindings. | ||
*/ | ||
handleSeenIdentifier(name) { | ||
@@ -211,2 +223,13 @@ let searchResult = this.resolveName(name); | ||
} | ||
/** | ||
* Declare that, if possible, the given names should all have declarations | ||
* added by inserting `var` at the start of the specified node. If any of them | ||
* end up changing scopes due to later information, or if any of them are | ||
* already declared, we'll just add the names to the most specific scope. | ||
* | ||
* To make calling code simpler, this method allows an empty array of names | ||
* (in which case it's a no-op) and allows names that are already defined | ||
* (in which case we immediately know that we won't be able to do an inline | ||
* binding). | ||
*/ | ||
addInlineBinding(node, names, { shouldRemoveParens }) { | ||
@@ -272,2 +295,7 @@ if (names.length === 0) { | ||
} | ||
/** | ||
* When we finish processing a function, we know that we have all information | ||
* we need for variables scoped to this function, so we can insert the `var` | ||
* declarations at the right places. | ||
*/ | ||
commitDeclarations(editor, source, tokens) { | ||
@@ -305,2 +333,6 @@ let usedNames = /* @__PURE__ */ new Set(); | ||
} | ||
/** | ||
* Get all names that we still need to declare (ones not in usedNames), sorted | ||
* and grouped by scope. | ||
*/ | ||
getBindingNamesByScope(usedNames) { | ||
@@ -353,2 +385,8 @@ let bindingNamesByScope = /* @__PURE__ */ new Map(); | ||
(0, import_traverse.default)(ast, { | ||
/** | ||
* Adds `var` for assignments, either in place or at the top of the scope. | ||
* | ||
* a = 1; // can add `var` inline | ||
* b(c = 2); // needs standalone `var` at the top of scope | ||
*/ | ||
AssignmentExpression(path) { | ||
@@ -373,2 +411,7 @@ let { node } = path; | ||
}, | ||
/** | ||
* We want to declare each variable at its most specific scope across all | ||
* assignments and usages, so note each usage, since it might affect that | ||
* scope. | ||
*/ | ||
Identifier(path) { | ||
@@ -378,4 +421,23 @@ let state2 = getState(); | ||
}, | ||
/** | ||
* Adds `var` to `for-in` and `for-of` loops, e.g. | ||
* | ||
* for (key in object) { | ||
* … | ||
* } | ||
* | ||
* for (item of list) { | ||
* … | ||
* } | ||
*/ | ||
ForInStatement: visitForStatement, | ||
ForOfStatement: visitForStatement, | ||
/** | ||
* Optimizes for the case where there are multiple assignments in one | ||
* sequence of expressions, e.g. | ||
* | ||
* for (i = 0, length = list.length; i < length; i++) { | ||
* … | ||
* } | ||
*/ | ||
SequenceExpression(path) { | ||
@@ -382,0 +444,0 @@ let state2 = getState(); |
{ | ||
"name": "add-variable-declarations", | ||
"version": "6.0.0", | ||
"version": "6.0.1", | ||
"description": "Add variable declarations for global assignments.", | ||
@@ -44,3 +44,3 @@ "main": "dist/index.js", | ||
"@types/jest": "^29.2.3", | ||
"@types/node": "^14.0.0", | ||
"@types/node": "^18.0.0", | ||
"jest": "^29.3.1", | ||
@@ -58,3 +58,3 @@ "ts-jest": "^29.0.3", | ||
"resolutions": { | ||
"**/@babel/types": "7.20.5" | ||
"**/@babel/types": "7.21.3" | ||
}, | ||
@@ -61,0 +61,0 @@ "publishConfig": { |
Sorry, the diff of this file is not supported yet
34408
14.15%10858
12.95%953
14.41%0
-100%