|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Sebastian Beltran @bjohansebas |
| 4 | +*/ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +const CompatibilityPlugin = require("../../javascript/CompatibilityPlugin"); |
| 9 | +const ESMExportBindingDependency = require("./ESMExportBindingDependency"); |
| 10 | +const { harmonySpecifierTag } = require("./ESMImportDependencyParserPlugin"); |
| 11 | + |
| 12 | +/** @import JavascriptParser, { Range, WriteStatement } from "../../javascript/JavascriptParser" */ |
| 13 | +/** @import { Expression, Pattern, AssignmentExpression, Node } from "estree" */ |
| 14 | +/** @import { BindingWrite } from "./ESMExportBindingDependency" */ |
| 15 | + |
| 16 | +const PLUGIN_NAME = "ESMExportBindingParserPlugin"; |
| 17 | +const bindingTag = Symbol(PLUGIN_NAME); |
| 18 | + |
| 19 | +class ESMExportBindingParserPlugin { |
| 20 | + /** |
| 21 | + * @param {JavascriptParser} parser parser |
| 22 | + * @returns {void} |
| 23 | + */ |
| 24 | + apply(parser) { |
| 25 | + /** @type {ESMExportBindingDependency | undefined} */ |
| 26 | + let dependency; |
| 27 | + /** @type {WeakSet<Node>} */ |
| 28 | + let seen; |
| 29 | + parser.hooks.program.tap(PLUGIN_NAME, () => { |
| 30 | + dependency = undefined; |
| 31 | + seen = new WeakSet(); |
| 32 | + }); |
| 33 | + /** |
| 34 | + * @param {string} id local name |
| 35 | + * @param {string} name export name |
| 36 | + * @returns {void} |
| 37 | + */ |
| 38 | + const tag = (id, name) => { |
| 39 | + if (parser.getTagData(id, harmonySpecifierTag)) return; |
| 40 | + const names = /** @type {string[] | undefined} */ ( |
| 41 | + parser.getTagData(id, bindingTag) |
| 42 | + ); |
| 43 | + if (names) names.push(name); |
| 44 | + else parser.tagVariable(id, bindingTag, [name]); |
| 45 | + }; |
| 46 | + parser.hooks.exportSpecifier.tap( |
| 47 | + { name: PLUGIN_NAME, stage: -100 }, |
| 48 | + (_statement, id, name) => { |
| 49 | + tag(id, name); |
| 50 | + } |
| 51 | + ); |
| 52 | + parser.hooks.exportExpression.tap( |
| 53 | + { name: PLUGIN_NAME, stage: -100 }, |
| 54 | + (_statement, expression) => { |
| 55 | + if ( |
| 56 | + (expression.type === "FunctionDeclaration" || |
| 57 | + expression.type === "ClassDeclaration") && |
| 58 | + expression.id |
| 59 | + ) { |
| 60 | + tag(expression.id.name, "default"); |
| 61 | + } |
| 62 | + } |
| 63 | + ); |
| 64 | + /** |
| 65 | + * @param {BindingWrite} write write |
| 66 | + * @returns {void} |
| 67 | + */ |
| 68 | + const add = (write) => { |
| 69 | + const renamed = |
| 70 | + /** @type {import("../../javascript/CompatibilityPlugin").CompatibilitySettings | undefined} */ ( |
| 71 | + parser.getTagData( |
| 72 | + write.id, |
| 73 | + CompatibilityPlugin.nestedWebpackIdentifierTag |
| 74 | + ) |
| 75 | + ); |
| 76 | + if (renamed) { |
| 77 | + write.originalName = write.id; |
| 78 | + write.id = renamed.name; |
| 79 | + } |
| 80 | + if (!dependency) { |
| 81 | + dependency = new ESMExportBindingDependency([]); |
| 82 | + parser.state.module.addDependency(dependency); |
| 83 | + } |
| 84 | + dependency.writes.push(write); |
| 85 | + }; |
| 86 | + /** |
| 87 | + * @param {Pattern | Expression} target target |
| 88 | + * @param {boolean=} shorthand shorthand |
| 89 | + * @param {Range=} defaultRange anonymous default |
| 90 | + * @returns {void} |
| 91 | + */ |
| 92 | + const pattern = (target, shorthand = false, defaultRange = undefined) => { |
| 93 | + switch (target.type) { |
| 94 | + case "Identifier": { |
| 95 | + const names = /** @type {string[] | undefined} */ ( |
| 96 | + parser.getTagData(target.name, bindingTag) |
| 97 | + ); |
| 98 | + if (names) { |
| 99 | + add({ |
| 100 | + id: target.name, |
| 101 | + names, |
| 102 | + range: /** @type {Range} */ (target.range), |
| 103 | + kind: "pattern", |
| 104 | + shorthand, |
| 105 | + defaultRange |
| 106 | + }); |
| 107 | + } |
| 108 | + break; |
| 109 | + } |
| 110 | + case "ArrayPattern": |
| 111 | + for (const element of target.elements) if (element) pattern(element); |
| 112 | + break; |
| 113 | + case "ObjectPattern": |
| 114 | + for (const property of target.properties) { |
| 115 | + if (property.type === "RestElement") pattern(property.argument); |
| 116 | + else pattern(property.value, property.shorthand); |
| 117 | + } |
| 118 | + break; |
| 119 | + case "RestElement": |
| 120 | + pattern(target.argument); |
| 121 | + break; |
| 122 | + case "AssignmentPattern": { |
| 123 | + const value = target.right; |
| 124 | + const anonymous = |
| 125 | + value.type === "ArrowFunctionExpression" || |
| 126 | + ((value.type === "FunctionExpression" || |
| 127 | + value.type === "ClassExpression") && |
| 128 | + !value.id); |
| 129 | + pattern( |
| 130 | + target.left, |
| 131 | + shorthand, |
| 132 | + anonymous ? /** @type {Range} */ (value.range) : undefined |
| 133 | + ); |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + }; |
| 138 | + /** |
| 139 | + * @param {AssignmentExpression | WriteStatement} expression write |
| 140 | + * @returns {void} |
| 141 | + */ |
| 142 | + const record = (expression) => { |
| 143 | + if (seen.has(expression)) return; |
| 144 | + seen.add(expression); |
| 145 | + const target = |
| 146 | + expression.type === "UpdateExpression" |
| 147 | + ? expression.argument |
| 148 | + : expression.left; |
| 149 | + if (target.type === "VariableDeclaration") return; |
| 150 | + if ( |
| 151 | + target.type === "Identifier" && |
| 152 | + (expression.type === "AssignmentExpression" || |
| 153 | + expression.type === "UpdateExpression") |
| 154 | + ) { |
| 155 | + const names = /** @type {string[] | undefined} */ ( |
| 156 | + parser.getTagData(target.name, bindingTag) |
| 157 | + ); |
| 158 | + if (names) { |
| 159 | + add({ |
| 160 | + id: target.name, |
| 161 | + names, |
| 162 | + range: /** @type {Range} */ (expression.range), |
| 163 | + kind: |
| 164 | + expression.type === "UpdateExpression" && !expression.prefix |
| 165 | + ? "postfix" |
| 166 | + : "expression", |
| 167 | + shorthand: false |
| 168 | + }); |
| 169 | + } |
| 170 | + } else { |
| 171 | + pattern(target); |
| 172 | + } |
| 173 | + }; |
| 174 | + parser.hooks.finish.tap(PLUGIN_NAME, () => { |
| 175 | + if (dependency) dependency.writes.sort((a, b) => a.range[0] - b.range[0]); |
| 176 | + }); |
| 177 | + parser.hooks.assignment.tap(PLUGIN_NAME, record); |
| 178 | + parser.hooks.assign.for(bindingTag).tap(PLUGIN_NAME, (expression) => { |
| 179 | + // A destructuring write must not redefine an outer binding in this scope. |
| 180 | + if (expression.left.type.endsWith("Pattern")) return true; |
| 181 | + }); |
| 182 | + parser.hooks.write.for(bindingTag).tap(PLUGIN_NAME, (expression) => { |
| 183 | + record(expression); |
| 184 | + }); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +module.exports = ESMExportBindingParserPlugin; |
0 commit comments