Skip to content

Commit

Permalink
Fix require-extension to ignore extension if it's part of the package…
Browse files Browse the repository at this point in the history
… name (fixes #319)
  • Loading branch information
yannickcr committed Nov 29, 2015
1 parent 2153154 commit 312d66a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/rules/require-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ var DEFAULTS = {
extentions: ['.jsx']
};

var PKG_REGEX = /^[^\.]((?!\/).)*$/;

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = function(context) {

function isPackage(id) {
return PKG_REGEX.test(id);
}

function isRequire(expression) {
return expression.callee.name === 'require';
}
Expand Down Expand Up @@ -53,8 +59,9 @@ module.exports = function(context) {

CallExpression: function(node) {
if (isRequire(node)) {
var ext = getExtension(getId(node));
if (isForbiddenExtension(ext)) {
var id = getId(node);
var ext = getExtension(id);
if (!isPackage(id) && isForbiddenExtension(ext)) {
context.report(node, 'Unable to require module with extension \'' + ext + '\'');
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/rules/require-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var RuleTester = require('eslint').RuleTester;

var REQUIRE_PACKAGE = 'require(\'eslint\')';

var REQUIRE_PACKAGE_JS = 'require(\'headroom.js\')';

var REQUIRE_JS = 'require(\'./index.js\')';

var REQUIRE_JSX = 'require(\'./index.jsx\')';
Expand Down Expand Up @@ -66,6 +68,9 @@ ruleTester.run('require-extension', rule, {
}, {
code: REQUIRE_JSX,
options: [{extensions: ['.js']}]
}, {
code: REQUIRE_PACKAGE_JS,
options: [{extensions: ['.js']}]
}
],

Expand Down

0 comments on commit 312d66a

Please sign in to comment.