forked from un-ts/eslint-plugin-import-x
-
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.
Getting closer to a working rule to enforce that a relative import re…
…solves to an extant file.
- Loading branch information
Showing
10 changed files
with
100 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ insert_final_newline = true | |
|
||
[*.js] | ||
indent_style = space | ||
end_of_line = lf | ||
indent_size = 2 | ||
end_of_line = lf |
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,3 @@ | ||
--- | ||
ecmaFeatures: | ||
modules: true |
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 @@ | ||
export function foo() { return "foo"; } |
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 @@ | ||
import foo from './bar'; |
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 |
---|---|---|
@@ -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") | ||
}; |
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,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 | ||
} | ||
}; | ||
}; |
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,3 @@ | ||
--- | ||
env: | ||
mocha: true |
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,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); | ||
}); | ||
}); |