forked from import-js/eslint-plugin-import
-
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.
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 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
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,35 @@ | ||
/** | ||
* @fileOverview Ensures that an required path exists, given resolution rules. | ||
* @author Ben Mosher | ||
*/ | ||
import resolve from '../core/resolve' | ||
|
||
module.exports = function (context) { | ||
function checkSource(call) { | ||
if (call.callee.type !== 'Identifier') return | ||
if (call.callee.name !== 'require') return | ||
|
||
var requireArg = call.arguments[0]; | ||
var modules; | ||
|
||
if (requireArg.type === 'ArrayExpression') { | ||
modules = requireArg.elements; | ||
} else { | ||
modules = [requireArg]; | ||
} | ||
|
||
modules.forEach(function(module) { | ||
if (module.type !== 'Literal') return | ||
if (typeof module.value !== 'string') return | ||
|
||
if (resolve(module.value, context) == null) { | ||
context.report(module, | ||
'Unable to resolve path to module \'' + module.value + '\'.') | ||
} | ||
}); | ||
} | ||
|
||
return { | ||
'CallExpression': checkSource | ||
} | ||
} |
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,63 @@ | ||
var path = require('path') | ||
|
||
import { test } from '../utils' | ||
|
||
import { RuleTester } from 'eslint' | ||
|
||
var ruleTester = new RuleTester() | ||
, rule = require('../../../lib/rules/no-unresolved-require') | ||
|
||
ruleTester.run('no-unresolved-require', rule, { | ||
valid: [ | ||
test({ code: 'require("./bar");' }), | ||
test({ code: "require('fs');" }), | ||
test({ code: 'require(["./bar", "fs"]);' }), | ||
test({ | ||
code: "require('in-alternate-root');", | ||
settings: { | ||
'import/resolve': { | ||
'paths': [path.join( process.cwd() | ||
, 'tests', 'files', 'alternate-root')], | ||
}, | ||
}, | ||
}), | ||
], | ||
|
||
invalid: [ | ||
// should fail for jsx by default | ||
test({ code: 'require("jsx-module/foo")' | ||
, errors: [ {message: 'Unable to resolve path to ' + | ||
'module \'jsx-module/foo\'.'} ], | ||
}), | ||
|
||
test({ code: 'require(["fs", "jsx-module/foo"])' | ||
, errors: [ {message: 'Unable to resolve path to ' + | ||
'module \'jsx-module/foo\'.'} ], | ||
}), | ||
|
||
test({ code: 'require("./reallyfake/module")' | ||
, settings: { 'import/ignore': ['^\\./fake/'] } | ||
, errors: [{ message: 'Unable to resolve path to module ' + | ||
'\'./reallyfake/module\'.' }], | ||
}), | ||
|
||
test({ code: 'require(["jsx-module/foo", "./reallyfake/module"])' | ||
, settings: { 'import/ignore': ['^\\./fake/'] } | ||
, errors: [{ message: 'Unable to resolve path to module ' + | ||
'\'jsx-module/foo\'.' }, | ||
{ message: 'Unable to resolve path to module ' + | ||
'\'./reallyfake/module\'.' }], | ||
}), | ||
|
||
test({ | ||
code: "require('./baz');", | ||
errors: [{ message: "Unable to resolve path to module './baz'." | ||
, type: 'Literal', | ||
}]}), | ||
test({ | ||
code: "require('./empty-folder');", | ||
errors: [{ message: "Unable to resolve path to module './empty-folder'." | ||
, type: 'Literal', | ||
}]}), | ||
], | ||
}) |