Skip to content

Commit 59080a5

Browse files
authored
Merge pull request #61 from rwjblue/linting
Add linting!
2 parents b8a2cfa + 611bf25 commit 59080a5

14 files changed

+984
-399
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/fixtures/

.eslintrc.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = {
2+
root: true,
3+
extends: ['eslint:recommended', 'prettier', 'plugin:node/recommended'],
4+
plugins: ['prettier', 'node'],
5+
parserOptions: {
6+
ecmaVersion: 2017,
7+
},
8+
env: {
9+
node: true,
10+
},
11+
rules: {
12+
'prettier/prettier': 'error',
13+
},
14+
15+
overrides: [
16+
{
17+
files: ['tests/**/*.js'],
18+
env: {
19+
jest: true
20+
}
21+
}
22+
]
23+
};

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "es5",
4+
"printWidth": 100
5+
}

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ language: node_js
33
node_js:
44
- '4'
55
- '6'
6-
- '7'
6+
- '8'
7+
- '10'
78

89
cache: yarn
910

1011
script:
12+
- yarn eslint .
1113
- yarn test --coverage

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
"@babel/preset-env": "^7.0.0-beta.42",
55
"babel-core": "^6.26.0",
66
"babel-preset-env": "^1.6.1",
7-
"jest": "^21.0.0"
7+
"eslint": "^4.19.1",
8+
"eslint-config-prettier": "^2.9.0",
9+
"eslint-plugin-node": "^6.0.1",
10+
"eslint-plugin-prettier": "^2.6.0",
11+
"jest": "^21.0.0",
12+
"prettier": "^1.12.1"
813
},
914
"name": "babel-plugin-debug-macros",
1015
"version": "0.2.0-beta.2",
@@ -41,5 +46,8 @@
4146
"testMatch": [
4247
"<rootDir>/tests/**/*-test.js"
4348
]
49+
},
50+
"engines": {
51+
"node": ">=4"
4452
}
4553
}

src/index.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,49 @@ const normalizeOptions = require('./utils/normalize-options').normalizeOptions;
77
function macros(babel) {
88
const t = babel.types;
99

10-
let macroBuilder;
1110
let options;
1211

1312
return {
14-
name: "babel-feature-flags-and-debug-macros",
13+
name: 'babel-feature-flags-and-debug-macros',
1514
visitor: {
16-
1715
Program: {
1816
enter(path, state) {
1917
options = normalizeOptions(state.opts);
2018
this.macroBuilder = new Macros(t, options);
2119

2220
let body = path.get('body');
2321

24-
body.forEach((item) => {
22+
body.forEach(item => {
2523
if (item.isImportDeclaration()) {
2624
let importPath = item.node.source.value;
2725

28-
let featureSources = options.featureSources;
2926
let debugToolsImport = options.debugTools.debugToolsImport;
3027
let envFlagsImport = options.envFlags.envFlagsImport;
3128

32-
let isFeaturesImport = featureSources.indexOf(importPath) > -1;
33-
3429
if (debugToolsImport && debugToolsImport === importPath) {
3530
this.macroBuilder.collectDebugToolsSpecifiers(item.get('specifiers'));
36-
} if (envFlagsImport && envFlagsImport === importPath) {
31+
}
32+
if (envFlagsImport && envFlagsImport === importPath) {
3733
this.macroBuilder.collectEnvFlagSpecifiers(item.get('specifiers'));
3834
}
3935
}
4036
});
41-
4237
},
4338

4439
exit(path) {
4540
this.macroBuilder.expand(path);
46-
}
41+
},
4742
},
4843

4944
ExpressionStatement(path) {
5045
this.macroBuilder.build(path);
51-
}
52-
}
46+
},
47+
},
5348
};
5449
}
5550

5651
macros.baseDir = function() {
5752
return path.dirname(__dirname);
58-
}
53+
};
5954

6055
module.exports = macros;

src/utils/builder.js

Lines changed: 20 additions & 18 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

@@ -104,7 +104,6 @@ module.exports = class Builder {
104104
callExpression = this._createConsoleAPI(options.consoleAPI || callee, args);
105105
}
106106

107-
let identifiers = this._getIdentifiers(args);
108107
let prefixedIdentifiers = [];
109108

110109
if (options.predicate) {
@@ -113,7 +112,10 @@ module.exports = class Builder {
113112
prefixedIdentifiers.push(negatedPredicate);
114113
}
115114

116-
this.expressions.push([path, this._buildLogicalExpressions(prefixedIdentifiers, callExpression)]);
115+
this.expressions.push([
116+
path,
117+
this._buildLogicalExpressions(prefixedIdentifiers, callExpression),
118+
]);
117119
}
118120

119121
/**
@@ -154,14 +156,14 @@ module.exports = class Builder {
154156
validate: (expression, args) => {
155157
let meta = args[2];
156158

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

161-
if (meta && meta.properties && !meta.properties.some(prop => prop.key.name === 'until')) {
163+
if (meta && meta.properties && !meta.properties.some(prop => prop.key.name === 'until')) {
162164
throw new ReferenceError(`deprecate's meta information requires an "until" field.`);
163165
}
164-
}
166+
},
165167
});
166168
}
167169

@@ -180,7 +182,7 @@ module.exports = class Builder {
180182
}
181183

182184
_getIdentifiers(args) {
183-
return args.filter((arg) => this.t.isIdentifier(arg));
185+
return args.filter(arg => this.t.isIdentifier(arg));
184186
}
185187

186188
_createGlobalExternalHelper(identifier, args, ns) {
@@ -196,22 +198,22 @@ module.exports = class Builder {
196198
_buildLogicalExpressions(identifiers, callExpression) {
197199
let t = this.t;
198200

199-
return (debugIdentifier) => {
201+
return debugIdentifier => {
200202
identifiers.unshift(debugIdentifier);
201203
identifiers.push(callExpression);
202204
let logicalExpressions;
203205

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-
}
206+
for (let i = 0; i < identifiers.length; i++) {
207+
let left = identifiers[i];
208+
let right = identifiers[i + 1];
209+
if (!logicalExpressions) {
210+
logicalExpressions = t.logicalExpression('&&', left, right);
211+
} else if (right) {
212+
logicalExpressions = t.logicalExpression('&&', logicalExpressions, right);
212213
}
214+
}
213215

214216
return logicalExpressions;
215-
}
217+
};
216218
}
217-
}
219+
};

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+
};

0 commit comments

Comments
 (0)