Skip to content

Commit

Permalink
remove escape regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Aug 16, 2017
1 parent acb5ad2 commit 26fb213
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
27 changes: 19 additions & 8 deletions src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '..')
Expand Down Expand Up @@ -73,7 +84,7 @@ module.exports = {
toRel(valueSplit
.slice(0, expectedNRelParents)
.concat(valueSplit.slice(valueNRelParents + diff))
.join('/'), '/')
.join('/'))
)
}

Expand Down

0 comments on commit 26fb213

Please sign in to comment.