forked from jsx-eslint/eslint-plugin-react
-
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.
Rename require-extension to jsx-require-extension
This rule is JSX-specific, not React-specific. Changing its name makes this clearer. I've kept the old one around as deprecated. We can remove it at the next major version bump. Addresses jsx-eslint#668 and jsx-eslint#686.
- Loading branch information
Showing
6 changed files
with
110 additions
and
66 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
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,88 @@ | ||
/** | ||
* @fileoverview Restrict file extensions that may be required | ||
* @author Scott Andrews | ||
*/ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Constants | ||
// ------------------------------------------------------------------------------ | ||
|
||
var DEFAULTS = { | ||
extensions: ['.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'; | ||
} | ||
|
||
function getId(expression) { | ||
return expression.arguments[0] && expression.arguments[0].value; | ||
} | ||
|
||
function getExtension(id) { | ||
return path.extname(id || ''); | ||
} | ||
|
||
function getExtensionsConfig() { | ||
return context.options[0] && context.options[0].extensions || DEFAULTS.extensions; | ||
} | ||
|
||
var forbiddenExtensions = getExtensionsConfig().reduce(function (extensions, extension) { | ||
extensions[extension] = true; | ||
return extensions; | ||
}, Object.create(null)); | ||
|
||
function isForbiddenExtension(ext) { | ||
return ext in forbiddenExtensions; | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
CallExpression: function(node) { | ||
if (isRequire(node)) { | ||
var id = getId(node); | ||
var ext = getExtension(id); | ||
if (!isPackage(id) && isForbiddenExtension(ext)) { | ||
context.report({ | ||
node: node, | ||
message: 'Unable to require module with extension \'' + ext + '\'' | ||
}); | ||
} | ||
} | ||
} | ||
|
||
}; | ||
|
||
}; | ||
|
||
module.exports.schema = [{ | ||
type: 'object', | ||
properties: { | ||
extensions: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
}, | ||
additionalProperties: false | ||
}]; |
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