UNPKG

2.33 kBJavaScriptView Raw
1/**
2 * @fileoverview Forbid certain props on DOM Nodes
3 * @author David Vázquez
4 */
5
6'use strict';
7
8const docsUrl = require('../util/docsUrl');
9
10// ------------------------------------------------------------------------------
11// Constants
12// ------------------------------------------------------------------------------
13
14const DEFAULTS = [];
15
16// ------------------------------------------------------------------------------
17// Rule Definition
18// ------------------------------------------------------------------------------
19
20module.exports = {
21 meta: {
22 docs: {
23 description: 'Forbid certain props on DOM Nodes',
24 category: 'Best Practices',
25 recommended: false,
26 url: docsUrl('forbid-dom-props')
27 },
28
29 schema: [{
30 type: 'object',
31 properties: {
32 forbid: {
33 type: 'array',
34 items: {
35 oneOf: [{
36 type: 'string'
37 }, {
38 type: 'object',
39 properties: {
40 propName: {
41 type: 'string'
42 },
43 message: {
44 type: 'string'
45 }
46 }
47 }],
48 minLength: 1
49 },
50 uniqueItems: true
51 }
52 },
53 additionalProperties: false
54 }]
55 },
56
57 create(context) {
58 const configuration = context.options[0] || {};
59 const forbid = new Map((configuration.forbid || DEFAULTS).map((value) => {
60 const propName = typeof value === 'string' ? value : value.propName;
61 const options = {
62 message: typeof value === 'string' ? null : value.message
63 };
64 return [propName, options];
65 }));
66
67 function isForbidden(prop) {
68 return forbid.has(prop);
69 }
70
71 return {
72 JSXAttribute(node) {
73 const tag = node.parent.name.name;
74 if (!(tag && tag[0] !== tag[0].toUpperCase())) {
75 // This is a Component, not a DOM node, so exit.
76 return;
77 }
78
79 const prop = node.name.name;
80
81 if (!isForbidden(prop)) {
82 return;
83 }
84
85 const customMessage = forbid.get(prop).message;
86 const errorMessage = customMessage || `Prop \`${prop}\` is forbidden on DOM Nodes`;
87
88 context.report({
89 node,
90 message: errorMessage
91 });
92 }
93 };
94 }
95};