Skip to content

Commit 5c7501c

Browse files
committed
Add prefer-const eslint rule
1 parent 0682367 commit 5c7501c

21 files changed

+75
-74
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"one-var": [0, { "initialized": "never" }],
113113
"operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }],
114114
"padded-blocks": [0, "never"],
115+
"prefer-const": 2,
115116
"quotes": [2, "single", "avoid-escape"],
116117
"radix": 2,
117118
"semi": [2, "always"],

examples/match.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const match = (list, pattern, options = {}) => {
1515
normalize = true;
1616
}
1717

18-
let isMatch = pm(pattern, options);
19-
let matches = new Set();
18+
const isMatch = pm(pattern, options);
19+
const matches = new Set();
2020
for (let ele of list) {
2121
if (normalize === true || options.normalize === true) {
2222
ele = path.posix.normalize(ele);
@@ -28,7 +28,7 @@ const match = (list, pattern, options = {}) => {
2828
return [...matches];
2929
};
3030

31-
let fixtures = ['a.md', 'a/b.md', './a.md', './a/b.md', 'a/b/c.md', './a/b/c.md', '.\\a\\b\\c.md', 'a\\b\\c.md'];
31+
const fixtures = ['a.md', 'a/b.md', './a.md', './a/b.md', 'a/b/c.md', './a/b/c.md', '.\\a\\b\\c.md', 'a\\b\\c.md'];
3232

3333
console.log(path.posix.normalize('./{a,b,c}/*.md'));
3434
console.log(match(fixtures, './**/*.md'));

lib/parse.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const expandRange = (args, options) => {
2525
}
2626

2727
args.sort();
28-
let value = `[${args.join('-')}]`;
28+
const value = `[${args.join('-')}]`;
2929

3030
try {
3131
/* eslint-disable no-new */
@@ -77,18 +77,18 @@ const parse = (input, options) => {
7777

7878
input = REPLACEMENTS[input] || input;
7979

80-
let opts = { ...options };
81-
let max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
82-
let len = input.length;
80+
const opts = { ...options };
81+
const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
82+
const len = input.length;
8383
if (len > max) {
8484
throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
8585
}
8686

87-
let bos = { type: 'bos', value: '', output: opts.prepend || '' };
88-
let tokens = [bos];
87+
const bos = { type: 'bos', value: '', output: opts.prepend || '' };
88+
const tokens = [bos];
8989

90-
let capture = opts.capture ? '' : '?:';
91-
let win32 = utils.isWindows(options);
90+
const capture = opts.capture ? '' : '?:';
91+
const win32 = utils.isWindows(options);
9292

9393
// create constants based on platform, for windows or posix
9494
const PLATFORM_CHARS = constants.globChars(win32);
@@ -113,9 +113,9 @@ const parse = (input, options) => {
113113
return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
114114
};
115115

116-
let nodot = opts.dot ? '' : NO_DOT;
116+
const nodot = opts.dot ? '' : NO_DOT;
117117
let star = opts.bash === true ? globstar(opts) : STAR;
118-
let qmarkNoDot = opts.dot ? QMARK : QMARK_NO_DOT;
118+
const qmarkNoDot = opts.dot ? QMARK : QMARK_NO_DOT;
119119

120120
if (opts.capture) {
121121
star = `(${star})`;
@@ -126,7 +126,7 @@ const parse = (input, options) => {
126126
opts.noextglob = opts.noext;
127127
}
128128

129-
let state = {
129+
const state = {
130130
index: -1,
131131
start: 0,
132132
consumed: '',
@@ -139,8 +139,8 @@ const parse = (input, options) => {
139139
tokens
140140
};
141141

142-
let extglobs = [];
143-
let stack = [];
142+
const extglobs = [];
143+
const stack = [];
144144
let prev = bos;
145145
let value;
146146

@@ -176,8 +176,8 @@ const parse = (input, options) => {
176176

177177
const push = tok => {
178178
if (prev.type === 'globstar') {
179-
let isBrace = state.braces > 0 && (tok.type === 'comma' || tok.type === 'brace');
180-
let isExtglob = extglobs.length && (tok.type === 'pipe' || tok.type === 'paren');
179+
const isBrace = state.braces > 0 && (tok.type === 'comma' || tok.type === 'brace');
180+
const isExtglob = extglobs.length && (tok.type === 'pipe' || tok.type === 'paren');
181181
if (tok.type !== 'slash' && tok.type !== 'paren' && !isBrace && !isExtglob) {
182182
state.output = state.output.slice(0, -prev.output.length);
183183
prev.type = 'star';
@@ -203,12 +203,12 @@ const parse = (input, options) => {
203203
};
204204

205205
const extglobOpen = (type, value) => {
206-
let token = { ...EXTGLOB_CHARS[value], conditions: 1, inner: '' };
206+
const token = { ...EXTGLOB_CHARS[value], conditions: 1, inner: '' };
207207

208208
token.prev = prev;
209209
token.parens = state.parens;
210210
token.output = state.output;
211-
let output = (opts.capture ? '(' : '') + token.open;
211+
const output = (opts.capture ? '(' : '') + token.open;
212212

213213
push({ type, value, output: state.output ? '' : ONE_CHAR });
214214
push({ type: 'paren', extglob: true, value: advance(), output });
@@ -301,7 +301,7 @@ const parse = (input, options) => {
301301
*/
302302

303303
if (value === '\\') {
304-
let next = peek();
304+
const next = peek();
305305

306306
if (next === '/' && opts.bash !== true) {
307307
continue;
@@ -841,7 +841,7 @@ const parse = (input, options) => {
841841
continue;
842842
}
843843

844-
let token = { type: 'star', value, output: star };
844+
const token = { type: 'star', value, output: star };
845845

846846
if (opts.bash === true) {
847847
token.output = '.*?';
@@ -907,7 +907,7 @@ const parse = (input, options) => {
907907
if (state.backtrack === true) {
908908
state.output = '';
909909

910-
for (let token of state.tokens) {
910+
for (const token of state.tokens) {
911911
state.output += token.output != null ? token.output : token.value;
912912

913913
if (token.suffix) {
@@ -989,10 +989,10 @@ parse.fastpaths = (input, options) => {
989989
return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${DOT_LITERAL}${ONE_CHAR}${star}`;
990990

991991
default: {
992-
let match = /^(.*?)\.(\w+)$/.exec(str);
992+
const match = /^(.*?)\.(\w+)$/.exec(str);
993993
if (!match) return;
994994

995-
let source = create(match[1], options);
995+
const source = create(match[1], options);
996996
if (!source) return;
997997

998998
return source + DOT_LITERAL + match[2];

lib/picomatch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const picomatch = (glob, options, returnState = false) => {
3232
if (Array.isArray(glob)) {
3333
const fns = glob.map(input => picomatch(input, options, returnState));
3434
return str => {
35-
for (let isMatch of fns) {
35+
for (const isMatch of fns) {
3636
const state = isMatch(str);
3737
if (state) return state;
3838
}
@@ -52,13 +52,13 @@ const picomatch = (glob, options, returnState = false) => {
5252

5353
let isIgnored = () => false;
5454
if (opts.ignore) {
55-
let ignoreOpts = { ...options, ignore: null, onMatch: null, onResult: null };
55+
const ignoreOpts = { ...options, ignore: null, onMatch: null, onResult: null };
5656
isIgnored = picomatch(opts.ignore, ignoreOpts, returnState);
5757
}
5858

5959
const matcher = (input, returnObject = false) => {
60-
let { isMatch, match, output } = picomatch.test(input, regex, options, { glob, posix });
61-
let result = { glob, state, regex, posix, input, output, match, isMatch };
60+
const { isMatch, match, output } = picomatch.test(input, regex, options, { glob, posix });
61+
const result = { glob, state, regex, posix, input, output, match, isMatch };
6262

6363
if (typeof opts.onResult === 'function') {
6464
opts.onResult(result);

lib/scan.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ module.exports = (input, options) => {
145145
}
146146
}
147147

148-
let isExtglobChar = code === CHAR_PLUS
148+
const isExtglobChar = code === CHAR_PLUS
149149
|| code === CHAR_AT
150150
|| code === CHAR_EXCLAMATION_MARK;
151151

@@ -181,7 +181,7 @@ module.exports = (input, options) => {
181181
}
182182

183183
let prefix = '';
184-
let orig = input;
184+
const orig = input;
185185
let base = input;
186186
let glob = '';
187187

test/api.scan.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert').strict;
55
const scan = require('../lib/scan');
66
const base = (...args) => scan(...args).base;
77
const both = (...args) => {
8-
let { base, glob } = scan(...args);
8+
const { base, glob } = scan(...args);
99
return [base, glob];
1010
};
1111

@@ -273,7 +273,7 @@ describe('picomatch', () => {
273273
});
274274

275275
it('should support regex character classes', () => {
276-
let opts = { unescape: true };
276+
const opts = { unescape: true };
277277
assert.deepEqual(both('[a-c]b*'), ['', '[a-c]b*']);
278278
assert.deepEqual(both('[a-j]*[^c]'), ['', '[a-j]*[^c]']);
279279
assert.deepEqual(both('[a-j]*[^c]b/c'), ['', '[a-j]*[^c]b/c']);
@@ -355,7 +355,7 @@ describe('picomatch', () => {
355355
});
356356

357357
it('should respect brace enclosures with embedded separators', () => {
358-
let opts = { unescape: true };
358+
const opts = { unescape: true };
359359
assert.equal(base('path/{,/,bar/baz,qux}/', opts), 'path');
360360
assert.equal(base('path/\\{,/,bar/baz,qux}/', opts), 'path/{,/,bar/baz,qux}/');
361361
assert.equal(base('path/\\{,/,bar/baz,qux\\}/', opts), 'path/{,/,bar/baz,qux}/');
@@ -367,7 +367,7 @@ describe('picomatch', () => {
367367
});
368368

369369
it('should handle escaped nested braces', () => {
370-
let opts = { unescape: true };
370+
const opts = { unescape: true };
371371
assert.equal(base('\\{../,./,\\{bar,/baz},qux}', opts), '{../,./,{bar,/baz},qux}');
372372
assert.equal(base('\\{../,./,\\{bar,/baz},qux}/', opts), '{../,./,{bar,/baz},qux}/');
373373
assert.equal(base('path/\\{,/,bar/{baz,qux}}/', opts), 'path/{,/,bar/{baz,qux}}/');
@@ -378,7 +378,7 @@ describe('picomatch', () => {
378378
});
379379

380380
it('should recognize escaped braces', () => {
381-
let opts = { unescape: true };
381+
const opts = { unescape: true };
382382
assert.equal(base('\\{foo,bar\\}', opts), '{foo,bar}');
383383
assert.equal(base('\\{foo,bar\\}/', opts), '{foo,bar}/');
384384
assert.equal(base('\\{foo,bar}/', opts), '{foo,bar}/');

test/dotfiles.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('dotfiles', () => {
2828
});
2929

3030
it('should match dotfiles when there is a leading dot:', () => {
31-
let opts = { dot: true };
31+
const opts = { dot: true };
3232
assert.deepEqual(match(['.dotfile'], '*', opts), ['.dotfile']);
3333
assert.deepEqual(match(['.dotfile'], '**', opts), ['.dotfile']);
3434
assert.deepEqual(match(['a/b', 'a/.b', '.a/b', '.a/.b'], '**', opts), ['a/b', 'a/.b', '.a/b', '.a/.b']);
@@ -40,7 +40,7 @@ describe('dotfiles', () => {
4040
});
4141

4242
it('should match dotfiles when there is not a leading dot:', () => {
43-
let opts = { dot: true };
43+
const opts = { dot: true };
4444
assert.deepEqual(match(['.dotfile'], '*.*', opts), ['.dotfile']);
4545
assert.deepEqual(match(['.a', '.b', 'c', 'c.md'], '*.*', opts), ['.a', '.b', 'c.md']);
4646
assert.deepEqual(match(['.dotfile'], '*.md', opts), []);
@@ -61,7 +61,7 @@ describe('dotfiles', () => {
6161

6262
describe('options.dot', () => {
6363
it('should match dotfiles when `options.dot` is true:', () => {
64-
let fixtures = ['a/./b', 'a/../b', 'a/c/b', 'a/.d/b'];
64+
const fixtures = ['a/./b', 'a/../b', 'a/c/b', 'a/.d/b'];
6565
assert.deepEqual(match(['.dotfile'], '*.*', { dot: true }), ['.dotfile']);
6666
assert.deepEqual(match(['.dotfile'], '*.md', { dot: true }), []);
6767
assert.deepEqual(match(['.dotfile'], '.dotfile', { dot: true }), ['.dotfile']);

test/extglobs-temp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ describe('extglobs', () => {
10581058
});
10591059

10601060
it('should work with character classes', () => {
1061-
let opts = { posix: true };
1061+
const opts = { posix: true };
10621062
assert(isMatch('a.b', 'a[^[:alnum:]]b', opts));
10631063
assert(isMatch('a,b', 'a[^[:alnum:]]b', opts));
10641064
assert(isMatch('a:b', 'a[^[:alnum:]]b', opts));
@@ -1133,7 +1133,7 @@ describe('extglobs', () => {
11331133
});
11341134

11351135
it('should support POSIX character classes in extglobs', () => {
1136-
let opts = { posix: true };
1136+
const opts = { posix: true };
11371137
assert(isMatch('a.c', '+([[:alpha:].])', opts));
11381138
assert(isMatch('a.c', '+([[:alpha:].])+([[:alpha:].])', opts));
11391139
assert(isMatch('a.c', '*([[:alpha:].])', opts));

test/extglobs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { isMatch, makeRe } = require('..');
1111

1212
describe('extglobs', () => {
1313
it('should throw on imbalanced sets when `options.strictBrackets` is true', () => {
14-
let opts = { strictBrackets: true };
14+
const opts = { strictBrackets: true };
1515
assert.throws(() => makeRe('a(b', opts), /Missing closing: "\)"/i);
1616
assert.throws(() => makeRe('a)b', opts), /Missing opening: "\("/i);
1717
});
@@ -713,7 +713,7 @@ describe('extglobs', () => {
713713
// these are not extglobs, and do not need to pass, but they are included
714714
// to test integration with other features
715715
it('should support regex characters', () => {
716-
let fixtures = ['a c', 'a.c', 'a.xy.zc', 'a.zc', 'a123c', 'a1c', 'abbbbc', 'abbbc', 'abbc', 'abc', 'abq', 'axy zc', 'axy', 'axy.zc', 'axyzc'];
716+
const fixtures = ['a c', 'a.c', 'a.xy.zc', 'a.zc', 'a123c', 'a1c', 'abbbbc', 'abbbc', 'abbc', 'abc', 'abq', 'axy zc', 'axy', 'axy.zc', 'axyzc'];
717717

718718
if (process.platform !== 'win32') {
719719
assert.deepEqual(match(['a\\b', 'a/b', 'ab'], 'a/b'), ['a/b']);

test/globstars.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('stars', () => {
3535
});
3636

3737
it('should respect trailing slashes on paterns', () => {
38-
let fixtures = ['a', 'a/', 'a/a', 'a/a/', 'a/a/a', 'a/a/a/', 'a/a/a/a', 'a/a/a/a/', 'a/a/a/a/a', 'a/a/a/a/a/', 'a/a/b', 'a/a/b/', 'a/b', 'a/b/', 'a/b/c/.d/e/', 'a/c', 'a/c/', 'a/b', 'a/x/', 'b', 'b/', 'x/y', 'x/y/', 'z/z', 'z/z/'];
38+
const fixtures = ['a', 'a/', 'a/a', 'a/a/', 'a/a/a', 'a/a/a/', 'a/a/a/a', 'a/a/a/a/', 'a/a/a/a/a', 'a/a/a/a/a/', 'a/a/b', 'a/a/b/', 'a/b', 'a/b/', 'a/b/c/.d/e/', 'a/c', 'a/c/', 'a/b', 'a/x/', 'b', 'b/', 'x/y', 'x/y/', 'z/z', 'z/z/'];
3939

4040
assert.deepEqual(match(fixtures, '**/*/a/'), ['a/a/', 'a/a/a/', 'a/a/a/a/', 'a/a/a/a/a/']);
4141
assert.deepEqual(match(fixtures, '**/*/a/*/'), ['a/a/a/', 'a/a/a/a/', 'a/a/a/a/a/', 'a/a/b/']);
@@ -52,7 +52,7 @@ describe('stars', () => {
5252
});
5353

5454
it('should match literal globstars when stars are escaped', () => {
55-
let fixtures = ['.md', '**a.md', '**.md', '.md', '**'];
55+
const fixtures = ['.md', '**a.md', '**.md', '.md', '**'];
5656
assert.deepEqual(match(fixtures, '\\*\\**.md'), ['**a.md', '**.md']);
5757
assert.deepEqual(match(fixtures, '\\*\\*.md'), ['**.md']);
5858
});
@@ -286,7 +286,7 @@ describe('stars', () => {
286286
});
287287

288288
it('should match leading dots when defined in pattern', () => {
289-
let fixtures = ['.gitignore', 'a/b/z/.dotfile', 'a/b/z/.dotfile.md', 'a/b/z/.dotfile.md', 'a/b/z/.dotfile.md'];
289+
const fixtures = ['.gitignore', 'a/b/z/.dotfile', 'a/b/z/.dotfile.md', 'a/b/z/.dotfile.md', 'a/b/z/.dotfile.md'];
290290
assert(!isMatch('.gitignore', 'a/**/z/*.md'));
291291
assert(!isMatch('a/b/z/.dotfile', 'a/**/z/*.md'));
292292
assert(!isMatch('a/b/z/.dotfile.md', '**/c/.*.md'));

0 commit comments

Comments
 (0)