source: imaps-frontend/node_modules/eslint-plugin-react/lib/rules/forbid-elements.js@ 0c6b92a

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

Pred finalna verzija

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/**
2 * @fileoverview Forbid certain elements
3 * @author Kenneth Chung
4 */
5
6'use strict';
7
8const has = require('hasown');
9const docsUrl = require('../util/docsUrl');
10const getText = require('../util/eslint').getText;
11const isCreateElement = require('../util/isCreateElement');
12const report = require('../util/report');
13
14// ------------------------------------------------------------------------------
15// Rule Definition
16// ------------------------------------------------------------------------------
17
18const messages = {
19 forbiddenElement: '<{{element}}> is forbidden',
20 forbiddenElement_message: '<{{element}}> is forbidden, {{message}}',
21};
22
23/** @type {import('eslint').Rule.RuleModule} */
24module.exports = {
25 meta: {
26 docs: {
27 description: 'Disallow certain elements',
28 category: 'Best Practices',
29 recommended: false,
30 url: docsUrl('forbid-elements'),
31 },
32
33 messages,
34
35 schema: [{
36 type: 'object',
37 properties: {
38 forbid: {
39 type: 'array',
40 items: {
41 anyOf: [
42 { type: 'string' },
43 {
44 type: 'object',
45 properties: {
46 element: { type: 'string' },
47 message: { type: 'string' },
48 },
49 required: ['element'],
50 additionalProperties: false,
51 },
52 ],
53 },
54 },
55 },
56 additionalProperties: false,
57 }],
58 },
59
60 create(context) {
61 const configuration = context.options[0] || {};
62 const forbidConfiguration = configuration.forbid || [];
63
64 /** @type {Record<string, { element: string, message?: string }>} */
65 const indexedForbidConfigs = {};
66
67 forbidConfiguration.forEach((item) => {
68 if (typeof item === 'string') {
69 indexedForbidConfigs[item] = { element: item };
70 } else {
71 indexedForbidConfigs[item.element] = item;
72 }
73 });
74
75 function reportIfForbidden(element, node) {
76 if (has(indexedForbidConfigs, element)) {
77 const message = indexedForbidConfigs[element].message;
78
79 report(
80 context,
81 message ? messages.forbiddenElement_message : messages.forbiddenElement,
82 message ? 'forbiddenElement_message' : 'forbiddenElement',
83 {
84 node,
85 data: {
86 element,
87 message,
88 },
89 }
90 );
91 }
92 }
93
94 return {
95 JSXOpeningElement(node) {
96 reportIfForbidden(getText(context, node.name), node.name);
97 },
98
99 CallExpression(node) {
100 if (!isCreateElement(context, node)) {
101 return;
102 }
103
104 const argument = node.arguments[0];
105 if (!argument) {
106 return;
107 }
108
109 if (argument.type === 'Identifier' && /^[A-Z_]/.test(argument.name)) {
110 reportIfForbidden(argument.name, argument);
111 } else if (argument.type === 'Literal' && /^[a-z][^.]*$/.test(String(argument.value))) {
112 reportIfForbidden(argument.value, argument);
113 } else if (argument.type === 'MemberExpression') {
114 reportIfForbidden(getText(context, argument), argument);
115 }
116 },
117 };
118 },
119};
Note: See TracBrowser for help on using the repository browser.