diff --git a/lib/rules/jsx-filename-extension.js b/lib/rules/jsx-filename-extension.js index fe433d6ecb..c210a552b1 100644 --- a/lib/rules/jsx-filename-extension.js +++ b/lib/rules/jsx-filename-extension.js @@ -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 === '') { - return; - } - - if (invalidNode) { - return; - } + if (filename === '') { + // 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); } // -------------------------------------------------------------------------- @@ -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}'` - }); } }; }