Skip to content

Commit

Permalink
Change lets to consts. Move root import up.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Aug 21, 2019
1 parent 7816c64 commit 4840625
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 51 deletions.
60 changes: 30 additions & 30 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ const parse = (input, options) => {
}

// collapse slashes to reduce potential for exploits
let match = /^\\+/.exec(input.slice(state.index + 1));
const match = /^\\+/.exec(input.slice(state.index + 1));
let slashes = 0;

if (match && match[0].length > 2) {
Expand Down Expand Up @@ -348,15 +348,15 @@ const parse = (input, options) => {

if (state.brackets > 0 && (value !== ']' || prev.value === '[' || prev.value === '[^')) {
if (opts.posix !== false && value === ':') {
let inner = prev.value.slice(1);
const inner = prev.value.slice(1);
if (inner.includes('[')) {
prev.posix = true;

if (inner.includes(':')) {
let idx = prev.value.lastIndexOf('[');
let pre = prev.value.slice(0, idx);
let rest = prev.value.slice(idx + 2);
let posix = POSIX_REGEX_SOURCE[rest];
const idx = prev.value.lastIndexOf('[');
const pre = prev.value.slice(0, idx);
const rest = prev.value.slice(idx + 2);
const posix = POSIX_REGEX_SOURCE[rest];
if (posix) {
prev.value = pre + posix;
state.backtrack = true;
Expand Down Expand Up @@ -427,7 +427,7 @@ const parse = (input, options) => {
throw new SyntaxError(syntaxError('opening', '('));
}

let extglob = extglobs[extglobs.length - 1];
const extglob = extglobs[extglobs.length - 1];
if (extglob && state.parens === extglob.parens + 1) {
extglobClose(extglobs.pop());
continue;
Expand Down Expand Up @@ -474,7 +474,7 @@ const parse = (input, options) => {

decrement('brackets');

let prevValue = prev.value.slice(1);
const prevValue = prev.value.slice(1);
if (prev.posix !== true && prevValue[0] === '^' && !prevValue.includes('/')) {
value = '/' + value;
}
Expand All @@ -488,7 +488,7 @@ const parse = (input, options) => {
continue;
}

let escaped = utils.escapeRegex(prev.value);
const escaped = utils.escapeRegex(prev.value);
state.output = state.output.slice(0, -prev.value.length);

// when literal brackets are explicitly enabled
Expand Down Expand Up @@ -524,8 +524,8 @@ const parse = (input, options) => {
let output = ')';

if (state.dots === true) {
let arr = tokens.slice();
let range = [];
const arr = tokens.slice();
const range = [];

for (let i = arr.length - 1; i >= 0; i--) {
tokens.pop();
Expand Down Expand Up @@ -619,7 +619,7 @@ const parse = (input, options) => {

if (value === '?') {
if (prev && prev.type === 'paren') {
let next = peek();
const next = peek();
let output = value;

if (next === '<' && !utils.supportsLookbehinds()) {
Expand Down Expand Up @@ -677,7 +677,7 @@ const parse = (input, options) => {
}

if (prev && (prev.type === 'bracket' || prev.type === 'paren' || prev.type === 'brace')) {
let output = prev.extglob === true ? '\\' + value : value;
const output = prev.extglob === true ? '\\' + value : value;
push({ type: 'plus', value, output });
continue;
}
Expand Down Expand Up @@ -715,7 +715,7 @@ const parse = (input, options) => {
value = '\\' + value;
}

let match = REGEX_NON_SPECIAL_CHAR.exec(input.slice(state.index + 1));
const match = REGEX_NON_SPECIAL_CHAR.exec(input.slice(state.index + 1));
if (match) {
value += match[0];
state.index += match[0].length;
Expand Down Expand Up @@ -750,26 +750,26 @@ const parse = (input, options) => {
continue;
}

let prior = prev.prev;
let before = prior.prev;
let isStart = prior.type === 'slash' || prior.type === 'bos';
let afterStar = before && (before.type === 'star' || before.type === 'globstar');
const prior = prev.prev;
const before = prior.prev;
const isStart = prior.type === 'slash' || prior.type === 'bos';
const afterStar = before && (before.type === 'star' || before.type === 'globstar');

if (opts.bash === true && (!isStart || (!eos() && peek() !== '/'))) {
push({ type: 'star', value, output: '' });
continue;
}

let isBrace = state.braces > 0 && (prior.type === 'comma' || prior.type === 'brace');
let isExtglob = extglobs.length && (prior.type === 'pipe' || prior.type === 'paren');
const isBrace = state.braces > 0 && (prior.type === 'comma' || prior.type === 'brace');
const isExtglob = extglobs.length && (prior.type === 'pipe' || prior.type === 'paren');
if (!isStart && prior.type !== 'paren' && !isBrace && !isExtglob) {
push({ type: 'star', value, output: '' });
continue;
}

// strip consecutive `/**/`
while (input.slice(state.index + 1, state.index + 4) === '/**') {
let after = input[state.index + 4];
const after = input[state.index + 4];
if (after && after !== '/') {
break;
}
Expand Down Expand Up @@ -799,9 +799,9 @@ const parse = (input, options) => {
continue;
}

let next = peek();
const next = peek();
if (prior.type === 'slash' && prior.prev.type !== 'bos' && next === '/') {
let end = peek(2) !== void 0 ? '|$' : '';
const end = peek(2) !== void 0 ? '|$' : '';

state.output = state.output.slice(0, -(prior.output + prev.output).length);
prior.output = '(?:' + prior.output;
Expand Down Expand Up @@ -926,15 +926,15 @@ const parse = (input, options) => {
*/

parse.fastpaths = (input, options) => {
let opts = { ...options };
let max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
let len = input.length;
const opts = { ...options };
const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
const len = input.length;
if (len > max) {
throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
}

input = REPLACEMENTS[input] || input;
let win32 = utils.isWindows(options);
const win32 = utils.isWindows(options);

// create constants based on platform, for windows or posix
const {
Expand All @@ -949,10 +949,10 @@ parse.fastpaths = (input, options) => {
START_ANCHOR
} = constants.globChars(win32);

let capture = opts.capture ? '' : '?:';
const capture = opts.capture ? '' : '?:';
let star = opts.bash === true ? '.*?' : STAR;
let nodot = opts.dot ? NO_DOTS : NO_DOT;
let slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;
const nodot = opts.dot ? NO_DOTS : NO_DOT;
const slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;

if (opts.capture) {
star = `(${star})`;
Expand Down
31 changes: 16 additions & 15 deletions lib/picomatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const scan = require('./scan');
const parse = require('./parse');
const utils = require('./utils');
const constants = require('./constants');

/**
* Creates a matcher function from one or more glob patterns. The
Expand All @@ -29,10 +30,10 @@ const utils = require('./utils');

const picomatch = (glob, options, returnState = false) => {
if (Array.isArray(glob)) {
let fns = glob.map(input => picomatch(input, options, returnState));
const fns = glob.map(input => picomatch(input, options, returnState));
return str => {
for (let isMatch of fns) {
let state = isMatch(str);
const state = isMatch(str);
if (state) return state;
}
return false;
Expand All @@ -43,10 +44,10 @@ const picomatch = (glob, options, returnState = false) => {
throw new TypeError('Expected pattern to be a non-empty string');
}

let opts = options || {};
let posix = utils.isWindows(options);
let regex = picomatch.makeRe(glob, options, false, true);
let state = regex.state;
const opts = options || {};
const posix = utils.isWindows(options);
const regex = picomatch.makeRe(glob, options, false, true);
const state = regex.state;
delete regex.state;

let isIgnored = () => false;
Expand Down Expand Up @@ -115,8 +116,8 @@ picomatch.test = (input, regex, options, { glob, posix } = {}) => {
return { isMatch: false, output: '' };
}

let opts = options || {};
let format = opts.format || (posix ? utils.toPosixSlashes : null);
const opts = options || {};
const format = opts.format || (posix ? utils.toPosixSlashes : null);
let match = input === glob;
let output = (match && format) ? format(input) : input;

Expand Down Expand Up @@ -151,7 +152,7 @@ picomatch.test = (input, regex, options, { glob, posix } = {}) => {
*/

picomatch.matchBase = (input, glob, options, posix = utils.isWindows(options)) => {
let regex = glob instanceof RegExp ? glob : picomatch.makeRe(glob, options);
const regex = glob instanceof RegExp ? glob : picomatch.makeRe(glob, options);
return regex.test(path.basename(input));
};

Expand Down Expand Up @@ -235,9 +236,9 @@ picomatch.makeRe = (input, options, returnOutput = false, returnState = false) =
throw new TypeError('Expected a non-empty string');
}

let opts = options || {};
let prepend = opts.contains ? '' : '^';
let append = opts.contains ? '' : '$';
const opts = options || {};
const prepend = opts.contains ? '' : '^';
const append = opts.contains ? '' : '$';
let state = { negated: false, fastpaths: true };
let prefix = '';
let output;
Expand Down Expand Up @@ -266,7 +267,7 @@ picomatch.makeRe = (input, options, returnOutput = false, returnState = false) =
source = `^(?!${source}).*$`;
}

let regex = picomatch.toRegex(source, options);
const regex = picomatch.toRegex(source, options);
if (returnState === true) {
regex.state = state;
}
Expand All @@ -293,7 +294,7 @@ picomatch.makeRe = (input, options, returnOutput = false, returnState = false) =

picomatch.toRegex = (source, options) => {
try {
let opts = options || {};
const opts = options || {};
return new RegExp(source, opts.flags || (opts.nocase ? 'i' : ''));
} catch (err) {
if (options && options.debug === true) throw err;
Expand All @@ -306,7 +307,7 @@ picomatch.toRegex = (source, options) => {
* @return {Object}
*/

picomatch.constants = require('./constants');
picomatch.constants = constants;

/**
* Expose "picomatch"
Expand Down
8 changes: 4 additions & 4 deletions lib/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ const isPathSeparator = code => {
*/

module.exports = (input, options) => {
let opts = options || {};
let length = input.length - 1;
const opts = options || {};
const length = input.length - 1;
let index = -1;
let start = 0;
let lastIndex = 0;
Expand All @@ -55,8 +55,8 @@ module.exports = (input, options) => {

let braceEscaped = false;

let eos = () => index >= length;
let advance = () => {
const eos = () => index >= length;
const advance = () => {
prev = code;
return input.charCodeAt(++index);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports.removeBackslashes = str => {
}

exports.supportsLookbehinds = () => {
let segs = process.version.slice(1).split('.');
const segs = process.version.slice(1).split('.');
if (segs.length === 3 && +segs[0] >= 9 || (+segs[0] === 8 && +segs[1] >= 10)) {
return true;
}
Expand All @@ -34,7 +34,7 @@ exports.isWindows = options => {
};

exports.escapeLast = (input, char, lastIdx) => {
let idx = input.lastIndexOf(char, lastIdx);
const idx = input.lastIndexOf(char, lastIdx);
if (idx === -1) return input;
if (input[idx - 1] === '\\') return exports.escapeLast(input, char, idx - 1);
return input.slice(0, idx) + '\\' + input.slice(idx);
Expand Down

0 comments on commit 4840625

Please sign in to comment.