Skip to content

Commit

Permalink
Handle edge-case
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-andrews committed Oct 13, 2017
1 parent ba36909 commit 7751bc3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ module.exports = {

create: function (context) {
const currentDir = path.dirname(context.getFilename())
const resolvesToSameModule = (path1, path2) => {
try {
return require.resolve((path1)) === require.resolve((path2))
} catch(e) {
return true
}
}

function checkSourceValue(source) {
const { value } = source
Expand All @@ -54,7 +61,7 @@ module.exports = {
}

const normed = normalize(value)
if (normed !== value) {
if (normed !== value && resolvesToSameModule(value, normed)) {
return report(normed)
}

Expand Down
1 change: 1 addition & 0 deletions tests/files/bar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 4
4 changes: 2 additions & 2 deletions tests/src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ function runResolverTests(resolver) {
errors: [ 'Useless path segments for "../", should be ".."'],
}),
test({
code: 'import "./files//malformed"',
errors: [ 'Useless path segments for "./files//malformed", should be "./files/malformed"'],
code: 'import "./deep//a"',
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'],
}),
],
})
Expand Down

3 comments on commit 7751bc3

@graingert
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't use require.resolve you have to use the resolver plugin

@danny-andrews
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, whoops

@danny-andrews
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ee1c316.

Please sign in to comment.