Skip to content

Commit f0df203

Browse files
committed
Replace destructurings for Node 4 support without Babel
1 parent 5adb01c commit f0df203

File tree

4 files changed

+48
-58
lines changed

4 files changed

+48
-58
lines changed

src/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { dirname, join } from 'path';
44
import { normalizeOptions } from './lib/utils/normalize-options';
55

66
function macros(babel) {
7-
const { types: t } = babel;
7+
const t = babel.types;
8+
89
let macroBuilder;
910
let options;
1011

@@ -23,11 +24,9 @@ function macros(babel) {
2324
if (item.isImportDeclaration()) {
2425
let importPath = item.node.source.value;
2526

26-
let {
27-
featureSources,
28-
debugTools: { debugToolsImport },
29-
envFlags: { envFlagsImport, flags }
30-
} = options;
27+
let featureSources = options.featureSources;
28+
let debugToolsImport = options.debugTools.debugToolsImport;
29+
let envFlagsImport = options.envFlags.envFlagsImport;
3130

3231
let isFeaturesImport = featureSources.indexOf(importPath) > -1;
3332

src/lib/utils/builder.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,20 @@ export default class Builder {
8282
_createMacroExpression(path, _options) {
8383
let options = _options || {};
8484

85-
let { t, module, global } = this;
85+
let t = this.t;
8686
let expression = path.node.expression;
87-
let { callee, arguments: args } = expression;
88-
let callExpression;
87+
let callee = expression.callee;
88+
let args = expression.arguments;
8989

9090
if (options.validate) {
9191
options.validate(expression, args);
9292
}
9393

94-
if (module || global) {
95-
if (global) {
96-
callExpression = this._createGlobalExternalHelper(callee, args, global);
97-
} else {
98-
callExpression = expression;
99-
}
94+
let callExpression;
95+
if (this.module) {
96+
callExpression = expression;
97+
} else if (this.global) {
98+
callExpression = this._createGlobalExternalHelper(callee, args, this.global);
10099
} else if (options.buildConsoleAPI) {
101100
callExpression = options.buildConsoleAPI(expression, args);
102101
} else {
@@ -145,13 +144,13 @@ export default class Builder {
145144
predicate: (expression, args) => args[1],
146145

147146
buildConsoleAPI: (expression, args) => {
148-
let [message] = args;
147+
let message = args[0];
149148

150149
return this._createConsoleAPI(this.t.identifier('warn'), [message]);
151150
},
152151

153152
validate: (expression, args) => {
154-
let [ , , meta ] = args;
153+
let meta = args[2];
155154

156155
if (meta && meta.properties && !meta.properties.some( prop => prop.key.name === 'id')) {
157156
throw new ReferenceError(`deprecate's meta information requires an "id" field.`);
@@ -168,10 +167,12 @@ export default class Builder {
168167
* Performs the actually expansion of macros
169168
*/
170169
expandMacros(debugFlag) {
171-
let { t } = this;
170+
let t = this.t;
172171
let flag = t.booleanLiteral(debugFlag);
173172
for (let i = 0; i < this.expressions.length; i++) {
174-
let [exp, logicalExp] = this.expressions[i];
173+
let expression = this.expressions[i];
174+
let exp = expression[0];
175+
let logicalExp = expression[1];
175176
exp.replaceWith(t.parenthesizedExpression(logicalExp(flag)));
176177
}
177178
}
@@ -181,17 +182,17 @@ export default class Builder {
181182
}
182183

183184
_createGlobalExternalHelper(identifier, args, ns) {
184-
let { t } = this;
185+
let t = this.t;
185186
return t.callExpression(t.memberExpression(t.identifier(ns), identifier), args);
186187
}
187188

188189
_createConsoleAPI(identifier, args) {
189-
let { t } = this;
190+
let t = this.t;
190191
return t.callExpression(t.memberExpression(t.identifier('console'), identifier), args);
191192
}
192193

193194
_buildLogicalExpressions(identifiers, callExpression) {
194-
let { t } = this;
195+
let t = this.t;
195196

196197
return (debugIdentifier) => {
197198
identifiers.unshift(debugIdentifier);

src/lib/utils/macros.js

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ export default class Macros {
3131
*/
3232
expand(path) {
3333
let debugBinding = path.scope.getBinding(DEBUG);
34-
let { builder, envFlags } = this;
35-
3634

3735
if (this._hasDebugModule(debugBinding)) {
3836
debugBinding.path.parentPath.remove();
@@ -41,12 +39,12 @@ export default class Macros {
4139
this._inlineFeatureFlags(path);
4240
this._inlineSvelteFlags(path);
4341
this._inlineEnvFlags(path)
44-
this.builder.expandMacros(envFlags.DEBUG);
42+
this.builder.expandMacros(this.envFlags.DEBUG);
4543
this._cleanImports(path);
4644
}
4745

4846
_inlineFeatureFlags(path) {
49-
let { envFlags, builder, featureFlags, featuresMap } = this;
47+
let featuresMap = this.featuresMap;
5048

5149
if (this.envFlags.DEBUG) { return; }
5250
Object.keys(featuresMap).forEach((source) => {
@@ -56,7 +54,7 @@ export default class Macros {
5654

5755
if (binding && flagValue !== null) {
5856
binding.referencePaths.forEach(referencePath => {
59-
referencePath.replaceWith(builder.t.booleanLiteral(flagValue));
57+
referencePath.replaceWith(this.builder.t.booleanLiteral(flagValue));
6058
});
6159

6260
if (binding.path.parentPath.isImportDeclaration()) {
@@ -68,38 +66,42 @@ export default class Macros {
6866
}
6967

7068
_inlineEnvFlags(path) {
71-
let { envFlags, builder } = this;
69+
let envFlags = this.envFlags;
70+
7271
Object.keys(envFlags).forEach(flag => {
7372
let binding = path.scope.getBinding(flag);
7473
if (binding &&
7574
binding.path.isImportSpecifier() &&
7675
binding.path.parent.source.value === this.envFlagsSource) {
7776

78-
binding.referencePaths.forEach(p => p.replaceWith(builder.t.booleanLiteral(envFlags[flag])));
77+
binding.referencePaths.forEach(p => p.replaceWith(this.builder.t.booleanLiteral(envFlags[flag])));
7978
}
8079
});
8180
}
8281

8382
_inlineSvelteFlags(path) {
84-
let { svelteMap, envFlags, builder } = this;
83+
let svelteMap = this.svelteMap;
84+
let envFlags = this.envFlags;
85+
let builder = this.builder;
86+
8587
let sources = Object.keys(svelteMap);
8688
sources.forEach((source) => {
8789
Object.keys(svelteMap[source]).forEach((flag) => {
8890
let binding = path.scope.getBinding(flag);
8991
if (binding !== undefined) {
9092
binding.referencePaths.forEach((p) => {
93+
let t = builder.t;
9194
if (envFlags.DEBUG) {
9295
if (svelteMap[source][flag] === false) {
93-
let { t } = builder;
9496
if (!p.parentPath.isIfStatement()) { return; }
9597
let consequent = p.parentPath.get('consequent');
96-
consequent.unshiftContainer('body', builder.t.throwStatement(
98+
consequent.unshiftContainer('body', t.throwStatement(
9799
t.newExpression(t.identifier('Error'), [t.stringLiteral(`You indicated you don't have any deprecations, however you are relying on ${flag}.`)])
98100
));
99101
}
100102
} else {
101103
if (p.parentPath.isIfStatement()) {
102-
p.replaceWith(builder.t.booleanLiteral(svelteMap[source][flag]));
104+
p.replaceWith(t.booleanLiteral(svelteMap[source][flag]));
103105
}
104106
}
105107
});
@@ -130,8 +132,8 @@ export default class Macros {
130132
*/
131133
build(path) {
132134
let expression = path.node.expression;
133-
let { builder, localDebugBindings } = this;
134-
if (builder.t.isCallExpression(expression) && localDebugBindings.some((b) => b.node.name === expression.callee.name)) {
135+
136+
if (this.builder.t.isCallExpression(expression) && this.localDebugBindings.some((b) => b.node.name === expression.callee.name)) {
135137
let imported = path.scope.getBinding(expression.callee.name).path.node.imported.name;
136138
this.builder[`${imported}`](path);
137139
}
@@ -152,31 +154,23 @@ export default class Macros {
152154
}
153155

154156
_detectForeignFeatureFlag(specifiers, source) {
155-
let { featuresMap } = this;
156157
specifiers.forEach((specifier) => {
157-
if (specifier.imported && featuresMap[source][specifier.imported.name] !== null) {
158+
if (specifier.imported && this.featuresMap[source][specifier.imported.name] !== null) {
158159
throw new Error(`Imported ${specifier.imported.name} from ${source} which is not a supported flag.`);
159160
}
160161
});
161162
}
162163

163164
_cleanImports(path) {
164-
let {
165-
debugHelpers,
166-
builder,
167-
featureFlags,
168-
featureSources
169-
} = this;
170-
171165
let body = path.get('body');
172166

173167
if (!this.envFlags.DEBUG) {
174168
for (let i = 0; i < body.length; i++) {
175169
let decl = body[i];
176170

177-
if (builder.t.isImportDeclaration(decl)) {
171+
if (this.builder.t.isImportDeclaration(decl)) {
178172
let source = decl.node.source.value;
179-
if (featureSources.indexOf(source) > -1) {
173+
if (this.featureSources.indexOf(source) > -1) {
180174
if (decl.node.specifiers.length > 0) {
181175
this._detectForeignFeatureFlag(decl.node.specifiers, source);
182176
} else {
@@ -188,7 +182,7 @@ export default class Macros {
188182
}
189183
}
190184

191-
if (!debugHelpers.module) {
185+
if (!this.debugHelpers.module) {
192186
if (this.localDebugBindings.length > 0) {
193187
this.localDebugBindings[0].parentPath.parentPath
194188
let importPath = this.localDebugBindings[0].findParent(p => p.isImportDeclaration());

src/lib/utils/normalize-options.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { satisfies } from 'semver';
22

33
export function normalizeOptions(options) {
4-
let {
5-
features = [],
6-
debugTools,
7-
envFlags,
8-
externalizeHelpers,
9-
svelte
10-
} = options;
4+
let features = options.features || [];
5+
let debugTools = options.debugTools;
6+
let envFlags = options.envFlags;
7+
let externalizeHelpers = options.externalizeHelpers;
8+
let svelte = options.svelte;
119

1210
let featureSources = [];
1311
let featuresMap = {};
@@ -52,10 +50,8 @@ export function normalizeOptions(options) {
5250
throw new Error('You must specify `debugTools.source`');
5351
}
5452

55-
let {
56-
source: debugToolsImport,
57-
assertPredicateIndex
58-
} = debugTools;
53+
let debugToolsImport = debugTools.source;
54+
let assertPredicateIndex = debugTools.assertPredicateIndex;
5955

6056
let envFlagsImport;
6157
let _envFlags = {};

0 commit comments

Comments
 (0)