source: imaps-frontend/node_modules/eslint-plugin-react/lib/rules/jsx-props-no-spreading.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: 4.1 KB
Line 
1/**
2 * @fileoverview Prevent JSX prop spreading
3 * @author Ashish Gambhir
4 */
5
6'use strict';
7
8const docsUrl = require('../util/docsUrl');
9const report = require('../util/report');
10
11// ------------------------------------------------------------------------------
12// Constants
13// ------------------------------------------------------------------------------
14
15const OPTIONS = { ignore: 'ignore', enforce: 'enforce' };
16const DEFAULTS = {
17 html: OPTIONS.enforce,
18 custom: OPTIONS.enforce,
19 explicitSpread: OPTIONS.enforce,
20 exceptions: [],
21};
22
23const isException = (tag, allExceptions) => allExceptions.indexOf(tag) !== -1;
24const isProperty = (property) => property.type === 'Property';
25const getTagNameFromMemberExpression = (node) => {
26 if (node.property.parent) {
27 return `${node.property.parent.object.name}.${node.property.name}`;
28 }
29 // for eslint 3
30 return `${node.object.name}.${node.property.name}`;
31};
32
33// ------------------------------------------------------------------------------
34// Rule Definition
35// ------------------------------------------------------------------------------
36
37const messages = {
38 noSpreading: 'Prop spreading is forbidden',
39};
40
41/** @type {import('eslint').Rule.RuleModule} */
42module.exports = {
43 meta: {
44 docs: {
45 description: 'Disallow JSX prop spreading',
46 category: 'Best Practices',
47 recommended: false,
48 url: docsUrl('jsx-props-no-spreading'),
49 },
50
51 messages,
52
53 schema: [{
54 allOf: [{
55 type: 'object',
56 properties: {
57 html: {
58 enum: [OPTIONS.enforce, OPTIONS.ignore],
59 },
60 custom: {
61 enum: [OPTIONS.enforce, OPTIONS.ignore],
62 },
63 explicitSpread: {
64 enum: [OPTIONS.enforce, OPTIONS.ignore],
65 },
66 exceptions: {
67 type: 'array',
68 items: {
69 type: 'string',
70 uniqueItems: true,
71 },
72 },
73 },
74 }, {
75 not: {
76 type: 'object',
77 required: ['html', 'custom'],
78 properties: {
79 html: {
80 enum: [OPTIONS.ignore],
81 },
82 custom: {
83 enum: [OPTIONS.ignore],
84 },
85 exceptions: {
86 type: 'array',
87 minItems: 0,
88 maxItems: 0,
89 },
90 },
91 },
92 }],
93 }],
94 },
95
96 create(context) {
97 const configuration = context.options[0] || {};
98 const ignoreHtmlTags = (configuration.html || DEFAULTS.html) === OPTIONS.ignore;
99 const ignoreCustomTags = (configuration.custom || DEFAULTS.custom) === OPTIONS.ignore;
100 const ignoreExplicitSpread = (configuration.explicitSpread || DEFAULTS.explicitSpread) === OPTIONS.ignore;
101 const exceptions = configuration.exceptions || DEFAULTS.exceptions;
102 return {
103 JSXSpreadAttribute(node) {
104 const jsxOpeningElement = node.parent.name;
105 const type = jsxOpeningElement.type;
106
107 let tagName;
108 if (type === 'JSXIdentifier') {
109 tagName = jsxOpeningElement.name;
110 } else if (type === 'JSXMemberExpression') {
111 tagName = getTagNameFromMemberExpression(jsxOpeningElement);
112 } else {
113 tagName = undefined;
114 }
115
116 const isHTMLTag = tagName && tagName[0] !== tagName[0].toUpperCase();
117 const isCustomTag = tagName && (tagName[0] === tagName[0].toUpperCase() || tagName.includes('.'));
118 if (
119 isHTMLTag
120 && ((ignoreHtmlTags && !isException(tagName, exceptions))
121 || (!ignoreHtmlTags && isException(tagName, exceptions)))
122 ) {
123 return;
124 }
125 if (
126 isCustomTag
127 && ((ignoreCustomTags && !isException(tagName, exceptions))
128 || (!ignoreCustomTags && isException(tagName, exceptions)))
129 ) {
130 return;
131 }
132 if (
133 ignoreExplicitSpread
134 && node.argument.type === 'ObjectExpression'
135 && node.argument.properties.every(isProperty)
136 ) {
137 return;
138 }
139 report(context, messages.noSpreading, 'noSpreading', {
140 node,
141 });
142 },
143 };
144 },
145};
Note: See TracBrowser for help on using the repository browser.