source: imaps-frontend/node_modules/eslint-plugin-react/lib/rules/jsx-filename-extension.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[d565449]1/**
2 * @fileoverview Restrict file extensions that may contain JSX
3 * @author Joe Lencioni
4 */
5
6'use strict';
7
8const path = require('path');
9const docsUrl = require('../util/docsUrl');
10const report = require('../util/report');
11
12// ------------------------------------------------------------------------------
13// Constants
14// ------------------------------------------------------------------------------
15
16const DEFAULTS = {
17 allow: 'always',
18 extensions: ['.jsx'],
19 ignoreFilesWithoutCode: false,
20};
21
22// ------------------------------------------------------------------------------
23// Rule Definition
24// ------------------------------------------------------------------------------
25
26const messages = {
27 noJSXWithExtension: 'JSX not allowed in files with extension \'{{ext}}\'',
28 extensionOnlyForJSX: 'Only files containing JSX may use the extension \'{{ext}}\'',
29};
30
31/** @type {import('eslint').Rule.RuleModule} */
32module.exports = {
33 meta: {
34 docs: {
35 description: 'Disallow file extensions that may contain JSX',
36 category: 'Stylistic Issues',
37 recommended: false,
38 url: docsUrl('jsx-filename-extension'),
39 },
40
41 messages,
42
43 schema: [{
44 type: 'object',
45 properties: {
46 allow: {
47 enum: ['always', 'as-needed'],
48 },
49 extensions: {
50 type: 'array',
51 items: {
52 type: 'string',
53 },
54 },
55 ignoreFilesWithoutCode: {
56 type: 'boolean',
57 },
58 },
59 additionalProperties: false,
60 }],
61 },
62
63 create(context) {
64 const filename = context.getFilename();
65
66 let jsxNode;
67
68 if (filename === '<text>') {
69 // No need to traverse any nodes.
70 return {};
71 }
72
73 const allow = (context.options[0] && context.options[0].allow) || DEFAULTS.allow;
74 const allowedExtensions = (context.options[0] && context.options[0].extensions) || DEFAULTS.extensions;
75 const ignoreFilesWithoutCode = (context.options[0] && context.options[0].ignoreFilesWithoutCode)
76 || DEFAULTS.ignoreFilesWithoutCode;
77 const isAllowedExtension = allowedExtensions.some((extension) => filename.slice(-extension.length) === extension);
78
79 function handleJSX(node) {
80 if (!jsxNode) {
81 jsxNode = node;
82 }
83 }
84
85 // --------------------------------------------------------------------------
86 // Public
87 // --------------------------------------------------------------------------
88
89 return {
90 JSXElement: handleJSX,
91 JSXFragment: handleJSX,
92
93 'Program:exit'(node) {
94 if (jsxNode) {
95 if (!isAllowedExtension) {
96 report(context, messages.noJSXWithExtension, 'noJSXWithExtension', {
97 node: jsxNode,
98 data: {
99 ext: path.extname(filename),
100 },
101 });
102 }
103 return;
104 }
105
106 if (isAllowedExtension && allow === 'as-needed') {
107 if (ignoreFilesWithoutCode && node.body.length === 0) {
108 return;
109 }
110 report(context, messages.extensionOnlyForJSX, 'extensionOnlyForJSX', {
111 node,
112 data: {
113 ext: path.extname(filename),
114 },
115 });
116 }
117 },
118 };
119 },
120};
Note: See TracBrowser for help on using the repository browser.