From 499df9941e1d5891bd554c57a5dbcd11516d094f Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Tue, 16 Oct 2018 10:56:37 +0200 Subject: [PATCH] Enforce const (and let) via eslint --- .eslintrc.json | 8 ++++++-- bin/.eslintrc.yaml | 3 +++ bin/buble | 2 +- bin/handleError.js | 6 +++--- bin/runBuble.js | 2 +- bin/showHelp.js | 2 +- src/index.js | 6 +++--- src/program/BlockStatement.js | 6 +++--- src/program/Node.js | 4 ++-- src/program/types/ArrayExpression.js | 2 +- src/program/types/ClassBody.js | 14 +++++++------- src/program/types/ForInStatement.js | 2 +- src/program/types/ForOfStatement.js | 2 +- src/program/types/NewExpression.js | 2 +- src/program/types/TemplateLiteral.js | 2 +- src/program/types/VariableDeclaration.js | 4 ++-- src/program/types/VariableDeclarator.js | 2 +- src/utils/getSnippet.js | 2 +- src/utils/locate.js | 12 ++++++------ src/utils/reserved.js | 2 +- src/utils/spread.js | 2 +- test/.eslintrc.json | 5 ++++- 22 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 bin/.eslintrc.yaml diff --git a/.eslintrc.json b/.eslintrc.json index febe0773..6fb7955d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,4 +1,5 @@ { + "root": true, "rules": { "indent": [ 2, "tab", { "SwitchCase": 1 } ], "linebreak-style": [ 2, "unix" ], @@ -6,7 +7,9 @@ "keyword-spacing": [ 2, { "before": true, "after": true } ], "space-before-blocks": [ 2, "always" ], "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], - "no-cond-assign": [ 0 ] + "no-cond-assign": [ 0 ], + "prefer-const": 2, + "no-var": 2 }, "env": { "es6": true, @@ -21,7 +24,8 @@ { "files": ["register.js"], "rules": { - "no-console": [ 0 ] + "no-console": [ 0 ], + "no-var": [ 0 ] } } ] diff --git a/bin/.eslintrc.yaml b/bin/.eslintrc.yaml new file mode 100644 index 00000000..cf1d141d --- /dev/null +++ b/bin/.eslintrc.yaml @@ -0,0 +1,3 @@ +rules: + no-console: "off" + no-var: "off" diff --git a/bin/buble b/bin/buble index 95af636d..ea1ebdf5 100755 --- a/bin/buble +++ b/bin/buble @@ -18,7 +18,7 @@ var command = minimist(process.argv.slice(2), { if (command.help || (process.argv.length <= 2 && process.stdin.isTTY)) { require('./showHelp')(); } else if (command.version) { - console.log('Bublé version ' + require('../package.json').version); // eslint-disable-line no-console + console.log('Bublé version ' + require('../package.json').version); } else { require('./runBuble')(command); } diff --git a/bin/handleError.js b/bin/handleError.js index 4888adb9..5ed84964 100644 --- a/bin/handleError.js +++ b/bin/handleError.js @@ -1,7 +1,7 @@ var chalk = require('chalk'); function print(msg) { - console.error(chalk.red(msg)); // eslint-disable-line no-console + console.error(chalk.red(msg)); } var handlers = { @@ -44,11 +44,11 @@ module.exports = function handleError(err) { print(err.message || err); if (err.stack) { - console.error(chalk.grey(err.stack)); // eslint-disable-line no-console + console.error(chalk.grey(err.stack)); } } - console.error( // eslint-disable-line no-console + console.error( 'Type ' + chalk.cyan('buble --help') + ' for help, or visit https://buble.surge.sh/guide/' diff --git a/bin/runBuble.js b/bin/runBuble.js index ab1080ab..22f71571 100644 --- a/bin/runBuble.js +++ b/bin/runBuble.js @@ -71,7 +71,7 @@ function write(result, to, command) { if (to) { fs.writeFileSync(to, result.code); } else { - console.log(result.code); // eslint-disable-line no-console + console.log(result.code); } } diff --git a/bin/showHelp.js b/bin/showHelp.js index 770c5669..e2b2ff4c 100644 --- a/bin/showHelp.js +++ b/bin/showHelp.js @@ -11,6 +11,6 @@ module.exports = function() { .toString() .replace('<%= version %>', require('../package.json').version); - console.log('\n' + help + '\n'); // eslint-disable-line no-console + console.log('\n' + help + '\n'); }); }; diff --git a/src/index.js b/src/index.js index c808762d..dc7798a7 100644 --- a/src/index.js +++ b/src/index.js @@ -36,7 +36,7 @@ export function target(target) { bitmask &= support; }); - let transforms = Object.create(null); + const transforms = Object.create(null); features.forEach((name, i) => { transforms[name] = !(bitmask & (1 << i)); }); @@ -60,7 +60,7 @@ export function transform(source, options = {}) { allowReturnOutsideFunction: true, onComment: (block, text) => { if (!jsx) { - let match = /@jsx\s+([^\s]+)/.exec(text); + const match = /@jsx\s+([^\s]+)/.exec(text); if (match) jsx = match[1]; } } @@ -72,7 +72,7 @@ export function transform(source, options = {}) { throw err; } - let transforms = target(options.target || {}); + const transforms = target(options.target || {}); Object.keys(options.transforms || {}).forEach(name => { if (name === 'modules') { if (!('moduleImport' in options.transforms)) diff --git a/src/program/BlockStatement.js b/src/program/BlockStatement.js index f0cb8942..43c4fba0 100644 --- a/src/program/BlockStatement.js +++ b/src/program/BlockStatement.js @@ -122,7 +122,7 @@ export default class BlockStatement extends Node { transpile(code, transforms) { const indentation = this.getIndentation(); - let introStatementGenerators = []; + const introStatementGenerators = []; if (this.argumentsAlias) { introStatementGenerators.push((start, prefix, suffix) => { @@ -212,7 +212,7 @@ export default class BlockStatement extends Node { start = this.start + 1; } - let prefix = `\n${indentation}`; + const prefix = `\n${indentation}`; let suffix = ';'; introStatementGenerators.forEach((fn, i) => { if (i === introStatementGenerators.length - 1) suffix = `;\n`; @@ -305,7 +305,7 @@ export default class BlockStatement extends Node { Object.keys(this.scope.blockScopedDeclarations).forEach(name => { const declarations = this.scope.blockScopedDeclarations[name]; - for (let declaration of declarations) { + for (const declaration of declarations) { let cont = false; // TODO implement proper continue... if (declaration.kind === 'for.let') { diff --git a/src/program/Node.js b/src/program/Node.js index 3e0429ce..8c6d74a1 100644 --- a/src/program/Node.js +++ b/src/program/Node.js @@ -1,7 +1,7 @@ // used for debugging, without the noise created by // circular references function toJSON(node) { - var obj = {}; + const obj = {}; Object.keys(node).forEach(key => { if ( @@ -79,7 +79,7 @@ export default class Node { } initialise(transforms) { - for (var key of this.keys) { + for (const key of this.keys) { const value = this[key]; if (Array.isArray(value)) { diff --git a/src/program/types/ArrayExpression.js b/src/program/types/ArrayExpression.js index d815d6ee..bf363bd7 100644 --- a/src/program/types/ArrayExpression.js +++ b/src/program/types/ArrayExpression.js @@ -28,7 +28,7 @@ export default class ArrayExpression extends Node { if (transforms.spreadRest) { // erase trailing comma after last array element if not an array hole if (this.elements.length) { - let lastElement = this.elements[this.elements.length - 1]; + const lastElement = this.elements[this.elements.length - 1]; if ( lastElement && /\s*,/.test(code.original.slice(lastElement.end, this.end)) diff --git a/src/program/types/ClassBody.js b/src/program/types/ClassBody.js index 6a785f61..8afbad42 100644 --- a/src/program/types/ClassBody.js +++ b/src/program/types/ClassBody.js @@ -49,9 +49,9 @@ export default class ClassBody extends Node { if (!inFunctionExpression) code.appendLeft(constructor.end, ';'); } - let namedFunctions = + const namedFunctions = this.program.options.namedFunctionExpressions !== false; - let namedConstructor = + const namedConstructor = namedFunctions || this.parent.superClass || this.parent.type !== 'ClassDeclaration'; @@ -86,8 +86,8 @@ export default class ClassBody extends Node { const scope = this.findScope(false); - let prototypeGettersAndSetters = []; - let staticGettersAndSetters = []; + const prototypeGettersAndSetters = []; + const staticGettersAndSetters = []; let prototypeAccessors; let staticAccessors; @@ -99,7 +99,7 @@ export default class ClassBody extends Node { } if (method.kind === 'constructor') { - let constructorName = namedConstructor ? ' ' + name : ''; + const constructorName = namedConstructor ? ' ' + name : ''; code.overwrite( method.key.start, method.key.end, @@ -198,8 +198,8 @@ export default class ClassBody extends Node { }); if (prototypeGettersAndSetters.length || staticGettersAndSetters.length) { - let intro = []; - let outro = []; + const intro = []; + const outro = []; if (prototypeGettersAndSetters.length) { intro.push( diff --git a/src/program/types/ForInStatement.js b/src/program/types/ForInStatement.js index 45346be3..e7f992a7 100644 --- a/src/program/types/ForInStatement.js +++ b/src/program/types/ForInStatement.js @@ -47,7 +47,7 @@ export default class ForInStatement extends LoopStatement { code.prependRight(pattern.end, isDeclaration ? ref : `var ${ref}`); - let statementGenerators = []; + const statementGenerators = []; destructure( code, id => scope.createIdentifier(id), diff --git a/src/program/types/ForOfStatement.js b/src/program/types/ForOfStatement.js index 2bf0e612..f553e8b2 100644 --- a/src/program/types/ForOfStatement.js +++ b/src/program/types/ForOfStatement.js @@ -55,7 +55,7 @@ export default class ForOfStatement extends LoopStatement { const isDeclaration = this.left.type === 'VariableDeclaration'; const maybeDestructuring = isDeclaration ? this.left.declarations[0].id : this.left; if (maybeDestructuring.type !== 'Identifier') { - let statementGenerators = []; + const statementGenerators = []; const ref = scope.createIdentifier('ref'); destructure( code, diff --git a/src/program/types/NewExpression.js b/src/program/types/NewExpression.js index a096cb4d..ccf9b8da 100644 --- a/src/program/types/NewExpression.js +++ b/src/program/types/NewExpression.js @@ -26,7 +26,7 @@ export default class NewExpression extends Node { if (transforms.spreadRest && this.arguments.length) { const firstArgument = this.arguments[0]; const isNew = true; - let hasSpreadElements = spread( + const hasSpreadElements = spread( code, this.arguments, firstArgument.start, diff --git a/src/program/types/TemplateLiteral.js b/src/program/types/TemplateLiteral.js index 93ac4ff0..6795613e 100644 --- a/src/program/types/TemplateLiteral.js +++ b/src/program/types/TemplateLiteral.js @@ -8,7 +8,7 @@ export default class TemplateLiteral extends Node { transforms.templateString && this.parent.type !== 'TaggedTemplateExpression' ) { - let ordered = this.expressions + const ordered = this.expressions .concat(this.quasis) .sort((a, b) => a.start - b.start || a.end - b.end) .filter((node, i) => { diff --git a/src/program/types/VariableDeclaration.js b/src/program/types/VariableDeclaration.js index aa5e7ec5..eabf265a 100644 --- a/src/program/types/VariableDeclaration.js +++ b/src/program/types/VariableDeclaration.js @@ -49,7 +49,7 @@ export default class VariableDeclaration extends Node { c = declarator.start; - let statementGenerators = []; + const statementGenerators = []; if (simple) { code.remove(declarator.id.end, declarator.end); @@ -72,7 +72,7 @@ export default class VariableDeclaration extends Node { statementGenerators ); - let prefix = inline ? 'var ' : ''; + const prefix = inline ? 'var ' : ''; let suffix = inline ? `, ` : `;\n${i0}`; statementGenerators.forEach((fn, j) => { if ( diff --git a/src/program/types/VariableDeclarator.js b/src/program/types/VariableDeclarator.js index 4f7dc888..0b3daa17 100644 --- a/src/program/types/VariableDeclarator.js +++ b/src/program/types/VariableDeclarator.js @@ -13,7 +13,7 @@ export default class VariableDeclarator extends Node { transpile(code, transforms) { if (!this.init && transforms.letConst && this.parent.kind !== 'var') { - let inLoop = this.findNearest( + const inLoop = this.findNearest( /Function|^For(In|Of)?Statement|^(?:Do)?WhileStatement/ ); if ( diff --git a/src/utils/getSnippet.js b/src/utils/getSnippet.js index a114c466..7dd6f397 100644 --- a/src/utils/getSnippet.js +++ b/src/utils/getSnippet.js @@ -1,5 +1,5 @@ function pad(num, len) { - let result = String(num); + const result = String(num); return result + repeat(' ', len - result.length); } diff --git a/src/utils/locate.js b/src/utils/locate.js index bd10b00c..ec092a2e 100644 --- a/src/utils/locate.js +++ b/src/utils/locate.js @@ -1,13 +1,13 @@ export default function locate(source, index) { - var lines = source.split('\n'); - var len = lines.length; + const lines = source.split('\n'); + const len = lines.length; - var lineStart = 0; - var i; + let lineStart = 0; + let i; for (i = 0; i < len; i += 1) { - var line = lines[i]; - var lineEnd = lineStart + line.length + 1; // +1 for newline + const line = lines[i]; + const lineEnd = lineStart + line.length + 1; // +1 for newline if (lineEnd > index) { return { line: i + 1, column: index - lineStart, char: i }; diff --git a/src/utils/reserved.js b/src/utils/reserved.js index 7268cbef..2a359352 100644 --- a/src/utils/reserved.js +++ b/src/utils/reserved.js @@ -1,4 +1,4 @@ -let reserved = Object.create(null); +const reserved = Object.create(null); 'do if in for let new try var case else enum eval null this true void with await break catch class const false super throw while yield delete export import public return static switch typeof default extends finally package private continue debugger function arguments interface protected implements instanceof' .split(' ') .forEach(word => (reserved[word] = true)); diff --git a/src/utils/spread.js b/src/utils/spread.js index 2026a925..ce2a10cc 100644 --- a/src/utils/spread.js +++ b/src/utils/spread.js @@ -31,7 +31,7 @@ export default function spread( if (isNew) { for (i = 0; i < elements.length; i += 1) { - let element = elements[i]; + const element = elements[i]; if (element.type === 'SpreadElement') { code.remove(element.start, element.argument.start); } else { diff --git a/test/.eslintrc.json b/test/.eslintrc.json index 936f0941..8766251b 100644 --- a/test/.eslintrc.json +++ b/test/.eslintrc.json @@ -2,5 +2,8 @@ "env": { "mocha": true }, - "rules": { "no-console": "off" } + "rules": { + "no-console": "off", + "no-var": "off" + } }