Skip to content

Commit

Permalink
Rename require-extension to jsx-require-extension
Browse files Browse the repository at this point in the history
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
lencioni committed Jul 18, 2016
1 parent 2ed6995 commit 42d81ae
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 66 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ Finally, enable all of the rules that you would like to use. Use [our preset](#
* [react/prefer-stateless-function](docs/rules/prefer-stateless-function.md): Enforce stateless React Components to be written as a pure function
* [react/prop-types](docs/rules/prop-types.md): Prevent missing props validation in a React component definition
* [react/react-in-jsx-scope](docs/rules/react-in-jsx-scope.md): Prevent missing `React` when using JSX
* [react/require-extension](docs/rules/require-extension.md): Restrict file extensions that may be required
* [react/require-optimization](docs/rules/require-optimization.md): Enforce React components to have a shouldComponentUpdate method
* [react/require-render-return](docs/rules/require-render-return.md): Enforce ES5 or ES6 class for returning value in render function
* [react/self-closing-comp](docs/rules/self-closing-comp.md): Prevent extra closing tags for components without children
Expand Down Expand Up @@ -124,6 +123,7 @@ Finally, enable all of the rules that you would like to use. Use [our preset](#
* [react/jsx-no-target-blank](docs/rules/jsx-no-target-blank.md): Prevent usage of unsafe `target='_blank'`
* [react/jsx-no-undef](docs/rules/jsx-no-undef.md): Disallow undeclared variables in JSX
* [react/jsx-pascal-case](docs/rules/jsx-pascal-case.md): Enforce PascalCase for user-defined JSX components
* [react/jsx-require-extension](docs/rules/jsx-require-extension.md): Restrict file extensions that may be required
* [react/jsx-sort-props](docs/rules/jsx-sort-props.md): Enforce props alphabetical sorting
* [react/jsx-space-before-closing](docs/rules/jsx-space-before-closing.md): Validate spacing before closing bracket in JSX (fixable)
* [react/jsx-uses-react](docs/rules/jsx-uses-react.md): Prevent React to be incorrectly marked as unused
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Restrict file extensions that may be required (require-extension)
# Restrict file extensions that may be required (jsx-require-extension)

`require()` statements should generally not include a file extension as there is a well defined mechanism for resolving a module ID to a specific file. This rule inspects the module ID being required and creates a warning if the ID contains a '.jsx' file extension.

Expand Down Expand Up @@ -29,7 +29,7 @@ The set of forbidden extensions is configurable. By default '.jsx' is blocked. I

```js
"rules": {
"react/require-extension": [1, { "extensions": [".js", ".jsx"] }],
"react/jsx-require-extension": [1, { "extensions": [".js", ".jsx"] }],
}
```

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
'jsx-boolean-value': require('./lib/rules/jsx-boolean-value'),
'sort-comp': require('./lib/rules/sort-comp'),
'require-extension': require('./lib/rules/require-extension'),
'jsx-require-extension': require('./lib/rules/jsx-require-extension'),
'jsx-no-duplicate-props': require('./lib/rules/jsx-no-duplicate-props'),
'jsx-max-props-per-line': require('./lib/rules/jsx-max-props-per-line'),
'jsx-no-literals': require('./lib/rules/jsx-no-literals'),
Expand Down
88 changes: 88 additions & 0 deletions lib/rules/jsx-require-extension.js
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
}];
77 changes: 16 additions & 61 deletions lib/rules/require-extension.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,32 @@
/**
* @fileoverview Restrict file extensions that may be required
* @author Scott Andrews
* @deprecated
*/
'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;
}
var util = require('util');
var jsxRequireExtension = require('./jsx-require-extension');
var isWarnedForDeprecation = false;

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 = function(context) {
return util._extend(jsxRequireExtension(context), {
Program: function() {
if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) {
return;
}
}

};

/* eslint-disable no-console */
console.log('The react/require-extension rule is deprecated. Please ' +
'use the react/jsx-require-extension rule instead.');
/* eslint-enable no-console */
isWarnedForDeprecation = true;
}
});
};

module.exports.schema = [{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// Requirements
// ------------------------------------------------------------------------------

var rule = require('../../../lib/rules/require-extension');
var rule = require('../../../lib/rules/jsx-require-extension');
var RuleTester = require('eslint').RuleTester;

// ------------------------------------------------------------------------------
Expand All @@ -34,7 +34,7 @@ var REQUIRE_OBJECT = 'require({})';
// ------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run('require-extension', rule, {
ruleTester.run('jsx-require-extension', rule, {

valid: [
{
Expand Down

0 comments on commit 42d81ae

Please sign in to comment.