From 26fb2139b1bb7c839d8fcc41fab3140c0b52d651 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 9 Aug 2017 10:20:33 +0100 Subject: [PATCH] remove escape regexp --- package.json | 1 - src/rules/no-useless-path-segments.js | 27 +++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index b153512161..e135a84d1e 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,6 @@ "eslint-module-utils": "^2.1.1", "has": "^1.0.1", "lodash.cond": "^4.3.0", - "lodash.escaperegexp": "^4.1.2", "lodash.sumby": "^4.3.0", "minimatch": "^3.0.3", "read-pkg-up": "^2.0.0" diff --git a/src/rules/no-useless-path-segments.js b/src/rules/no-useless-path-segments.js index 562206b180..423cfa8b2f 100644 --- a/src/rules/no-useless-path-segments.js +++ b/src/rules/no-useless-path-segments.js @@ -5,20 +5,31 @@ import path from 'path' import sumBy from 'lodash.sumby' -import escapeRegExp from 'lodash.escaperegexp' import resolve from 'eslint-module-utils/resolve' import moduleVisitor from 'eslint-module-utils/moduleVisitor' -function toRel(rel, sep) { - const rSep = escapeRegExp(sep) - const stripped = rel.replace(new RegExp(`${rSep}$`, 'g'), '') - return stripped.match(new RegExp(`^((\\.\\.)|(\\.))($|${rSep})`)) ? +/** + * convert a potentialy relative path from node utils into a true + * relative path. + * + * ../ -> .. + * ./ -> . + * .foo/bar -> ./.foo/bar + * ..foo/bar -> ./..foo/bar + * foo/bar -> ./foo/bar + * + * @param rel {string} relative posix path potentially missing leading './' + * @returns {string} relative posix path that always starts with a ./ + **/ +function toRel(rel) { + const stripped = rel.replace(/\/$/g, '') + return stripped.match(/^((\.\.)|(\.))($|\/)/) ? stripped : - `.${sep}${stripped}` + `./${stripped}` } function normalize(fn) { - return toRel(path.posix.normalize(fn), '/') + return toRel(path.posix.normalize(fn)) } const countRelParent = x => sumBy(x, v => v === '..') @@ -73,7 +84,7 @@ module.exports = { toRel(valueSplit .slice(0, expectedNRelParents) .concat(valueSplit.slice(valueNRelParents + diff)) - .join('/'), '/') + .join('/')) ) }