-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyplugin__define_var.cjs
More file actions
33 lines (31 loc) · 1.34 KB
/
myplugin__define_var.cjs
File metadata and controls
33 lines (31 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module.exports = function({ types: t }) {
return {
visitor: {
AssignmentExpression(path, state) {
const { addVar = false, useWindow = true } = state.opts;
if (t.isIdentifier(path.node.left)) {
const binding = path.scope.getBinding(path.node.left.name);
if (!binding) {
if (addVar) {
// Trasforma l'assegnazione in una dichiarazione "var"
const declaration = t.variableDeclaration("var", [
t.variableDeclarator(
t.identifier(path.node.left.name),
path.node.right
),
]);
path.replaceWith(declaration);
} else if (useWindow) {
// Trasforma il lato sinistro in "window.<nomeVariabile>"
const newLeft = t.memberExpression(
t.identifier("window"),
t.identifier(path.node.left.name)
);
path.node.left = newLeft;
}
}
}
},
},
};
};