Skip to content

Commit

Permalink
Getting closer to a working rule to enforce that a relative import re…
Browse files Browse the repository at this point in the history
…solves to an extant file.
  • Loading branch information
benmosher committed Mar 16, 2015
1 parent 48842ff commit d1d8b9e
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ insert_final_newline = true

[*.js]
indent_style = space
end_of_line = lf
indent_size = 2
end_of_line = lf
3 changes: 3 additions & 0 deletions files/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
ecmaFeatures:
modules: true
1 change: 1 addition & 0 deletions files/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function foo() { return "foo"; }
1 change: 1 addition & 0 deletions files/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo from './bar';
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exports.rules = {
"test-import-visitor": require("./lib/rules/test-import-visitor")
"test-import-visitor": require("./lib/rules/test-import-visitor"),
"valid-relative-path": require("./lib/rules/valid-relative-path")
};
31 changes: 31 additions & 0 deletions lib/rules/valid-relative-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @fileOverview Ensures that an imported relative path exists, relative to the currently linting file.
* @author Ben Mosher
*/

"use strict";

var path = require("path");

var RELATIVE_PATH = new RegExp("^(\.{1,2})?" + path.sep);

function isRelative(p) {
return RELATIVE_PATH.test(p);
}

function resolveImportPath(context, importPath) {
return path.resolve(path.dirname(context.getFilename()), importPath);
}

module.exports = function (context) {
return {
"ImportDeclaration": function (node) {
var importPath = node.source.value;
if (isRelative(importPath)) {
context.report(node, resolveImportPath(context, importPath));
}

// check file exists
}
};
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
},
"homepage": "https://github.com/benmosher/eslint-plugin-import",
"devDependencies": {
"eslint": "^0.17.0",
"chai": "^2.1.2",
"eslint": "^0.16.x",
"eslint-tester": "^0.6.0",
"mocha": "^2.2.1"
"mocha": "^2.2.1",
"object-assign": "^2.0.0"
}
}
3 changes: 3 additions & 0 deletions tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
env:
mocha: true
9 changes: 5 additions & 4 deletions tests/lib/rules/test-import-visitor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"use strict";

var linter = require("eslint").linter,
ESLintTester = require("eslint-tester"),
eslintTester = new ESLintTester(linter);
ESLintTester = require("eslint-tester");

var eslintTester = new ESLintTester(linter);

eslintTester.addRuleTest("lib/rules/test-import-visitor", {
valid: [
"var validVariable = true;",
"var validVariable = true;"
],

invalid: [
Expand All @@ -16,4 +17,4 @@ eslintTester.addRuleTest("lib/rules/test-import-visitor", {
errors: [ { message: "This is an import!" } ]
}
]
});
});
48 changes: 48 additions & 0 deletions tests/lib/rules/valid-relative-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict";

var assign = require("object-assign");

var expect = require("chai").expect;

var path = require("path");

var linter = require("eslint").linter,
CLIEngine = require("eslint").CLIEngine,
ESLintTester = require("eslint-tester");

var eslintTester = new ESLintTester(linter);

function valid(c) {
return {
code: c,
ecmaFeatures: { modules: true}
};
}

function invalid(c, errors) {
return assign(valid(c), {errors: errors});
}

eslintTester.addRuleTest("lib/rules/valid-relative-path", {
valid: [
valid("import foo from 'bar';")
],

invalid: [
invalid("import foo from './bar/baz'",
[ { message: "<input>" } ])
]
});

describe("valid-relative-path: against actual files", function () {
var cli = new CLIEngine({
rulePaths: [path.resolve(process.cwd(), "lib/rules")],
rules: { "valid-relative-path": 2 }
});

it("should follow relative paths.", function () {
var report = cli.executeOnFiles([path.join(__dirname, "../../../files/foo.js")]);
console.log(report.results[0].messages);
expect(report.errorCount).to.equal(1);
});
});

0 comments on commit d1d8b9e

Please sign in to comment.