source: imaps-frontend/node_modules/eslint-plugin-react/lib/rules/style-prop-object.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[d565449]1/**
2 * @fileoverview Enforce style prop value is an object
3 * @author David Petersen
4 */
5
6'use strict';
7
8const variableUtil = require('../util/variable');
9const docsUrl = require('../util/docsUrl');
10const isCreateElement = require('../util/isCreateElement');
11const report = require('../util/report');
12
13// ------------------------------------------------------------------------------
14// Rule Definition
15// ------------------------------------------------------------------------------
16
17const messages = {
18 stylePropNotObject: 'Style prop value must be an object',
19};
20
21module.exports = {
22 meta: {
23 docs: {
24 description: 'Enforce style prop value is an object',
25 category: 'Possible Errors',
26 recommended: false,
27 url: docsUrl('style-prop-object'),
28 },
29
30 messages,
31
32 schema: [
33 {
34 type: 'object',
35 properties: {
36 allow: {
37 type: 'array',
38 items: {
39 type: 'string',
40 },
41 additionalItems: false,
42 uniqueItems: true,
43 },
44 },
45 },
46 ],
47 },
48
49 create(context) {
50 const allowed = new Set(((context.options.length > 0) && context.options[0].allow) || []);
51
52 /**
53 * @param {ASTNode} expression An Identifier node
54 * @returns {boolean}
55 */
56 function isNonNullaryLiteral(expression) {
57 return expression.type === 'Literal' && expression.value !== null;
58 }
59
60 /**
61 * @param {object} node A Identifier node
62 */
63 function checkIdentifiers(node) {
64 const variable = variableUtil.getVariableFromContext(context, node, node.name);
65
66 if (!variable || !variable.defs[0] || !variable.defs[0].node.init) {
67 return;
68 }
69
70 if (isNonNullaryLiteral(variable.defs[0].node.init)) {
71 report(context, messages.stylePropNotObject, 'stylePropNotObject', {
72 node,
73 });
74 }
75 }
76
77 return {
78 CallExpression(node) {
79 if (
80 isCreateElement(context, node)
81 && node.arguments.length > 1
82 ) {
83 if (node.arguments[0].name) {
84 // store name of component
85 const componentName = node.arguments[0].name;
86
87 // allowed list contains the name
88 if (allowed.has(componentName)) {
89 // abort operation
90 return;
91 }
92 }
93 if (node.arguments[1].type === 'ObjectExpression') {
94 const style = node.arguments[1].properties.find((property) => property.key && property.key.name === 'style' && !property.computed);
95 if (style) {
96 if (style.value.type === 'Identifier') {
97 checkIdentifiers(style.value);
98 } else if (isNonNullaryLiteral(style.value)) {
99 report(context, messages.stylePropNotObject, 'stylePropNotObject', {
100 node: style.value,
101 });
102 }
103 }
104 }
105 }
106 },
107
108 JSXAttribute(node) {
109 if (!node.value || node.name.name !== 'style') {
110 return;
111 }
112 // store parent element
113 const parentElement = node.parent;
114
115 // parent element is a JSXOpeningElement
116 if (parentElement && parentElement.type === 'JSXOpeningElement') {
117 // get the name of the JSX element
118 const name = parentElement.name && parentElement.name.name;
119
120 // allowed list contains the name
121 if (allowed.has(name)) {
122 // abort operation
123 return;
124 }
125 }
126
127 if (node.value.type !== 'JSXExpressionContainer' || isNonNullaryLiteral(node.value.expression)) {
128 report(context, messages.stylePropNotObject, 'stylePropNotObject', {
129 node,
130 });
131 } else if (node.value.expression.type === 'Identifier') {
132 checkIdentifiers(node.value.expression);
133 }
134 },
135 };
136 },
137};
Note: See TracBrowser for help on using the repository browser.