Skip to content

Commit ac7f3f5

Browse files
committed
jsx-filename-extension: support shorthand fragments
1 parent 2165858 commit ac7f3f5

File tree

2 files changed

+57
-28
lines changed

2 files changed

+57
-28
lines changed

lib/rules/jsx-filename-extension.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,38 +43,41 @@ module.exports = {
4343
},
4444

4545
create: function(context) {
46+
let invalidExtension;
47+
let invalidNode;
48+
4649
function getExtensionsConfig() {
4750
return context.options[0] && context.options[0].extensions || DEFAULTS.extensions;
4851
}
4952

50-
let invalidExtension;
51-
let invalidNode;
53+
function handleJSX(node) {
54+
const filename = context.getFilename();
55+
if (filename === '<text>') {
56+
return;
57+
}
5258

53-
// --------------------------------------------------------------------------
54-
// Public
55-
// --------------------------------------------------------------------------
59+
if (invalidNode) {
60+
return;
61+
}
5662

57-
return {
58-
JSXElement: function(node) {
59-
const filename = context.getFilename();
60-
if (filename === '<text>') {
61-
return;
62-
}
63+
const allowedExtensions = getExtensionsConfig();
64+
const isAllowedExtension = allowedExtensions.some(extension => filename.slice(-extension.length) === extension);
6365

64-
if (invalidNode) {
65-
return;
66-
}
66+
if (isAllowedExtension) {
67+
return;
68+
}
6769

68-
const allowedExtensions = getExtensionsConfig();
69-
const isAllowedExtension = allowedExtensions.some(extension => filename.slice(-extension.length) === extension);
70+
invalidNode = node;
71+
invalidExtension = path.extname(filename);
72+
}
7073

71-
if (isAllowedExtension) {
72-
return;
73-
}
74+
// --------------------------------------------------------------------------
75+
// Public
76+
// --------------------------------------------------------------------------
7477

75-
invalidNode = node;
76-
invalidExtension = path.extname(filename);
77-
},
78+
return {
79+
JSXElement: handleJSX,
80+
JSXFragment: handleJSX,
7881

7982
'Program:exit': function() {
8083
if (!invalidNode) {

tests/lib/rules/jsx-filename-extension.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const parserOptions = {
2323
// Code Snippets
2424
// ------------------------------------------------------------------------------
2525

26-
const withJSX = 'module.exports = function MyComponent() { return <div>\n<div />\n</div>; }';
26+
const withJSXElement = 'module.exports = function MyComponent() { return <div>\n<div />\n</div>; }';
27+
const withJSXFragment = 'module.exports = function MyComponent() { return <>\n</>; }';
2728
const withoutJSX = 'module.exports = {}';
2829

2930
// ------------------------------------------------------------------------------
@@ -36,29 +37,54 @@ ruleTester.run('jsx-filename-extension', rule, {
3637
valid: [
3738
{
3839
filename: '<text>',
39-
code: withJSX
40+
code: withJSXElement
4041
},
4142
{
4243
filename: 'MyComponent.jsx',
43-
code: withJSX
44+
code: withJSXElement
4445
}, {
4546
filename: 'MyComponent.js',
4647
options: [{extensions: ['.js', '.jsx']}],
47-
code: withJSX
48+
code: withJSXElement
4849
}, {
4950
filename: 'notAComponent.js',
5051
code: withoutJSX
52+
}, {
53+
filename: '<text>',
54+
code: withJSXFragment,
55+
parser: 'babel-eslint'
56+
},
57+
{
58+
filename: 'MyComponent.jsx',
59+
code: withJSXFragment,
60+
parser: 'babel-eslint'
61+
}, {
62+
filename: 'MyComponent.js',
63+
options: [{extensions: ['.js', '.jsx']}],
64+
code: withJSXFragment,
65+
parser: 'babel-eslint'
5166
}
5267
],
5368

5469
invalid: [
5570
{
5671
filename: 'MyComponent.js',
57-
code: withJSX,
72+
code: withJSXElement,
73+
errors: [{message: 'JSX not allowed in files with extension \'.js\''}]
74+
}, {
75+
filename: 'MyComponent.jsx',
76+
code: withJSXElement,
77+
options: [{extensions: ['.js']}],
78+
errors: [{message: 'JSX not allowed in files with extension \'.jsx\''}]
79+
}, {
80+
filename: 'MyComponent.js',
81+
code: withJSXFragment,
82+
parser: 'babel-eslint',
5883
errors: [{message: 'JSX not allowed in files with extension \'.js\''}]
5984
}, {
6085
filename: 'MyComponent.jsx',
61-
code: withJSX,
86+
code: withJSXFragment,
87+
parser: 'babel-eslint',
6288
options: [{extensions: ['.js']}],
6389
errors: [{message: 'JSX not allowed in files with extension \'.jsx\''}]
6490
}

0 commit comments

Comments
 (0)