Skip to content

Commit b5f3d00

Browse files
author
Robert Jackson
committed
eslint --fix .
1 parent b37d3c1 commit b5f3d00

File tree

7 files changed

+466
-377
lines changed

7 files changed

+466
-377
lines changed

src/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@ function macros(babel) {
1111
let options;
1212

1313
return {
14-
name: "babel-feature-flags-and-debug-macros",
14+
name: 'babel-feature-flags-and-debug-macros',
1515
visitor: {
16-
1716
Program: {
1817
enter(path, state) {
1918
options = normalizeOptions(state.opts);
2019
this.macroBuilder = new Macros(t, options);
2120

2221
let body = path.get('body');
2322

24-
body.forEach((item) => {
23+
body.forEach(item => {
2524
if (item.isImportDeclaration()) {
2625
let importPath = item.node.source.value;
2726

@@ -33,28 +32,28 @@ function macros(babel) {
3332

3433
if (debugToolsImport && debugToolsImport === importPath) {
3534
this.macroBuilder.collectDebugToolsSpecifiers(item.get('specifiers'));
36-
} if (envFlagsImport && envFlagsImport === importPath) {
35+
}
36+
if (envFlagsImport && envFlagsImport === importPath) {
3737
this.macroBuilder.collectEnvFlagSpecifiers(item.get('specifiers'));
3838
}
3939
}
4040
});
41-
4241
},
4342

4443
exit(path) {
4544
this.macroBuilder.expand(path);
46-
}
45+
},
4746
},
4847

4948
ExpressionStatement(path) {
5049
this.macroBuilder.build(path);
51-
}
52-
}
50+
},
51+
},
5352
};
5453
}
5554

5655
macros.baseDir = function() {
5756
return path.dirname(__dirname);
58-
}
57+
};
5958

6059
module.exports = macros;

src/utils/builder.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = class Builder {
3535
}
3636

3737
this._createMacroExpression(path, {
38-
predicate
38+
predicate,
3939
});
4040
}
4141

@@ -113,7 +113,10 @@ module.exports = class Builder {
113113
prefixedIdentifiers.push(negatedPredicate);
114114
}
115115

116-
this.expressions.push([path, this._buildLogicalExpressions(prefixedIdentifiers, callExpression)]);
116+
this.expressions.push([
117+
path,
118+
this._buildLogicalExpressions(prefixedIdentifiers, callExpression),
119+
]);
117120
}
118121

119122
/**
@@ -154,14 +157,14 @@ module.exports = class Builder {
154157
validate: (expression, args) => {
155158
let meta = args[2];
156159

157-
if (meta && meta.properties && !meta.properties.some( prop => prop.key.name === 'id')) {
160+
if (meta && meta.properties && !meta.properties.some(prop => prop.key.name === 'id')) {
158161
throw new ReferenceError(`deprecate's meta information requires an "id" field.`);
159162
}
160163

161-
if (meta && meta.properties && !meta.properties.some(prop => prop.key.name === 'until')) {
164+
if (meta && meta.properties && !meta.properties.some(prop => prop.key.name === 'until')) {
162165
throw new ReferenceError(`deprecate's meta information requires an "until" field.`);
163166
}
164-
}
167+
},
165168
});
166169
}
167170

@@ -180,7 +183,7 @@ module.exports = class Builder {
180183
}
181184

182185
_getIdentifiers(args) {
183-
return args.filter((arg) => this.t.isIdentifier(arg));
186+
return args.filter(arg => this.t.isIdentifier(arg));
184187
}
185188

186189
_createGlobalExternalHelper(identifier, args, ns) {
@@ -196,22 +199,22 @@ module.exports = class Builder {
196199
_buildLogicalExpressions(identifiers, callExpression) {
197200
let t = this.t;
198201

199-
return (debugIdentifier) => {
202+
return debugIdentifier => {
200203
identifiers.unshift(debugIdentifier);
201204
identifiers.push(callExpression);
202205
let logicalExpressions;
203206

204-
for (let i = 0; i < identifiers.length; i++) {
205-
let left = identifiers[i];
206-
let right = identifiers[i + 1];
207-
if (!logicalExpressions) {
208-
logicalExpressions = t.logicalExpression('&&', left, right);
209-
} else if (right) {
210-
logicalExpressions = t.logicalExpression('&&', logicalExpressions, right)
211-
}
207+
for (let i = 0; i < identifiers.length; i++) {
208+
let left = identifiers[i];
209+
let right = identifiers[i + 1];
210+
if (!logicalExpressions) {
211+
logicalExpressions = t.logicalExpression('&&', left, right);
212+
} else if (right) {
213+
logicalExpressions = t.logicalExpression('&&', logicalExpressions, right);
212214
}
215+
}
213216

214217
return logicalExpressions;
215-
}
218+
};
216219
}
217-
}
220+
};

src/utils/macros.js

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = class Macros {
2222
this.builder = new Builder(t, {
2323
module: this.debugHelpers.module,
2424
global: this.debugHelpers.global,
25-
assertPredicateIndex: options.debugTools.assertPredicateIndex
25+
assertPredicateIndex: options.debugTools.assertPredicateIndex,
2626
});
2727
}
2828

@@ -35,7 +35,7 @@ module.exports = class Macros {
3535

3636
this._inlineFeatureFlags(path);
3737
this._inlineSvelteFlags(path);
38-
this._inlineEnvFlags(path)
38+
this._inlineEnvFlags(path);
3939
this.builder.expandMacros(this.envFlags.DEBUG);
4040

4141
if (this._hasDebugModule(debugBinding)) {
@@ -48,9 +48,11 @@ module.exports = class Macros {
4848
_inlineFeatureFlags(path) {
4949
let featuresMap = this.featuresMap;
5050

51-
if (this.envFlags.DEBUG) { return; }
52-
Object.keys(featuresMap).forEach((source) => {
53-
Object.keys(featuresMap[source]).forEach((flag) => {
51+
if (this.envFlags.DEBUG) {
52+
return;
53+
}
54+
Object.keys(featuresMap).forEach(source => {
55+
Object.keys(featuresMap[source]).forEach(flag => {
5456
let flagValue = featuresMap[source][flag];
5557
let binding = path.scope.getBinding(flag);
5658

@@ -71,13 +73,16 @@ module.exports = class Macros {
7173
let envFlags = this.envFlags;
7274

7375
Object.keys(envFlags).forEach(flag => {
74-
let binding = path.scope.getBinding(flag);
75-
if (binding &&
76-
binding.path.isImportSpecifier() &&
77-
binding.path.parent.source.value === this.envFlagsSource) {
78-
79-
binding.referencePaths.forEach(p => p.replaceWith(this.builder.t.booleanLiteral(envFlags[flag])));
80-
}
76+
let binding = path.scope.getBinding(flag);
77+
if (
78+
binding &&
79+
binding.path.isImportSpecifier() &&
80+
binding.path.parent.source.value === this.envFlagsSource
81+
) {
82+
binding.referencePaths.forEach(p =>
83+
p.replaceWith(this.builder.t.booleanLiteral(envFlags[flag]))
84+
);
85+
}
8186
});
8287
}
8388

@@ -87,19 +92,28 @@ module.exports = class Macros {
8792
let builder = this.builder;
8893

8994
let sources = Object.keys(svelteMap);
90-
sources.forEach((source) => {
91-
Object.keys(svelteMap[source]).forEach((flag) => {
95+
sources.forEach(source => {
96+
Object.keys(svelteMap[source]).forEach(flag => {
9297
let binding = path.scope.getBinding(flag);
9398
if (binding !== undefined) {
94-
binding.referencePaths.forEach((p) => {
99+
binding.referencePaths.forEach(p => {
95100
let t = builder.t;
96101
if (envFlags.DEBUG) {
97102
if (svelteMap[source][flag] === false) {
98-
if (!p.parentPath.isIfStatement()) { return; }
103+
if (!p.parentPath.isIfStatement()) {
104+
return;
105+
}
99106
let consequent = p.parentPath.get('consequent');
100-
consequent.unshiftContainer('body', t.throwStatement(
101-
t.newExpression(t.identifier('Error'), [t.stringLiteral(`You indicated you don't have any deprecations, however you are relying on ${flag}.`)])
102-
));
107+
consequent.unshiftContainer(
108+
'body',
109+
t.throwStatement(
110+
t.newExpression(t.identifier('Error'), [
111+
t.stringLiteral(
112+
`You indicated you don't have any deprecations, however you are relying on ${flag}.`
113+
),
114+
])
115+
)
116+
);
103117
}
104118
} else {
105119
if (p.parentPath.isIfStatement()) {
@@ -135,14 +149,17 @@ module.exports = class Macros {
135149
build(path) {
136150
let expression = path.node.expression;
137151

138-
if (this.builder.t.isCallExpression(expression) && this.localDebugBindings.some((b) => b.node.name === expression.callee.name)) {
152+
if (
153+
this.builder.t.isCallExpression(expression) &&
154+
this.localDebugBindings.some(b => b.node.name === expression.callee.name)
155+
) {
139156
let imported = path.scope.getBinding(expression.callee.name).path.node.imported.name;
140157
this.builder[`${imported}`](path);
141158
}
142159
}
143160

144161
_collectImportBindings(specifiers, buffer) {
145-
specifiers.forEach((specifier) => {
162+
specifiers.forEach(specifier => {
146163
if (specifier.node.imported && SUPPORTED_MACROS.indexOf(specifier.node.imported.name) > -1) {
147164
buffer.push(specifier.get('local'));
148165
}
@@ -156,9 +173,11 @@ module.exports = class Macros {
156173
}
157174

158175
_detectForeignFeatureFlag(specifiers, source) {
159-
specifiers.forEach((specifier) => {
176+
specifiers.forEach(specifier => {
160177
if (specifier.imported && this.featuresMap[source][specifier.imported.name] !== null) {
161-
throw new Error(`Imported ${specifier.imported.name} from ${source} which is not a supported flag.`);
178+
throw new Error(
179+
`Imported ${specifier.imported.name} from ${source} which is not a supported flag.`
180+
);
162181
}
163182
});
164183
}
@@ -186,16 +205,16 @@ module.exports = class Macros {
186205

187206
if (!this.debugHelpers.module) {
188207
if (this.localDebugBindings.length > 0) {
189-
this.localDebugBindings[0].parentPath.parentPath
208+
this.localDebugBindings[0].parentPath.parentPath;
190209
let importPath = this.localDebugBindings[0].findParent(p => p.isImportDeclaration());
191210
let specifiers = importPath.get('specifiers');
192211

193212
if (specifiers.length === this.localDebugBindings.length) {
194213
this.localDebugBindings[0].parentPath.parentPath.remove();
195214
} else {
196-
this.localDebugBindings.forEach((binding) => binding.parentPath.remove());
215+
this.localDebugBindings.forEach(binding => binding.parentPath.remove());
197216
}
198217
}
199218
}
200219
}
201-
}
220+
};

src/utils/normalize-options.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ function normalizeOptions(options) {
1515
let hasSvelteBuild = false;
1616

1717
if (!Array.isArray(features)) {
18-
features = [features]
18+
features = [features];
1919
}
2020

21-
features = features.map((feature) => {
21+
features = features.map(feature => {
2222
let featuresSource = feature.source;
2323
featureSources.push(featuresSource);
2424
let name = feature.name;
@@ -27,12 +27,15 @@ function normalizeOptions(options) {
2727
featuresMap[featuresSource] = {};
2828
svelteMap[featuresSource] = {};
2929

30-
Object.keys(feature.flags).forEach((flagName) => {
30+
Object.keys(feature.flags).forEach(flagName => {
3131
let value = feature.flags[flagName];
3232

3333
if (svelte !== undefined && typeof value === 'string' && svelte[name]) {
3434
hasSvelteBuild = true;
35-
flags[flagName] = svelteMap[featuresSource][flagName] = satisfies(value, `>=${svelte[name]}`);
35+
flags[flagName] = svelteMap[featuresSource][flagName] = satisfies(
36+
value,
37+
`>=${svelte[name]}`
38+
);
3639
} else if (typeof value === 'boolean' || value === null) {
3740
flags[flagName] = featuresMap[featuresSource][flagName] = value;
3841
} else {
@@ -43,11 +46,10 @@ function normalizeOptions(options) {
4346
return {
4447
name,
4548
source: feature.source,
46-
flags
47-
}
49+
flags,
50+
};
4851
});
4952

50-
5153
if (!debugTools) {
5254
throw new Error('You must specify `debugTools.source`');
5355
}
@@ -64,7 +66,7 @@ function normalizeOptions(options) {
6466
_envFlags = envFlags.flags;
6567
}
6668
} else {
67-
throw new Error('You must specify envFlags.flags.DEBUG at minimum.')
69+
throw new Error('You must specify envFlags.flags.DEBUG at minimum.');
6870
}
6971

7072
return {
@@ -77,12 +79,12 @@ function normalizeOptions(options) {
7779
svelte,
7880
envFlags: {
7981
envFlagsImport,
80-
flags: _envFlags
82+
flags: _envFlags,
8183
},
8284
debugTools: {
8385
debugToolsImport,
84-
assertPredicateIndex
85-
}
86+
assertPredicateIndex,
87+
},
8688
};
8789
}
8890

0 commit comments

Comments
 (0)