Skip to content

Commit

Permalink
add no-redundant-path-segments rule Fixes import-js#471
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Grainger committed Aug 8, 2017
1 parent dd28130 commit 8529eb2
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/rules/no-redundant-path-segments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @fileOverview Ensures that there are no redundant path segments
* @author Thomas Grainger
*/

import path from 'path';
import _ from 'lodash/fp';
import resolve from 'eslint-module-utils/resolve'
import moduleVisitor from 'eslint-module-utils/moduleVisitor'

function stripExtname(fn) {
const { dir, name } = path.parse(fn);
return path.format(dir, name);
}

const cartesianProduct =
_.reduce((a, b) =>
_.flatMap(x =>
_.map(y =>
x.concat([y])
)(b)
)(a)
)([[]]);


const anyMatch = _.flow(
cartesianProduct,
_.some(([a, b]) => a === b)
);

module.exports = {
meta: {},

create: function (context) {
const currentPath = context.getFilename();
const currentPathStripped = stripExtname(currentPath);

function checkSourceValue(source) {
const { value } = source;
const resolvedPath = resolve(source, context)
if (resolvedPath === undefined) {
return;
}

const expected = path.relative(currentPath, resolvedPath);
const matched = anyMatch([
[value, stripExtname(expected)],
[expected, stripExtname(expected)]
]);

if (!matched) {
context.report({
node: source,
message: `Redundant paths for "${value}", should be ${expected}`
})
}
}

return moduleVisitor(checkSourceValue, context.options[0])
},
}

0 comments on commit 8529eb2

Please sign in to comment.