Skip to content

Commit

Permalink
Merge pull request #1128 from 1pete/fix-1127
Browse files Browse the repository at this point in the history
make rule `no-useless-path-segments` work with commonjs
  • Loading branch information
ljharb authored Jul 16, 2018
2 parents e5ee158 + 2c1886e commit 5480240
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ module.exports = {
url: docsUrl('no-useless-path-segments'),
},

schema: [
{
type: 'object',
properties: {
commonjs: { type: 'boolean' },
},
additionalProperties: false,
},
],

fixable: 'code',
},

Expand Down
47 changes: 47 additions & 0 deletions tests/src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const rule = require('rules/no-useless-path-segments')
function runResolverTests(resolver) {
ruleTester.run(`no-useless-path-segments (${resolver})`, rule, {
valid: [
// commonjs with default options
test({ code: 'require("./../files/malformed.js")' }),

// esmodule
test({ code: 'import "./malformed.js"' }),
test({ code: 'import "./test-module"' }),
test({ code: 'import "./bar/"' }),
Expand All @@ -16,6 +20,49 @@ function runResolverTests(resolver) {
],

invalid: [
// commonjs
test({
code: 'require("./../files/malformed.js")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./../files/malformed.js", should be "../files/malformed.js"'],
}),
test({
code: 'require("./../files/malformed")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./../files/malformed", should be "../files/malformed"'],
}),
test({
code: 'require("../files/malformed.js")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "../files/malformed.js", should be "./malformed.js"'],
}),
test({
code: 'require("../files/malformed")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "../files/malformed", should be "./malformed"'],
}),
test({
code: 'require("./test-module/")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./test-module/", should be "./test-module"'],
}),
test({
code: 'require("./")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./", should be "."'],
}),
test({
code: 'require("../")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "../", should be ".."'],
}),
test({
code: 'require("./deep//a")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'],
}),

// esmodule
test({
code: 'import "./../files/malformed.js"',
errors: [ 'Useless path segments for "./../files/malformed.js", should be "../files/malformed.js"'],
Expand Down

0 comments on commit 5480240

Please sign in to comment.