Skip to content

Commit

Permalink
Enforce const (and let) via eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianheine committed Oct 16, 2018
1 parent a4d66ef commit 499df99
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 41 deletions.
8 changes: 6 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"root": true,
"rules": {
"indent": [ 2, "tab", { "SwitchCase": 1 } ],
"linebreak-style": [ 2, "unix" ],
"semi": [ 2, "always" ],
"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,
Expand All @@ -21,7 +24,8 @@
{
"files": ["register.js"],
"rules": {
"no-console": [ 0 ]
"no-console": [ 0 ],
"no-var": [ 0 ]
}
}
]
Expand Down
3 changes: 3 additions & 0 deletions bin/.eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rules:
no-console: "off"
no-var: "off"
2 changes: 1 addition & 1 deletion bin/buble
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
6 changes: 3 additions & 3 deletions bin/handleError.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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/'
Expand Down
2 changes: 1 addition & 1 deletion bin/runBuble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion bin/showHelp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
};
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand All @@ -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];
}
}
Expand All @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions src/program/BlockStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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`;
Expand Down Expand Up @@ -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') {
Expand Down
4 changes: 2 additions & 2 deletions src/program/Node.js
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/program/types/ArrayExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 7 additions & 7 deletions src/program/types/ClassBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/program/types/ForInStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/program/types/ForOfStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/program/types/NewExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/program/types/TemplateLiteral.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/program/types/VariableDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion src/program/types/VariableDeclarator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getSnippet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function pad(num, len) {
let result = String(num);
const result = String(num);
return result + repeat(' ', len - result.length);
}

Expand Down
12 changes: 6 additions & 6 deletions src/utils/locate.js
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion src/utils/reserved.js
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/utils/spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion test/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"env": {
"mocha": true
},
"rules": { "no-console": "off" }
"rules": {
"no-console": "off",
"no-var": "off"
}
}

0 comments on commit 499df99

Please sign in to comment.