forked from import-js/eslint-plugin-import
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add no-redundant-path-segments rule Fixes import-js#471
- Loading branch information
Showing
3 changed files
with
74 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,46 @@ | ||
/** | ||
* @fileOverview Ensures that there are no redundant path segments | ||
* @author Thomas Grainger | ||
*/ | ||
|
||
import path from 'path' | ||
import resolve from 'eslint-module-utils/resolve' | ||
import moduleVisitor from 'eslint-module-utils/moduleVisitor' | ||
|
||
function relative(from, to) { | ||
const rel = path.relative(from, to) | ||
return rel.startsWith('.') ? rel : `.${path.sep}${rel}` | ||
} | ||
|
||
module.exports = { | ||
meta: {}, | ||
|
||
create: function (context) { | ||
const currentDir = path.dirname(context.getFilename()) | ||
|
||
function checkSourceValue(source) { | ||
const { value } = source | ||
if (!value.startsWith('.')) { | ||
return | ||
} | ||
|
||
const resolvedPath = resolve(value, context) | ||
if (resolvedPath === undefined) { | ||
return | ||
} | ||
|
||
const expected = path.parse(relative(currentDir, resolvedPath)) | ||
const valueParsed = path.parse(value) | ||
|
||
if (valueParsed.dir !== expected.dir) { | ||
const proposed = path.format({ dir: expected.dir, base: valueParsed.base }) | ||
context.report({ | ||
node: source, | ||
message: `Redundant path segments for "${value}", should be "${proposed}"`, | ||
}) | ||
} | ||
} | ||
|
||
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,27 @@ | ||
import { test } from '../utils' | ||
import { RuleTester } from 'eslint' | ||
|
||
const ruleTester = new RuleTester() | ||
const rule = require('rules/no-redundant-path-segments') | ||
|
||
function runResolverTests(resolver) { | ||
ruleTester.run(`no-redundant-path-segments (${resolver})`, rule, { | ||
valid: [ | ||
test({ code: 'import "./malformed.js"' }), | ||
test({ code: 'import fs from "fs"' }), | ||
], | ||
|
||
invalid: [ | ||
test({ | ||
code: 'import "./../files/malformed.js"', | ||
errors: [ 'Redundant path segments for "./../files/malformed.js", should be "./malformed.js"'], | ||
}), | ||
test({ | ||
code: 'import "./../files/malformed"', | ||
errors: [ 'Redundant path segments for "./../files/malformed", should be "./malformed"'], | ||
}), | ||
], | ||
}) | ||
} | ||
|
||
['node', 'webpack'].forEach(runResolverTests) |