-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #912 from graingert/no-useless-path-segments
add no-useless-path-segments rule Fixes #471
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @fileOverview Ensures that there are no useless path segments | ||
* @author Thomas Grainger | ||
*/ | ||
|
||
import path from 'path' | ||
import sumBy from 'lodash/sumBy' | ||
import resolve from 'eslint-module-utils/resolve' | ||
import moduleVisitor from 'eslint-module-utils/moduleVisitor' | ||
|
||
/** | ||
* convert a potentially 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 /^((\.\.)|(\.))($|\/)/.test(stripped) ? stripped : `./${stripped}` | ||
} | ||
|
||
function normalize(fn) { | ||
return toRel(path.posix.normalize(fn)) | ||
} | ||
|
||
const countRelParent = x => sumBy(x, v => v === '..') | ||
|
||
module.exports = { | ||
meta: { fixable: 'code' }, | ||
|
||
create: function (context) { | ||
const currentDir = path.dirname(context.getFilename()) | ||
|
||
function checkSourceValue(source) { | ||
const { value } = source | ||
|
||
function report(proposed) { | ||
context.report({ | ||
node: source, | ||
message: `Useless path segments for "${value}", should be "${proposed}"`, | ||
fix: fixer => fixer.replaceText(source, JSON.stringify(proposed)), | ||
}) | ||
} | ||
|
||
if (!value.startsWith('.')) { | ||
return | ||
} | ||
|
||
const resolvedPath = resolve(value, context) | ||
const normed = normalize(value) | ||
if (normed !== value && resolvedPath === resolve(normed, context)) { | ||
return report(normed) | ||
} | ||
|
||
if (value.startsWith('./')) { | ||
return | ||
} | ||
|
||
if (resolvedPath === undefined) { | ||
return | ||
} | ||
|
||
const expected = path.relative(currentDir, resolvedPath) | ||
const expectedSplit = expected.split(path.sep) | ||
const valueSplit = value.replace(/^\.\//, '').split('/') | ||
const valueNRelParents = countRelParent(valueSplit) | ||
const expectedNRelParents = countRelParent(expectedSplit) | ||
const diff = valueNRelParents - expectedNRelParents | ||
|
||
if (diff <= 0) { | ||
return | ||
} | ||
|
||
return report( | ||
toRel(valueSplit | ||
.slice(0, expectedNRelParents) | ||
.concat(valueSplit.slice(valueNRelParents + diff)) | ||
.join('/')) | ||
) | ||
} | ||
|
||
return moduleVisitor(checkSourceValue, context.options[0]) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 4 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { test } from '../utils' | ||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
const rule = require('rules/no-useless-path-segments') | ||
|
||
function runResolverTests(resolver) { | ||
ruleTester.run(`no-useless-path-segments (${resolver})`, rule, { | ||
valid: [ | ||
test({ code: 'import "./malformed.js"' }), | ||
test({ code: 'import "./test-module"' }), | ||
test({ code: 'import "./bar/"' }), | ||
test({ code: 'import "."' }), | ||
test({ code: 'import ".."' }), | ||
test({ code: 'import fs from "fs"' }), | ||
], | ||
|
||
invalid: [ | ||
test({ | ||
code: 'import "./../files/malformed.js"', | ||
errors: [ 'Useless path segments for "./../files/malformed.js", should be "../files/malformed.js"'], | ||
}), | ||
test({ | ||
code: 'import "./../files/malformed"', | ||
errors: [ 'Useless path segments for "./../files/malformed", should be "../files/malformed"'], | ||
}), | ||
test({ | ||
code: 'import "../files/malformed.js"', | ||
errors: [ 'Useless path segments for "../files/malformed.js", should be "./malformed.js"'], | ||
}), | ||
test({ | ||
code: 'import "../files/malformed"', | ||
errors: [ 'Useless path segments for "../files/malformed", should be "./malformed"'], | ||
}), | ||
test({ | ||
code: 'import "./test-module/"', | ||
errors: [ 'Useless path segments for "./test-module/", should be "./test-module"'], | ||
}), | ||
test({ | ||
code: 'import "./"', | ||
errors: [ 'Useless path segments for "./", should be "."'], | ||
}), | ||
test({ | ||
code: 'import "../"', | ||
errors: [ 'Useless path segments for "../", should be ".."'], | ||
}), | ||
test({ | ||
code: 'import "./deep//a"', | ||
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'], | ||
}), | ||
], | ||
}) | ||
} | ||
|
||
['node', 'webpack'].forEach(runResolverTests) |