[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 |
|
---|
| 23 | module.exports = {
|
---|
| 24 | meta: {
|
---|
| 25 | docs: {
|
---|
| 26 | description: 'Disallow certain elements',
|
---|
| 27 | category: 'Best Practices',
|
---|
| 28 | recommended: false,
|
---|
| 29 | url: docsUrl('forbid-elements'),
|
---|
| 30 | },
|
---|
| 31 |
|
---|
| 32 | messages,
|
---|
| 33 |
|
---|
| 34 | schema: [{
|
---|
| 35 | type: 'object',
|
---|
| 36 | properties: {
|
---|
| 37 | forbid: {
|
---|
| 38 | type: 'array',
|
---|
| 39 | items: {
|
---|
| 40 | anyOf: [
|
---|
| 41 | { type: 'string' },
|
---|
| 42 | {
|
---|
| 43 | type: 'object',
|
---|
| 44 | properties: {
|
---|
| 45 | element: { type: 'string' },
|
---|
| 46 | message: { type: 'string' },
|
---|
| 47 | },
|
---|
| 48 | required: ['element'],
|
---|
| 49 | additionalProperties: false,
|
---|
| 50 | },
|
---|
| 51 | ],
|
---|
| 52 | },
|
---|
| 53 | },
|
---|
| 54 | },
|
---|
| 55 | additionalProperties: false,
|
---|
| 56 | }],
|
---|
| 57 | },
|
---|
| 58 |
|
---|
| 59 | create(context) {
|
---|
| 60 | const configuration = context.options[0] || {};
|
---|
| 61 | const forbidConfiguration = configuration.forbid || [];
|
---|
| 62 |
|
---|
| 63 | /** @type {Record<string, { element: string, message?: string }>} */
|
---|
| 64 | const indexedForbidConfigs = {};
|
---|
| 65 |
|
---|
| 66 | forbidConfiguration.forEach((item) => {
|
---|
| 67 | if (typeof item === 'string') {
|
---|
| 68 | indexedForbidConfigs[item] = { element: item };
|
---|
| 69 | } else {
|
---|
| 70 | indexedForbidConfigs[item.element] = item;
|
---|
| 71 | }
|
---|
| 72 | });
|
---|
| 73 |
|
---|
| 74 | function reportIfForbidden(element, node) {
|
---|
| 75 | if (has(indexedForbidConfigs, element)) {
|
---|
| 76 | const message = indexedForbidConfigs[element].message;
|
---|
| 77 |
|
---|
| 78 | report(
|
---|
| 79 | context,
|
---|
| 80 | message ? messages.forbiddenElement_message : messages.forbiddenElement,
|
---|
| 81 | message ? 'forbiddenElement_message' : 'forbiddenElement',
|
---|
| 82 | {
|
---|
| 83 | node,
|
---|
| 84 | data: {
|
---|
| 85 | element,
|
---|
| 86 | message,
|
---|
| 87 | },
|
---|
| 88 | }
|
---|
| 89 | );
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return {
|
---|
| 94 | JSXOpeningElement(node) {
|
---|
| 95 | reportIfForbidden(getText(context, node.name), node.name);
|
---|
| 96 | },
|
---|
| 97 |
|
---|
| 98 | CallExpression(node) {
|
---|
| 99 | if (!isCreateElement(context, node)) {
|
---|
| 100 | return;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | const argument = node.arguments[0];
|
---|
| 104 | if (!argument) {
|
---|
| 105 | return;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | const argType = argument.type;
|
---|
| 109 |
|
---|
| 110 | if (argType === 'Identifier' && /^[A-Z_]/.test(argument.name)) {
|
---|
| 111 | reportIfForbidden(argument.name, argument);
|
---|
| 112 | } else if (argType === 'Literal' && /^[a-z][^.]*$/.test(argument.value)) {
|
---|
| 113 | reportIfForbidden(argument.value, argument);
|
---|
| 114 | } else if (argType === 'MemberExpression') {
|
---|
| 115 | reportIfForbidden(getText(context, argument), argument);
|
---|
| 116 | }
|
---|
| 117 | },
|
---|
| 118 | };
|
---|
| 119 | },
|
---|
| 120 | };
|
---|