Skip to content

Commit

Permalink
[Refactor]: Simplify logic for jsx-filename-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Aug 8, 2020
1 parent 5a00096 commit da960ed
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions lib/rules/jsx-filename-extension.js
Expand Up @@ -44,32 +44,22 @@ module.exports = {
},

create(context) {
let invalidExtension;
let invalidNode;
const filename = context.getFilename();

function getExtensionsConfig() {
return context.options[0] && context.options[0].extensions || DEFAULTS.extensions;
}
let jsxNode;

function handleJSX(node) {
const filename = context.getFilename();
if (filename === '<text>') {
return;
}

if (invalidNode) {
return;
}
if (filename === '<text>') {
// No need to traverse any nodes.
return {};
}

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

if (isAllowedExtension) {
return;
function handleJSX(node) {
if (!jsxNode) {
jsxNode = node;
}

invalidNode = node;
invalidExtension = path.extname(filename);
}

// --------------------------------------------------------------------------
Expand All @@ -81,14 +71,14 @@ module.exports = {
JSXFragment: handleJSX,

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

context.report({
node: invalidNode,
message: `JSX not allowed in files with extension '${invalidExtension}'`
});
}
};
}
Expand Down

0 comments on commit da960ed

Please sign in to comment.