[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Forbid certain elements
|
---|
| 3 | * @author Kenneth Chung
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | 'use strict';
|
---|
| 7 |
|
---|
| 8 | const has = require('hasown');
|
---|
| 9 | const docsUrl = require('../util/docsUrl');
|
---|
| 10 | const getText = require('../util/eslint').getText;
|
---|
| 11 | const isCreateElement = require('../util/isCreateElement');
|
---|
| 12 | const report = require('../util/report');
|
---|
| 13 |
|
---|
| 14 | // ------------------------------------------------------------------------------
|
---|
| 15 | // Rule Definition
|
---|
| 16 | // ------------------------------------------------------------------------------
|
---|
| 17 |
|
---|
| 18 | const messages = {
|
---|
| 19 | forbiddenElement: '<{{element}}> is forbidden',
|
---|
| 20 | forbiddenElement_message: '<{{element}}> is forbidden, {{message}}',
|
---|
| 21 | };
|
---|
| 22 |
|
---|
[0c6b92a] | 23 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
[d565449] | 24 | module.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 |
|
---|
[0c6b92a] | 109 | if (argument.type === 'Identifier' && /^[A-Z_]/.test(argument.name)) {
|
---|
[d565449] | 110 | reportIfForbidden(argument.name, argument);
|
---|
[0c6b92a] | 111 | } else if (argument.type === 'Literal' && /^[a-z][^.]*$/.test(String(argument.value))) {
|
---|
[d565449] | 112 | reportIfForbidden(argument.value, argument);
|
---|
[0c6b92a] | 113 | } else if (argument.type === 'MemberExpression') {
|
---|
[d565449] | 114 | reportIfForbidden(getText(context, argument), argument);
|
---|
| 115 | }
|
---|
| 116 | },
|
---|
| 117 | };
|
---|
| 118 | },
|
---|
| 119 | };
|
---|