1 | /**
|
---|
2 | * @fileoverview Forbid certain props on DOM Nodes
|
---|
3 | * @author David Vázquez
|
---|
4 | */
|
---|
5 |
|
---|
6 | 'use strict';
|
---|
7 |
|
---|
8 | const docsUrl = require('../util/docsUrl');
|
---|
9 | const report = require('../util/report');
|
---|
10 |
|
---|
11 | // ------------------------------------------------------------------------------
|
---|
12 | // Constants
|
---|
13 | // ------------------------------------------------------------------------------
|
---|
14 |
|
---|
15 | const DEFAULTS = [];
|
---|
16 |
|
---|
17 | // ------------------------------------------------------------------------------
|
---|
18 | // Rule Definition
|
---|
19 | // ------------------------------------------------------------------------------
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @param {Map<string, object>} forbidMap // { disallowList: null | string[], message: null | string }
|
---|
23 | * @param {string} prop
|
---|
24 | * @param {string} tagName
|
---|
25 | * @returns {boolean}
|
---|
26 | */
|
---|
27 | function isForbidden(forbidMap, prop, tagName) {
|
---|
28 | const options = forbidMap.get(prop);
|
---|
29 | return options && (
|
---|
30 | typeof tagName === 'undefined'
|
---|
31 | || !options.disallowList
|
---|
32 | || options.disallowList.indexOf(tagName) !== -1
|
---|
33 | );
|
---|
34 | }
|
---|
35 |
|
---|
36 | const messages = {
|
---|
37 | propIsForbidden: 'Prop "{{prop}}" is forbidden on DOM Nodes',
|
---|
38 | };
|
---|
39 |
|
---|
40 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
41 | module.exports = {
|
---|
42 | meta: {
|
---|
43 | docs: {
|
---|
44 | description: 'Disallow certain props on DOM Nodes',
|
---|
45 | category: 'Best Practices',
|
---|
46 | recommended: false,
|
---|
47 | url: docsUrl('forbid-dom-props'),
|
---|
48 | },
|
---|
49 |
|
---|
50 | messages,
|
---|
51 |
|
---|
52 | schema: [{
|
---|
53 | type: 'object',
|
---|
54 | properties: {
|
---|
55 | forbid: {
|
---|
56 | type: 'array',
|
---|
57 | items: {
|
---|
58 | anyOf: [{
|
---|
59 | type: 'string',
|
---|
60 | }, {
|
---|
61 | type: 'object',
|
---|
62 | properties: {
|
---|
63 | propName: {
|
---|
64 | type: 'string',
|
---|
65 | },
|
---|
66 | disallowedFor: {
|
---|
67 | type: 'array',
|
---|
68 | uniqueItems: true,
|
---|
69 | items: {
|
---|
70 | type: 'string',
|
---|
71 | },
|
---|
72 | },
|
---|
73 | message: {
|
---|
74 | type: 'string',
|
---|
75 | },
|
---|
76 | },
|
---|
77 | }],
|
---|
78 | minLength: 1,
|
---|
79 | },
|
---|
80 | uniqueItems: true,
|
---|
81 | },
|
---|
82 | },
|
---|
83 | additionalProperties: false,
|
---|
84 | }],
|
---|
85 | },
|
---|
86 |
|
---|
87 | create(context) {
|
---|
88 | const configuration = context.options[0] || {};
|
---|
89 | const forbid = new Map((configuration.forbid || DEFAULTS).map((value) => {
|
---|
90 | const propName = typeof value === 'string' ? value : value.propName;
|
---|
91 | return [propName, {
|
---|
92 | disallowList: typeof value === 'string' ? null : (value.disallowedFor || null),
|
---|
93 | message: typeof value === 'string' ? null : value.message,
|
---|
94 | }];
|
---|
95 | }));
|
---|
96 |
|
---|
97 | return {
|
---|
98 | JSXAttribute(node) {
|
---|
99 | const tag = node.parent.name.name;
|
---|
100 | if (!(tag && typeof tag === 'string' && tag[0] !== tag[0].toUpperCase())) {
|
---|
101 | // This is a Component, not a DOM node, so exit.
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | const prop = node.name.name;
|
---|
106 |
|
---|
107 | if (!isForbidden(forbid, prop, tag)) {
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 | const customMessage = forbid.get(prop).message;
|
---|
112 |
|
---|
113 | report(context, customMessage || messages.propIsForbidden, !customMessage && 'propIsForbidden', {
|
---|
114 | node,
|
---|
115 | data: {
|
---|
116 | prop,
|
---|
117 | },
|
---|
118 | });
|
---|
119 | },
|
---|
120 | };
|
---|
121 | },
|
---|
122 | };
|
---|