@atlaskit/eslint-plugin-design-system 8.15.4 → 8.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/constellation/index/usage.mdx +62 -16
- package/dist/cjs/rules/ensure-design-token-usage/typography.js +2 -2
- package/dist/cjs/rules/prefer-primitives/index.js +2 -3
- package/dist/cjs/rules/prefer-primitives/utils.js +16 -5
- package/dist/cjs/rules/use-primitives/index.js +89 -58
- package/dist/cjs/rules/use-primitives/transformers/css-to-xcss.js +95 -0
- package/dist/cjs/rules/use-primitives/transformers/index.js +31 -0
- package/dist/cjs/rules/use-primitives/transformers/jsx-element-to-box.js +26 -0
- package/dist/cjs/rules/use-primitives/utils/contains-only-supported-attrs.js +19 -0
- package/dist/cjs/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.js +31 -0
- package/dist/cjs/rules/use-primitives/utils/get-attribute-value-identifier.js +37 -0
- package/dist/cjs/rules/use-primitives/utils/get-function-argument-at-pos.js +10 -0
- package/dist/cjs/rules/use-primitives/utils/get-jsx-attribute-by-name.js +16 -0
- package/dist/cjs/rules/use-primitives/utils/get-variable-definition-value.js +29 -0
- package/dist/cjs/rules/use-primitives/utils/get-variable-usage-count.js +21 -0
- package/dist/cjs/rules/use-primitives/utils/index.js +89 -0
- package/dist/cjs/rules/use-primitives/utils/is-function-named.js +19 -0
- package/dist/cjs/rules/use-primitives/utils/is-valid-tag-name.js +13 -0
- package/dist/cjs/rules/use-primitives/utils/update-jsx-attribute-by-name.js +31 -0
- package/dist/cjs/rules/use-primitives/utils/update-jsx-element-name.js +16 -0
- package/dist/cjs/rules/use-primitives/utils/upsert-import-declaration.js +80 -0
- package/dist/es2019/rules/ensure-design-token-usage/typography.js +1 -1
- package/dist/es2019/rules/prefer-primitives/index.js +1 -2
- package/dist/es2019/rules/prefer-primitives/utils.js +11 -2
- package/dist/es2019/rules/use-primitives/index.js +91 -60
- package/dist/es2019/rules/use-primitives/transformers/css-to-xcss.js +91 -0
- package/dist/es2019/rules/use-primitives/transformers/index.js +2 -0
- package/dist/es2019/rules/use-primitives/transformers/jsx-element-to-box.js +16 -0
- package/dist/es2019/rules/use-primitives/utils/contains-only-supported-attrs.js +13 -0
- package/dist/es2019/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.js +26 -0
- package/dist/es2019/rules/use-primitives/utils/get-attribute-value-identifier.js +32 -0
- package/dist/es2019/rules/use-primitives/utils/get-function-argument-at-pos.js +4 -0
- package/dist/es2019/rules/use-primitives/utils/get-jsx-attribute-by-name.js +10 -0
- package/dist/es2019/rules/use-primitives/utils/get-variable-definition-value.js +23 -0
- package/dist/es2019/rules/use-primitives/utils/get-variable-usage-count.js +15 -0
- package/dist/es2019/rules/use-primitives/utils/index.js +12 -0
- package/dist/es2019/rules/use-primitives/utils/is-function-named.js +13 -0
- package/dist/es2019/rules/use-primitives/utils/is-valid-tag-name.js +7 -0
- package/dist/es2019/rules/use-primitives/utils/update-jsx-attribute-by-name.js +26 -0
- package/dist/es2019/rules/use-primitives/utils/update-jsx-element-name.js +12 -0
- package/dist/es2019/rules/use-primitives/utils/upsert-import-declaration.js +76 -0
- package/dist/esm/rules/ensure-design-token-usage/typography.js +1 -1
- package/dist/esm/rules/prefer-primitives/index.js +1 -2
- package/dist/esm/rules/prefer-primitives/utils.js +13 -2
- package/dist/esm/rules/use-primitives/index.js +91 -60
- package/dist/esm/rules/use-primitives/transformers/css-to-xcss.js +88 -0
- package/dist/esm/rules/use-primitives/transformers/index.js +2 -0
- package/dist/esm/rules/use-primitives/transformers/jsx-element-to-box.js +19 -0
- package/dist/esm/rules/use-primitives/utils/contains-only-supported-attrs.js +13 -0
- package/dist/esm/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.js +26 -0
- package/dist/esm/rules/use-primitives/utils/get-attribute-value-identifier.js +32 -0
- package/dist/esm/rules/use-primitives/utils/get-function-argument-at-pos.js +4 -0
- package/dist/esm/rules/use-primitives/utils/get-jsx-attribute-by-name.js +10 -0
- package/dist/esm/rules/use-primitives/utils/get-variable-definition-value.js +23 -0
- package/dist/esm/rules/use-primitives/utils/get-variable-usage-count.js +15 -0
- package/dist/esm/rules/use-primitives/utils/index.js +12 -0
- package/dist/esm/rules/use-primitives/utils/is-function-named.js +13 -0
- package/dist/esm/rules/use-primitives/utils/is-valid-tag-name.js +7 -0
- package/dist/esm/rules/use-primitives/utils/update-jsx-attribute-by-name.js +24 -0
- package/dist/esm/rules/use-primitives/utils/update-jsx-element-name.js +10 -0
- package/dist/esm/rules/use-primitives/utils/upsert-import-declaration.js +75 -0
- package/dist/types/rules/prefer-primitives/utils.d.ts +2 -1
- package/dist/types/rules/use-primitives/transformers/css-to-xcss.d.ts +9 -0
- package/dist/types/rules/use-primitives/transformers/index.d.ts +2 -0
- package/dist/types/rules/use-primitives/transformers/jsx-element-to-box.d.ts +3 -0
- package/dist/types/rules/use-primitives/utils/contains-only-supported-attrs.d.ts +2 -0
- package/dist/types/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.d.ts +9 -0
- package/dist/types/rules/use-primitives/utils/get-attribute-value-identifier.d.ts +14 -0
- package/dist/types/rules/use-primitives/utils/get-function-argument-at-pos.d.ts +2 -0
- package/dist/types/rules/use-primitives/utils/get-jsx-attribute-by-name.d.ts +2 -0
- package/dist/types/rules/use-primitives/utils/get-variable-definition-value.d.ts +2 -0
- package/dist/types/rules/use-primitives/utils/get-variable-usage-count.d.ts +6 -0
- package/dist/types/rules/use-primitives/utils/index.d.ts +12 -0
- package/dist/types/rules/use-primitives/utils/is-function-named.d.ts +2 -0
- package/dist/types/rules/use-primitives/utils/is-valid-tag-name.d.ts +2 -0
- package/dist/types/rules/use-primitives/utils/update-jsx-attribute-by-name.d.ts +3 -0
- package/dist/types/rules/use-primitives/utils/update-jsx-element-name.d.ts +3 -0
- package/dist/types/rules/use-primitives/utils/upsert-import-declaration.d.ts +11 -0
- package/dist/types-ts4.5/rules/prefer-primitives/utils.d.ts +2 -1
- package/dist/types-ts4.5/rules/use-primitives/transformers/css-to-xcss.d.ts +9 -0
- package/dist/types-ts4.5/rules/use-primitives/transformers/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/use-primitives/transformers/jsx-element-to-box.d.ts +3 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/contains-only-supported-attrs.d.ts +2 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/convert-ast-object-expression-to-js-object.d.ts +9 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/get-attribute-value-identifier.d.ts +14 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/get-function-argument-at-pos.d.ts +2 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/get-jsx-attribute-by-name.d.ts +2 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/get-variable-definition-value.d.ts +2 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/get-variable-usage-count.d.ts +6 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/index.d.ts +12 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/is-function-named.d.ts +2 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/is-valid-tag-name.d.ts +2 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/update-jsx-attribute-by-name.d.ts +3 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/update-jsx-element-name.d.ts +3 -0
- package/dist/types-ts4.5/rules/use-primitives/utils/upsert-import-declaration.d.ts +11 -0
- package/package.json +3 -1
- package/dist/cjs/rules/use-primitives/utils.js +0 -364
- package/dist/es2019/rules/use-primitives/utils.js +0 -353
- package/dist/esm/rules/use-primitives/utils.js +0 -359
- package/dist/types/rules/use-primitives/utils.d.ts +0 -13
- package/dist/types-ts4.5/rules/use-primitives/utils.d.ts +0 -13
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.validPrimitiveElements = exports.shouldSuggestText = exports.shouldSuggestStack = exports.shouldSuggestInline = exports.shouldSuggestBox = exports.primitiveFixer = exports.isWhiteSpace = exports.isValidPrimitiveElement = exports.getChildrenByType = void 0;
|
|
8
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
9
|
-
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
10
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
11
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } // eslint-disable-next-line import/no-extraneous-dependencies
|
|
12
|
-
var shouldSuggestBox = exports.shouldSuggestBox = function shouldSuggestBox(node
|
|
13
|
-
// scope: Scope.Scope,
|
|
14
|
-
) {
|
|
15
|
-
if (!node) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
if (!isValidPrimitiveElement(node)) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Check for empty elements: `<div></div>` || `<span></span>`
|
|
24
|
-
*/
|
|
25
|
-
if (node.children.length === 0) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Check for elements containing only whitespace. e.g. `<div> </div>` || `<span> </span>`
|
|
31
|
-
*/
|
|
32
|
-
if (containsOnlyWhitespace(node)) {
|
|
33
|
-
return true;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* If an element contains more than one JSX child, then we don't want to convert it.
|
|
38
|
-
*/
|
|
39
|
-
var JSXChildren = getChildrenByType(node, ['JSXElement']);
|
|
40
|
-
if (JSXChildren.length > 1) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Check for things like:
|
|
46
|
-
* ```
|
|
47
|
-
* <div>
|
|
48
|
-
* <h2>heading</h2>
|
|
49
|
-
* subheading <= rejected because of standalone piece of text
|
|
50
|
-
* </div>
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
var nonWhiteSpaceTextChildren = getChildrenByType(node, ['JSXText']).filter(function (child) {
|
|
54
|
-
return !isWhiteSpace(child.value);
|
|
55
|
-
});
|
|
56
|
-
if (nonWhiteSpaceTextChildren.length > 0) {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Possible since we now know the div only has one child
|
|
61
|
-
// const singleChild = node.children[0];
|
|
62
|
-
|
|
63
|
-
// // let's narrow down the type inside of the expression
|
|
64
|
-
// if (isNodeOfType(singleChild, 'JSXExpressionContainer')) {
|
|
65
|
-
// const expression = singleChild.expression;
|
|
66
|
-
|
|
67
|
-
// // If an Identifier that is just a string then should be a <Text>, not <Box>
|
|
68
|
-
// const identifier =
|
|
69
|
-
// isNodeOfType(expression, 'Identifier') &&
|
|
70
|
-
// getIdentifierInParentScope(scope, expression.name);
|
|
71
|
-
|
|
72
|
-
// if (
|
|
73
|
-
// identifier &&
|
|
74
|
-
// (isVariableAnnotatedWithType(identifier, 'StringTypeAnnotation') ||
|
|
75
|
-
// isVariableAnnotatedWithType(identifier, 'TSStringKeyword'))
|
|
76
|
-
// ) {
|
|
77
|
-
// return false;
|
|
78
|
-
// }
|
|
79
|
-
// }
|
|
80
|
-
|
|
81
|
-
return true;
|
|
82
|
-
};
|
|
83
|
-
var shouldSuggestInline = exports.shouldSuggestInline = function shouldSuggestInline(node, context) {
|
|
84
|
-
if (!node) {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
if (!isValidPrimitiveElement(node)) {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
var cssStyleObject = getCSSPropStyleObject(node, context);
|
|
91
|
-
if (!cssStyleObject) {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
if (!hasInlineCompatibleStyles(cssStyleObject)) {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
var JSXChildren = getChildrenByType(node, ['JSXElement']);
|
|
98
|
-
if (JSXChildren.length < 2) {
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
return true;
|
|
102
|
-
};
|
|
103
|
-
var shouldSuggestStack = exports.shouldSuggestStack = function shouldSuggestStack(node, context) {
|
|
104
|
-
if (!node) {
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
if (!isValidPrimitiveElement(node)) {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
var cssStyleObject = getCSSPropStyleObject(node, context);
|
|
111
|
-
if (!cssStyleObject) {
|
|
112
|
-
return false;
|
|
113
|
-
}
|
|
114
|
-
if (!hasStackCompatibleStyles(cssStyleObject)) {
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
var JSXChildren = getChildrenByType(node, ['JSXElement']);
|
|
118
|
-
if (JSXChildren.length < 2) {
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
return true;
|
|
122
|
-
};
|
|
123
|
-
var shouldSuggestText = exports.shouldSuggestText = function shouldSuggestText(node, scope) {
|
|
124
|
-
if (!node) {
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// node doesn't have children then it should be a <Box>
|
|
129
|
-
if (!node.children || node.children.length === 0) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
var containsOnlyText = node.children.filter(function (child) {
|
|
133
|
-
return (0, _eslintCodemodUtils.isNodeOfType)(child, 'Literal') || (0, _eslintCodemodUtils.isNodeOfType)(child, 'JSXText');
|
|
134
|
-
});
|
|
135
|
-
if (containsOnlyText) {
|
|
136
|
-
return true;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// if the element contains strictly a single child... let's see what we can discover
|
|
140
|
-
if (node.children.length === 1) {
|
|
141
|
-
var singleChild = node.children[0];
|
|
142
|
-
|
|
143
|
-
// let's narrow down the type inside of the expression
|
|
144
|
-
if ((0, _eslintCodemodUtils.isNodeOfType)(singleChild, 'JSXExpressionContainer')) {
|
|
145
|
-
var expression = singleChild.expression;
|
|
146
|
-
|
|
147
|
-
// A direct Literal is another case for only <Text>
|
|
148
|
-
if ((0, _eslintCodemodUtils.isNodeOfType)(expression, 'Literal')) {
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// If an Identifier then we want to confirm it's just string to suggest only <Text>
|
|
153
|
-
var identifier = (0, _eslintCodemodUtils.isNodeOfType)(expression, 'Identifier') && (0, _eslintCodemodUtils.getIdentifierInParentScope)(scope, expression.name);
|
|
154
|
-
if (identifier && (isVariableAnnotatedWithType(identifier, 'StringTypeAnnotation') || isVariableAnnotatedWithType(identifier, 'TSStringKeyword'))) {
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return true;
|
|
160
|
-
};
|
|
161
|
-
var primitiveFixer = exports.primitiveFixer = function primitiveFixer(node, nodeName, context) {
|
|
162
|
-
return function (fixer) {
|
|
163
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXOpeningElement')) {
|
|
164
|
-
return [];
|
|
165
|
-
}
|
|
166
|
-
var parent = node.parent;
|
|
167
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(parent, 'JSXElement')) {
|
|
168
|
-
return [];
|
|
169
|
-
}
|
|
170
|
-
var fixes = [];
|
|
171
|
-
var body = context.getSourceCode().ast.body;
|
|
172
|
-
var imports = body.filter(function (node) {
|
|
173
|
-
return (0, _eslintCodemodUtils.isNodeOfType)(node, 'ImportDeclaration');
|
|
174
|
-
});
|
|
175
|
-
var primitivesNode = imports.find(function (node) {
|
|
176
|
-
return (0, _eslintCodemodUtils.hasImportDeclaration)(node, '@atlaskit/primitives');
|
|
177
|
-
});
|
|
178
|
-
if (!primitivesNode) {
|
|
179
|
-
fixes.push(fixer.insertTextBefore(body[0], "".concat((0, _eslintCodemodUtils.insertImportDeclaration)('@atlaskit/primitives', [nodeName]), ";\n")));
|
|
180
|
-
} else {
|
|
181
|
-
fixes.push(fixer.replaceText(primitivesNode, "".concat((0, _eslintCodemodUtils.insertImportSpecifier)(primitivesNode, nodeName), ";\n")));
|
|
182
|
-
}
|
|
183
|
-
var closingElement = parent.closingElement;
|
|
184
|
-
var jsxId = (0, _eslintCodemodUtils.jsxIdentifier)(nodeName);
|
|
185
|
-
var attributes = [];
|
|
186
|
-
|
|
187
|
-
// Box defaults to div. We only need to add an as prop if it's not a div
|
|
188
|
-
if (node.name.name !== 'div' && nodeName === 'Box') {
|
|
189
|
-
var asProp = (0, _eslintCodemodUtils.jsxAttribute)({
|
|
190
|
-
name: (0, _eslintCodemodUtils.jsxIdentifier)('as'),
|
|
191
|
-
value: (0, _eslintCodemodUtils.literal)({
|
|
192
|
-
value: "".concat(node.name.name),
|
|
193
|
-
raw: "\"".concat(node.name.name, "\"")
|
|
194
|
-
})
|
|
195
|
-
});
|
|
196
|
-
attributes.push(asProp);
|
|
197
|
-
}
|
|
198
|
-
var hasStylingProps = getJSXAttributeByName(node, 'style') || getJSXAttributeByName(node, 'css') || getJSXAttributeByName(node, 'class') || getJSXAttributeByName(node, 'className');
|
|
199
|
-
if (hasStylingProps) {
|
|
200
|
-
fixes.push(fixer.insertTextBefore(node, '// TODO: Manually convert styling into props\n'));
|
|
201
|
-
}
|
|
202
|
-
var candidateAttributes = node.attributes.map(function (attr) {
|
|
203
|
-
// pull out any named attributes we can re-map
|
|
204
|
-
if ((0, _eslintCodemodUtils.isNodeOfType)(attr, 'JSXAttribute')) {
|
|
205
|
-
if (attr.name.name === 'data-testid') {
|
|
206
|
-
return (0, _eslintCodemodUtils.jsxAttribute)(_objectSpread(_objectSpread({}, attr), {}, {
|
|
207
|
-
name: (0, _eslintCodemodUtils.jsxIdentifier)('testId')
|
|
208
|
-
}));
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return attr;
|
|
212
|
-
}).filter(Boolean);
|
|
213
|
-
fixes.push(fixer.replaceText(node, (0, _eslintCodemodUtils.jsxOpeningElement)(_objectSpread(_objectSpread({}, node), {}, {
|
|
214
|
-
name: jsxId,
|
|
215
|
-
attributes: candidateAttributes.concat(attributes)
|
|
216
|
-
})).toString()));
|
|
217
|
-
if (closingElement && closingElement.name) {
|
|
218
|
-
fixes.push(fixer.replaceText(closingElement.name, "".concat(jsxId)));
|
|
219
|
-
}
|
|
220
|
-
return fixes;
|
|
221
|
-
};
|
|
222
|
-
};
|
|
223
|
-
var isVariableAnnotatedWithType = function isVariableAnnotatedWithType(identifier, typeAnnotation) {
|
|
224
|
-
var _identifiers$;
|
|
225
|
-
if (!identifier || identifier.identifiers.length === 0) {
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
var typeAnnotationObject = (_identifiers$ = identifier.identifiers[0]) === null || _identifiers$ === void 0 || (_identifiers$ = _identifiers$.typeAnnotation) === null || _identifiers$ === void 0 ? void 0 : _identifiers$.typeAnnotation;
|
|
229
|
-
|
|
230
|
-
// variable declaration lacks type definitions
|
|
231
|
-
if (!typeAnnotationObject) {
|
|
232
|
-
return false;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// single type annotation
|
|
236
|
-
if (typeAnnotationObject.type === typeAnnotation) {
|
|
237
|
-
return true;
|
|
238
|
-
}
|
|
239
|
-
return false;
|
|
240
|
-
};
|
|
241
|
-
var getJSXAttributeByName = function getJSXAttributeByName(node, attrName) {
|
|
242
|
-
return node.attributes.find(function (attr) {
|
|
243
|
-
// Ignore JSXSpreadAttributes
|
|
244
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(attr, 'JSXAttribute')) {
|
|
245
|
-
return false;
|
|
246
|
-
}
|
|
247
|
-
return attr.name.name === attrName;
|
|
248
|
-
});
|
|
249
|
-
};
|
|
250
|
-
var isWhiteSpace = exports.isWhiteSpace = function isWhiteSpace(value) {
|
|
251
|
-
return value.trim() === '';
|
|
252
|
-
};
|
|
253
|
-
function containsOnlyWhitespace(node) {
|
|
254
|
-
return node.children.every(function (child) {
|
|
255
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(child, 'JSXText')) {
|
|
256
|
-
return false;
|
|
257
|
-
}
|
|
258
|
-
return isWhiteSpace(child.value);
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
var getChildrenByType = exports.getChildrenByType = function getChildrenByType(node, types) {
|
|
262
|
-
return node.children.filter(function (child) {
|
|
263
|
-
return types.find(function (type) {
|
|
264
|
-
return (0, _eslintCodemodUtils.isNodeOfType)(child, type);
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
// FIXME: This not correctly typed
|
|
270
|
-
|
|
271
|
-
var getCSSPropStyleObject = function getCSSPropStyleObject(node, context) {
|
|
272
|
-
var cssAttr = getJSXAttributeByName(node.openingElement, 'css');
|
|
273
|
-
var styleObj = {};
|
|
274
|
-
if (cssAttr && cssAttr.value) {
|
|
275
|
-
var scope = context.getScope();
|
|
276
|
-
var _ref = cssAttr.value,
|
|
277
|
-
expression = _ref.expression;
|
|
278
|
-
var variableDeclarator = (0, _eslintCodemodUtils.getIdentifierInParentScope)(scope, expression.name);
|
|
279
|
-
var defNode = variableDeclarator === null || variableDeclarator === void 0 ? void 0 : variableDeclarator.defs[0];
|
|
280
|
-
if (!defNode) {
|
|
281
|
-
return undefined;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// check if the variable declaration has a call expression init
|
|
285
|
-
// eg we're looking for css() type inits
|
|
286
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(defNode.node, 'VariableDeclarator') && !(0, _eslintCodemodUtils.isNodeOfType)(defNode.node.init, 'CallExpression')) {
|
|
287
|
-
return undefined;
|
|
288
|
-
}
|
|
289
|
-
var init = defNode.node.init;
|
|
290
|
-
|
|
291
|
-
// make the sure the init is called 'css'
|
|
292
|
-
if (!((0, _eslintCodemodUtils.isNodeOfType)(init.callee, 'Identifier') && init.callee.name === 'css')) {
|
|
293
|
-
return undefined;
|
|
294
|
-
}
|
|
295
|
-
var styles = init.arguments[0];
|
|
296
|
-
if (!styles) {
|
|
297
|
-
return undefined;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// make sure the styles are object styles (they should be but let's just double check)
|
|
301
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(styles, 'ObjectExpression')) {
|
|
302
|
-
return undefined;
|
|
303
|
-
}
|
|
304
|
-
styles.properties.forEach(function (prop) {
|
|
305
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(prop, 'Property')) {
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(prop.key, 'Identifier')) {
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
if (!(0, _eslintCodemodUtils.isNodeOfType)(prop.value, 'Literal')) {
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
styleObj[prop.key.name] = prop.value.value;
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
return styleObj;
|
|
318
|
-
};
|
|
319
|
-
var validPrimitiveElements = exports.validPrimitiveElements = new Set(['div', 'span', 'article', 'aside', 'dialog', 'footer', 'header', 'li', 'main', 'nav', 'ol', 'section', 'ul']);
|
|
320
|
-
var isValidPrimitiveElement = exports.isValidPrimitiveElement = function isValidPrimitiveElement(node) {
|
|
321
|
-
return validPrimitiveElements.has(node.openingElement.name.name);
|
|
322
|
-
};
|
|
323
|
-
var hasInlineCompatibleStyles = function hasInlineCompatibleStyles(cssStyleObject) {
|
|
324
|
-
if (!['flex', 'inline-flex'].includes(cssStyleObject['display'])) {
|
|
325
|
-
return false;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Default `flexDirection` is `'row'`, so we can recommend an Inline if
|
|
330
|
-
* it's `undefined`, or if it's explicitly `'row'` or `'row-reverse'`.
|
|
331
|
-
*
|
|
332
|
-
* Note: Currently we are including `'row-reverse'` even though Inline doesn't support it
|
|
333
|
-
* as an attempt to educate makers about accessibility concerns. *
|
|
334
|
-
*/
|
|
335
|
-
var flexDirection = cssStyleObject['flex-direction'] || cssStyleObject['flexDirection'];
|
|
336
|
-
var validFlexDirectionValue = ['row', 'row-reverse', undefined].includes(flexDirection);
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* Note: Currently we are including `'wrap-reverse'` even though Inline doesn't support it
|
|
340
|
-
* as an attempt to educate makers about accessibility concerns.
|
|
341
|
-
*/
|
|
342
|
-
var flexWrap = cssStyleObject['flex-wrap'] || cssStyleObject['flexWrap'];
|
|
343
|
-
var validFlexWrapValue = ['wrap', 'wrap-reverse'].includes(flexWrap);
|
|
344
|
-
if (validFlexDirectionValue || validFlexWrapValue) {
|
|
345
|
-
return true;
|
|
346
|
-
}
|
|
347
|
-
return false;
|
|
348
|
-
};
|
|
349
|
-
var hasStackCompatibleStyles = function hasStackCompatibleStyles(cssStyleObject) {
|
|
350
|
-
if (!['flex', 'inline-flex'].includes(cssStyleObject['display'])) {
|
|
351
|
-
return false;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Note: Currently we are including `'column-reverse'` even though Stack doesn't support it
|
|
356
|
-
* as an attempt to educate makers about accessibility concerns.
|
|
357
|
-
*/
|
|
358
|
-
var flexDirection = cssStyleObject['flex-direction'] || cssStyleObject['flexDirection'];
|
|
359
|
-
var validFlexDirectionValue = ['column', 'column-reverse'].includes(flexDirection);
|
|
360
|
-
if (!validFlexDirectionValue) {
|
|
361
|
-
return false;
|
|
362
|
-
}
|
|
363
|
-
return true;
|
|
364
|
-
};
|