Skip to content

Commit

Permalink
New jsx-filename-extension: Add allow option
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Aug 8, 2020
1 parent da960ed commit 729678b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/rules/jsx-filename-extension.md
Expand Up @@ -22,6 +22,18 @@ function MyComponent() {

## Rule Options

### `allow` (default: `"always"`)

When to allow a JSX filename extension. By default all files may have a JSX extension. Set this to `as-needed` to only allow JSX file extensions in files that contain JSX syntax.

```js
"rules": {
"react/jsx-filename-extension": [1, { "allow": "as-needed" }]
}
```

### `extensions` (default: `[".jsx"]`)

The set of allowed extensions is configurable. By default '.jsx' is allowed. If you wanted to allow both '.jsx' and '.js', the configuration would be:

```js
Expand Down
15 changes: 14 additions & 1 deletion lib/rules/jsx-filename-extension.js
Expand Up @@ -13,6 +13,7 @@ const docsUrl = require('../util/docsUrl');
// ------------------------------------------------------------------------------

const DEFAULTS = {
allow: 'always',
extensions: ['.jsx']
};

Expand All @@ -32,6 +33,9 @@ module.exports = {
schema: [{
type: 'object',
properties: {
allow: {
enum: ['always', 'as-needed']
},
extensions: {
type: 'array',
items: {
Expand All @@ -53,6 +57,7 @@ module.exports = {
return {};
}

const allow = (context.options[0] && context.options[0].allow) || DEFAULTS.allow;
const allowedExtensions = (context.options[0] && context.options[0].extensions) || DEFAULTS.extensions;
const isAllowedExtension = allowedExtensions.some((extension) => filename.slice(-extension.length) === extension);

Expand All @@ -70,14 +75,22 @@ module.exports = {
JSXElement: handleJSX,
JSXFragment: handleJSX,

'Program:exit'() {
'Program:exit'(node) {
if (jsxNode) {
if (!isAllowedExtension) {
context.report({
node: jsxNode,
message: `JSX not allowed in files with extension '${path.extname(filename)}'`
});
}
return;
}

if (isAllowedExtension && allow === 'as-needed') {
context.report({
node,
message: `Only files containing JSX may use the extension '${path.extname(filename)}'`
});
}
}
};
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/jsx-filename-extension.js
Expand Up @@ -45,6 +45,14 @@ ruleTester.run('jsx-filename-extension', rule, {
{
filename: 'MyComponent.jsx',
code: withJSXElement
}, {
filename: 'MyComponent.js',
code: withoutJSX,
options: [{allow: 'as-needed'}]
}, {
filename: 'MyComponent.jsx',
code: withJSXElement,
options: [{allow: 'as-needed'}]
}, {
filename: 'MyComponent.js',
options: [{extensions: ['.js', '.jsx']}],
Expand Down Expand Up @@ -74,6 +82,11 @@ ruleTester.run('jsx-filename-extension', rule, {
filename: 'MyComponent.js',
code: withJSXElement,
errors: [{message: 'JSX not allowed in files with extension \'.js\''}]
}, {
filename: 'MyComponent.jsx',
code: withoutJSX,
options: [{allow: 'as-needed'}],
errors: [{message: 'Only files containing JSX may use the extension \'.jsx\''}]
}, {
filename: 'MyComponent.jsx',
code: withJSXElement,
Expand Down

0 comments on commit 729678b

Please sign in to comment.