Skip to content

Commit c1168d9

Browse files
authored
style: enable prefer-const rule (eslint-community#102)
https://eslint.org/docs/rules/prefer-const let and const better communicate if a variable is reassigned, and it is possible to use them in all files since 'use strict' is enabled (eslint-community#100).
1 parent 752bbd7 commit c1168d9

17 files changed

+63
-61
lines changed

.eslintrc.json

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"plugin:prettier/recommended"
1414
],
1515
"rules": {
16+
"no-var": "error",
17+
"prefer-const": "error",
1618
"strict": ["error", "global"],
1719
"eslint-plugin/require-meta-docs-url": [
1820
"error",

rules/always-return.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ module.exports = {
8888
// s2_4:
8989
// { good: false,
9090
// loc: <loc> } } } ]
91-
var funcInfoStack = []
91+
const funcInfoStack = []
9292

9393
function markCurrentBranchAsGood() {
94-
var funcInfo = peek(funcInfoStack)
95-
var currentBranchID = peek(funcInfo.branchIDStack)
94+
const funcInfo = peek(funcInfoStack)
95+
const currentBranchID = peek(funcInfo.branchIDStack)
9696
if (funcInfo.branchInfoMap[currentBranchID]) {
9797
funcInfo.branchInfoMap[currentBranchID].good = true
9898
}
@@ -104,13 +104,13 @@ module.exports = {
104104
ThrowStatement: markCurrentBranchAsGood,
105105

106106
onCodePathSegmentStart: function(segment, node) {
107-
var funcInfo = peek(funcInfoStack)
107+
const funcInfo = peek(funcInfoStack)
108108
funcInfo.branchIDStack.push(segment.id)
109109
funcInfo.branchInfoMap[segment.id] = { good: false, node: node }
110110
},
111111

112112
onCodePathSegmentEnd: function(segment, node) {
113-
var funcInfo = peek(funcInfoStack)
113+
const funcInfo = peek(funcInfoStack)
114114
funcInfo.branchIDStack.pop()
115115
},
116116

@@ -122,24 +122,24 @@ module.exports = {
122122
},
123123

124124
onCodePathEnd: function(path, node) {
125-
var funcInfo = funcInfoStack.pop()
125+
const funcInfo = funcInfoStack.pop()
126126

127127
if (!isInlineThenFunctionExpression(node)) {
128128
return
129129
}
130130

131131
path.finalSegments.forEach(segment => {
132-
var id = segment.id
133-
var branch = funcInfo.branchInfoMap[id]
132+
const id = segment.id
133+
const branch = funcInfo.branchInfoMap[id]
134134
if (!branch.good) {
135135
if (hasParentReturnStatement(branch.node)) {
136136
return
137137
}
138138

139139
// check shortcircuit syntax like `x && x()` and `y || x()``
140-
var prevSegments = segment.prevSegments
141-
for (var ii = prevSegments.length - 1; ii >= 0; --ii) {
142-
var prevSegment = prevSegments[ii]
140+
const prevSegments = segment.prevSegments
141+
for (let ii = prevSegments.length - 1; ii >= 0; --ii) {
142+
const prevSegment = prevSegments[ii]
143143
if (funcInfo.branchInfoMap[prevSegment.id].good) return
144144
}
145145

rules/catch-or-return.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
'use strict'
88

9-
var isPromise = require('./lib/is-promise')
9+
const isPromise = require('./lib/is-promise')
1010

1111
module.exports = {
1212
meta: {
@@ -15,9 +15,9 @@ module.exports = {
1515
}
1616
},
1717
create: function(context) {
18-
var options = context.options[0] || {}
19-
var allowThen = options.allowThen
20-
var terminationMethod = options.terminationMethod || 'catch'
18+
const options = context.options[0] || {}
19+
const allowThen = options.allowThen
20+
let terminationMethod = options.terminationMethod || 'catch'
2121

2222
if (typeof terminationMethod === 'string') {
2323
terminationMethod = [terminationMethod]

rules/lib/has-promise-callback.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
function hasPromiseCallback(node) {
1010
if (node.type !== 'CallExpression') return
1111
if (node.callee.type !== 'MemberExpression') return
12-
var propertyName = node.callee.property.name
12+
const propertyName = node.callee.property.name
1313
return propertyName === 'then' || propertyName === 'catch'
1414
}
1515

rules/lib/is-callback.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict'
22

3-
var isNamedCallback = require('./is-named-callback')
3+
const isNamedCallback = require('./is-named-callback')
44

55
function isCallingBack(node, exceptions) {
6-
var isCallExpression = node.type === 'CallExpression'
7-
var callee = node.callee || {}
8-
var nameIsCallback = isNamedCallback(callee.name, exceptions)
9-
var isCB = isCallExpression && nameIsCallback
6+
const isCallExpression = node.type === 'CallExpression'
7+
const callee = node.callee || {}
8+
const nameIsCallback = isNamedCallback(callee.name, exceptions)
9+
const isCB = isCallExpression && nameIsCallback
1010
return isCB
1111
}
1212

rules/lib/is-inside-callback.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
'use strict'
22

3-
var isInsidePromise = require('./is-inside-promise')
3+
const isInsidePromise = require('./is-inside-promise')
44

55
function isInsideCallback(node) {
6-
var isCallExpression =
6+
const isCallExpression =
77
node.type === 'FunctionExpression' ||
88
node.type === 'ArrowFunctionExpression' ||
99
node.type === 'FunctionDeclaration' // this may be controversial
1010

1111
// it's totally fine to use promises inside promises
1212
if (isInsidePromise(node)) return
1313

14-
var name = node.params && node.params[0] && node.params[0].name
15-
var firstArgIsError = name === 'err' || name === 'error'
16-
var isInACallback = isCallExpression && firstArgIsError
14+
const name = node.params && node.params[0] && node.params[0].name
15+
const firstArgIsError = name === 'err' || name === 'error'
16+
const isInACallback = isCallExpression && firstArgIsError
1717
return isInACallback
1818
}
1919

rules/lib/is-inside-promise.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict'
22

33
function isInsidePromise(node) {
4-
var isFunctionExpression =
4+
const isFunctionExpression =
55
node.type === 'FunctionExpression' ||
66
node.type === 'ArrowFunctionExpression'
7-
var parent = node.parent || {}
8-
var callee = parent.callee || {}
9-
var name = (callee.property && callee.property.name) || ''
10-
var parentIsPromise = name === 'then' || name === 'catch'
11-
var isInCB = isFunctionExpression && parentIsPromise
7+
const parent = node.parent || {}
8+
const callee = parent.callee || {}
9+
const name = (callee.property && callee.property.name) || ''
10+
const parentIsPromise = name === 'then' || name === 'catch'
11+
const isInCB = isFunctionExpression && parentIsPromise
1212
return isInCB
1313
}
1414

rules/lib/is-named-callback.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

3-
var callbacks = ['done', 'cb', 'callback', 'next']
3+
let callbacks = ['done', 'cb', 'callback', 'next']
44

55
module.exports = function isNamedCallback(potentialCallbackName, exceptions) {
6-
for (var i = 0; i < exceptions.length; i++) {
6+
for (let i = 0; i < exceptions.length; i++) {
77
callbacks = callbacks.filter(function(item) {
88
return item !== exceptions[i]
99
})

rules/lib/is-promise.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
'use strict'
66

7-
var STATIC_METHODS = ['all', 'race', 'reject', 'resolve']
7+
const STATIC_METHODS = ['all', 'race', 'reject', 'resolve']
88

99
function isPromise(expression) {
1010
return (

rules/no-callback-in-promise.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
'use strict'
77

8-
var hasPromiseCallback = require('./lib/has-promise-callback')
9-
var isInsidePromise = require('./lib/is-inside-promise')
10-
var isCallback = require('./lib/is-callback')
8+
const hasPromiseCallback = require('./lib/has-promise-callback')
9+
const isInsidePromise = require('./lib/is-inside-promise')
10+
const isCallback = require('./lib/is-callback')
1111

1212
module.exports = {
1313
meta: {
@@ -19,13 +19,13 @@ module.exports = {
1919
create: function(context) {
2020
return {
2121
CallExpression: function(node) {
22-
var options = context.options[0] || {}
23-
var exceptions = options.exceptions || []
22+
const options = context.options[0] || {}
23+
const exceptions = options.exceptions || []
2424
if (!isCallback(node, exceptions)) {
2525
// in general we send you packing if you're not a callback
2626
// but we also need to watch out for whatever.then(cb)
2727
if (hasPromiseCallback(node)) {
28-
var name =
28+
const name =
2929
node.arguments && node.arguments[0] && node.arguments[0].name
3030
if (
3131
name === 'callback' ||

rules/no-native.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424
}
2525
},
2626
create: function(context) {
27-
var MESSAGE = '"{{name}}" is not defined.'
27+
const MESSAGE = '"{{name}}" is not defined.'
2828

2929
/**
3030
* Checks for and reports reassigned constants
@@ -35,7 +35,7 @@ module.exports = {
3535
*/
3636
return {
3737
'Program:exit': function() {
38-
var scope = context.getScope()
38+
const scope = context.getScope()
3939

4040
scope.implicit.left.forEach(function(ref) {
4141
if (ref.identifier.name !== 'Promise') {

rules/no-nesting.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
'use strict'
77

8-
var hasPromiseCallback = require('./lib/has-promise-callback')
9-
var isInsidePromise = require('./lib/is-inside-promise')
8+
const hasPromiseCallback = require('./lib/has-promise-callback')
9+
const isInsidePromise = require('./lib/is-inside-promise')
1010

1111
module.exports = {
1212
meta: {

rules/no-promise-in-callback.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
'use strict'
77

8-
var isPromise = require('./lib/is-promise')
9-
var isInsideCallback = require('./lib/is-inside-callback')
8+
const isPromise = require('./lib/is-promise')
9+
const isInsideCallback = require('./lib/is-inside-callback')
1010

1111
module.exports = {
1212
meta: {

rules/no-return-in-finally.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
var isPromise = require('./lib/is-promise')
3+
const isPromise = require('./lib/is-promise')
44

55
module.exports = {
66
meta: {

rules/no-return-wrap.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
'use strict'
88

9-
var isPromise = require('./lib/is-promise')
10-
var rejectMessage = 'Expected throw instead of Promise.reject'
11-
var resolveMessage = 'Avoid wrapping return values in Promise.resolve'
9+
const isPromise = require('./lib/is-promise')
10+
const rejectMessage = 'Expected throw instead of Promise.reject'
11+
const resolveMessage = 'Avoid wrapping return values in Promise.resolve'
1212

1313
function isInPromise(context) {
14-
var expression = context.getAncestors().filter(function(node) {
14+
const expression = context.getAncestors().filter(function(node) {
1515
return node.type === 'ExpressionStatement'
1616
})[0]
1717
return expression && expression.expression && isPromise(expression.expression)
@@ -24,8 +24,8 @@ module.exports = {
2424
}
2525
},
2626
create: function(context) {
27-
var options = context.options[0] || {}
28-
var allowReject = options.allowReject
27+
const options = context.options[0] || {}
28+
const allowReject = options.allowReject
2929

3030
return {
3131
ReturnStatement: function(node) {

rules/param-names.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
return {
1111
NewExpression: function(node) {
1212
if (node.callee.name === 'Promise' && node.arguments.length === 1) {
13-
var params = node.arguments[0].params
13+
const params = node.arguments[0].params
1414

1515
if (!params || !params.length) {
1616
return

rules/prefer-await-to-callbacks.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict'
77

8-
var errorMessage = 'Avoid callbacks. Prefer Async/Await.'
8+
const errorMessage = 'Avoid callbacks. Prefer Async/Await.'
99

1010
module.exports = {
1111
meta: {
@@ -16,8 +16,8 @@ module.exports = {
1616
},
1717
create: function(context) {
1818
function checkLastParamsForCallback(node) {
19-
var len = node.params.length - 1
20-
var lastParam = node.params[len]
19+
const len = node.params.length - 1
20+
const lastParam = node.params[len]
2121
if (
2222
lastParam &&
2323
(lastParam.name === 'callback' || lastParam.name === 'cb')
@@ -41,9 +41,9 @@ module.exports = {
4141
}
4242

4343
// thennables aren't allowed either
44-
var args = node.arguments
45-
var num = args.length - 1
46-
var arg = num > -1 && node.arguments && node.arguments[num]
44+
const args = node.arguments
45+
const num = args.length - 1
46+
const arg = num > -1 && node.arguments && node.arguments[num]
4747
if (
4848
(arg && arg.type === 'FunctionExpression') ||
4949
arg.type === 'ArrowFunctionExpression'

0 commit comments

Comments
 (0)