-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jsx-filename-extension rule (fixes #495)
Some projects want to require that any file that uses JSX to end in .jsx, and others prefer to use .js. This rule can be used to enforce this. I see this as complimentary to require-extension.
- Loading branch information
Showing
5 changed files
with
172 additions
and
1 deletion.
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 @@ | ||
# Restrict file extensions that may contain JSX (jsx-filename-extension) | ||
|
||
## Rule Details | ||
|
||
The following pattern is considered a warning: | ||
|
||
```jsx | ||
// filename: MyComponent.js | ||
function MyComponent() { | ||
return <div />; | ||
} | ||
``` | ||
|
||
The following pattern is not considered a warning: | ||
|
||
```jsx | ||
// filename: MyComponent.jsx | ||
function MyComponent() { | ||
return <div />; | ||
} | ||
``` | ||
|
||
## Rule Options | ||
|
||
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 | ||
"rules": { | ||
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }], | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care about restricting the file extensions that may contain JSX. |
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,68 @@ | ||
/** | ||
* @fileoverview Restrict file extensions that may contain JSX | ||
* @author Joe Lencioni | ||
*/ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Constants | ||
// ------------------------------------------------------------------------------ | ||
|
||
var DEFAULTS = { | ||
extensions: ['.jsx'] | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
|
||
function getExtensionsConfig() { | ||
return context.options[0] && context.options[0].extensions || DEFAULTS.extensions; | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
JSXElement: function(node) { | ||
var allowedExtensions = getExtensionsConfig(); | ||
var filename = context.getFilename(); | ||
|
||
var isAllowedExtension = allowedExtensions.some(function (extension) { | ||
return filename.slice(-extension.length) === extension; | ||
}); | ||
|
||
if (isAllowedExtension) { | ||
return; | ||
} | ||
|
||
var extension = path.extname(filename); | ||
|
||
context.report({ | ||
node: node, | ||
message: 'JSX not allowed in files with extension \'' + extension + '\'' | ||
}); | ||
} | ||
}; | ||
|
||
}; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @fileoverview Restrict file extensions that may contain JSX | ||
* @author Joe Lencioni | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/jsx-filename-extension'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
var parserOptions = { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Code Snippets | ||
// ------------------------------------------------------------------------------ | ||
|
||
var withJSX = 'module.exports = function MyComponent() { return <div />; }'; | ||
var withoutJSX = 'module.exports = {}'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('jsx-filename-extension', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'MyComponent.jsx', | ||
code: withJSX, | ||
parserOptions: parserOptions | ||
}, { | ||
filename: 'MyComponent.js', | ||
options: [{extensions: ['.js', '.jsx']}], | ||
code: withJSX, | ||
parserOptions: parserOptions | ||
}, { | ||
filename: 'notAComponent.js', | ||
code: withoutJSX | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'MyComponent.js', | ||
code: withJSX, | ||
parserOptions: parserOptions, | ||
errors: [{message: 'JSX not allowed in files with extension \'.js\''}] | ||
}, { | ||
filename: 'MyComponent.jsx', | ||
code: withJSX, | ||
parserOptions: parserOptions, | ||
options: [{extensions: ['.js']}], | ||
errors: [{message: 'JSX not allowed in files with extension \'.jsx\''}] | ||
} | ||
] | ||
|
||
}); |