@atlaskit/eslint-plugin-ui-styling-standard 0.16.4 → 0.16.5
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 +9 -0
- package/dist/cjs/rules/no-imported-style-values/index.js +50 -2
- package/dist/es2019/rules/no-imported-style-values/index.js +49 -2
- package/dist/esm/rules/no-imported-style-values/index.js +50 -2
- package/package.json +1 -1
- package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +208 -0
- package/src/rules/no-imported-style-values/index.tsx +51 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @atlaskit/eslint-plugin-ui-styling-standard
|
|
2
2
|
|
|
3
|
+
## 0.16.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#111807](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/111807)
|
|
8
|
+
[`a0bd2ff79dc32`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/a0bd2ff79dc32) -
|
|
9
|
+
Fixes `no-imported-style-values` to allow imported identifiers when used in the LHS of conditional
|
|
10
|
+
expressions in the `css` prop. This enables dynamic application of styles using feature flags.
|
|
11
|
+
|
|
3
12
|
## 0.16.4
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
|
@@ -10,6 +10,7 @@ var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
|
|
|
10
10
|
var _findVariable = require("@atlaskit/eslint-utils/find-variable");
|
|
11
11
|
var _allowedFunctionCalls = require("@atlaskit/eslint-utils/allowed-function-calls");
|
|
12
12
|
var _esquery = _interopRequireDefault(require("esquery"));
|
|
13
|
+
var _estraverse = _interopRequireDefault(require("estraverse"));
|
|
13
14
|
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
14
15
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
15
16
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
@@ -75,6 +76,22 @@ var isIdentifierImported = function isIdentifierImported(identifier, context, fu
|
|
|
75
76
|
});
|
|
76
77
|
};
|
|
77
78
|
var checkIdentifier = function checkIdentifier(identifier, context, functionAllowList, keysAllowList) {
|
|
79
|
+
var _ref = identifier,
|
|
80
|
+
parent = _ref.parent;
|
|
81
|
+
|
|
82
|
+
// Even though `member` in `object.member` is syntactically an identifier,
|
|
83
|
+
// it should be ignored as `.member` is syntax sugar for `['member']` here.
|
|
84
|
+
// We should still lint `object[member]` (when `parent.computed` === true)
|
|
85
|
+
if (parent.type === 'MemberExpression' && identifier === parent.property && !parent.computed) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Even though `key` in `{ key: 'value' }` is syntactically an identifier,
|
|
90
|
+
// it should be ignored as `key` is syntax sugar for `'key'` here.
|
|
91
|
+
// We should still lint `[key]` (when `parent.computed` === true)
|
|
92
|
+
if (parent && parent.type === 'Property' && identifier === parent.key && !parent.computed) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
78
95
|
if (isIdentifierImported(identifier, context, functionAllowList, keysAllowList)) {
|
|
79
96
|
context.report({
|
|
80
97
|
node: identifier,
|
|
@@ -127,8 +144,39 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
|
|
|
127
144
|
}
|
|
128
145
|
node.arguments.forEach(checkForIdentifiers);
|
|
129
146
|
},
|
|
130
|
-
'JSXAttribute[name.name=/^style|css$/]
|
|
131
|
-
|
|
147
|
+
'JSXAttribute[name.name=/^style|css$/]': function JSXAttributeNameNameStyleCss$(jsxAttribute) {
|
|
148
|
+
_estraverse.default.traverse(jsxAttribute, {
|
|
149
|
+
fallback: function fallback(node) {
|
|
150
|
+
// estraverse does not know about nodes not in the ESTree standard.
|
|
151
|
+
// This includes JSX nodes, so we must provide a fallback method to handle them.
|
|
152
|
+
// Using `fallback: 'iteration'` can cause cycles because it will iterate
|
|
153
|
+
// over the `parent` attribute on JSX nodes.
|
|
154
|
+
return Object.keys(node).filter(function (key) {
|
|
155
|
+
return key !== 'parent';
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
enter: function enter(node, parent) {
|
|
159
|
+
// Ignore type contexts
|
|
160
|
+
// Cast required because ESTree is not aware of TypeScript nodes
|
|
161
|
+
if (node.type === 'TSTypeReference') {
|
|
162
|
+
return _estraverse.default.VisitorOption.Skip;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Ignore conditions of ternary expressions
|
|
166
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'ConditionalExpression' && node === parent.test) {
|
|
167
|
+
return _estraverse.default.VisitorOption.Skip;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Ignore LHS of logical expressions (&&, ||)
|
|
171
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'LogicalExpression' && node === parent.left) {
|
|
172
|
+
return _estraverse.default.VisitorOption.Skip;
|
|
173
|
+
}
|
|
174
|
+
if (node.type !== 'Identifier') {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
checkIdentifier(node, context, functionAllowList, keysAllowList);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
132
180
|
}
|
|
133
181
|
};
|
|
134
182
|
}
|
|
@@ -3,6 +3,7 @@ import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-util
|
|
|
3
3
|
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
4
4
|
import { getAllowedDynamicKeys, getAllowedFunctionCalls, isAllowListedVariable } from '@atlaskit/eslint-utils/allowed-function-calls';
|
|
5
5
|
import esquery from 'esquery';
|
|
6
|
+
import estraverse from 'estraverse';
|
|
6
7
|
const schema = [{
|
|
7
8
|
type: 'object',
|
|
8
9
|
properties: {
|
|
@@ -63,6 +64,23 @@ const isIdentifierImported = (identifier, context, functionAllowList, keysAllowL
|
|
|
63
64
|
});
|
|
64
65
|
};
|
|
65
66
|
const checkIdentifier = (identifier, context, functionAllowList, keysAllowList) => {
|
|
67
|
+
const {
|
|
68
|
+
parent
|
|
69
|
+
} = identifier;
|
|
70
|
+
|
|
71
|
+
// Even though `member` in `object.member` is syntactically an identifier,
|
|
72
|
+
// it should be ignored as `.member` is syntax sugar for `['member']` here.
|
|
73
|
+
// We should still lint `object[member]` (when `parent.computed` === true)
|
|
74
|
+
if (parent.type === 'MemberExpression' && identifier === parent.property && !parent.computed) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Even though `key` in `{ key: 'value' }` is syntactically an identifier,
|
|
79
|
+
// it should be ignored as `key` is syntax sugar for `'key'` here.
|
|
80
|
+
// We should still lint `[key]` (when `parent.computed` === true)
|
|
81
|
+
if (parent && parent.type === 'Property' && identifier === parent.key && !parent.computed) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
66
84
|
if (isIdentifierImported(identifier, context, functionAllowList, keysAllowList)) {
|
|
67
85
|
context.report({
|
|
68
86
|
node: identifier,
|
|
@@ -107,8 +125,37 @@ export const rule = createLintRule({
|
|
|
107
125
|
}
|
|
108
126
|
node.arguments.forEach(checkForIdentifiers);
|
|
109
127
|
},
|
|
110
|
-
'JSXAttribute[name.name=/^style|css$/]
|
|
111
|
-
|
|
128
|
+
'JSXAttribute[name.name=/^style|css$/]'(jsxAttribute) {
|
|
129
|
+
estraverse.traverse(jsxAttribute, {
|
|
130
|
+
fallback(node) {
|
|
131
|
+
// estraverse does not know about nodes not in the ESTree standard.
|
|
132
|
+
// This includes JSX nodes, so we must provide a fallback method to handle them.
|
|
133
|
+
// Using `fallback: 'iteration'` can cause cycles because it will iterate
|
|
134
|
+
// over the `parent` attribute on JSX nodes.
|
|
135
|
+
return Object.keys(node).filter(key => key !== 'parent');
|
|
136
|
+
},
|
|
137
|
+
enter(node, parent) {
|
|
138
|
+
// Ignore type contexts
|
|
139
|
+
// Cast required because ESTree is not aware of TypeScript nodes
|
|
140
|
+
if (node.type === 'TSTypeReference') {
|
|
141
|
+
return estraverse.VisitorOption.Skip;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Ignore conditions of ternary expressions
|
|
145
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'ConditionalExpression' && node === parent.test) {
|
|
146
|
+
return estraverse.VisitorOption.Skip;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Ignore LHS of logical expressions (&&, ||)
|
|
150
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'LogicalExpression' && node === parent.left) {
|
|
151
|
+
return estraverse.VisitorOption.Skip;
|
|
152
|
+
}
|
|
153
|
+
if (node.type !== 'Identifier') {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
checkIdentifier(node, context, functionAllowList, keysAllowList);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
112
159
|
}
|
|
113
160
|
};
|
|
114
161
|
}
|
|
@@ -6,6 +6,7 @@ import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-util
|
|
|
6
6
|
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
7
7
|
import { getAllowedDynamicKeys, getAllowedFunctionCalls, isAllowListedVariable } from '@atlaskit/eslint-utils/allowed-function-calls';
|
|
8
8
|
import esquery from 'esquery';
|
|
9
|
+
import estraverse from 'estraverse';
|
|
9
10
|
var schema = [{
|
|
10
11
|
type: 'object',
|
|
11
12
|
properties: {
|
|
@@ -68,6 +69,22 @@ var isIdentifierImported = function isIdentifierImported(identifier, context, fu
|
|
|
68
69
|
});
|
|
69
70
|
};
|
|
70
71
|
var checkIdentifier = function checkIdentifier(identifier, context, functionAllowList, keysAllowList) {
|
|
72
|
+
var _ref = identifier,
|
|
73
|
+
parent = _ref.parent;
|
|
74
|
+
|
|
75
|
+
// Even though `member` in `object.member` is syntactically an identifier,
|
|
76
|
+
// it should be ignored as `.member` is syntax sugar for `['member']` here.
|
|
77
|
+
// We should still lint `object[member]` (when `parent.computed` === true)
|
|
78
|
+
if (parent.type === 'MemberExpression' && identifier === parent.property && !parent.computed) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Even though `key` in `{ key: 'value' }` is syntactically an identifier,
|
|
83
|
+
// it should be ignored as `key` is syntax sugar for `'key'` here.
|
|
84
|
+
// We should still lint `[key]` (when `parent.computed` === true)
|
|
85
|
+
if (parent && parent.type === 'Property' && identifier === parent.key && !parent.computed) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
71
88
|
if (isIdentifierImported(identifier, context, functionAllowList, keysAllowList)) {
|
|
72
89
|
context.report({
|
|
73
90
|
node: identifier,
|
|
@@ -120,8 +137,39 @@ export var rule = createLintRule({
|
|
|
120
137
|
}
|
|
121
138
|
node.arguments.forEach(checkForIdentifiers);
|
|
122
139
|
},
|
|
123
|
-
'JSXAttribute[name.name=/^style|css$/]
|
|
124
|
-
|
|
140
|
+
'JSXAttribute[name.name=/^style|css$/]': function JSXAttributeNameNameStyleCss$(jsxAttribute) {
|
|
141
|
+
estraverse.traverse(jsxAttribute, {
|
|
142
|
+
fallback: function fallback(node) {
|
|
143
|
+
// estraverse does not know about nodes not in the ESTree standard.
|
|
144
|
+
// This includes JSX nodes, so we must provide a fallback method to handle them.
|
|
145
|
+
// Using `fallback: 'iteration'` can cause cycles because it will iterate
|
|
146
|
+
// over the `parent` attribute on JSX nodes.
|
|
147
|
+
return Object.keys(node).filter(function (key) {
|
|
148
|
+
return key !== 'parent';
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
enter: function enter(node, parent) {
|
|
152
|
+
// Ignore type contexts
|
|
153
|
+
// Cast required because ESTree is not aware of TypeScript nodes
|
|
154
|
+
if (node.type === 'TSTypeReference') {
|
|
155
|
+
return estraverse.VisitorOption.Skip;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Ignore conditions of ternary expressions
|
|
159
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'ConditionalExpression' && node === parent.test) {
|
|
160
|
+
return estraverse.VisitorOption.Skip;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Ignore LHS of logical expressions (&&, ||)
|
|
164
|
+
if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'LogicalExpression' && node === parent.left) {
|
|
165
|
+
return estraverse.VisitorOption.Skip;
|
|
166
|
+
}
|
|
167
|
+
if (node.type !== 'Identifier') {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
checkIdentifier(node, context, functionAllowList, keysAllowList);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
125
173
|
}
|
|
126
174
|
};
|
|
127
175
|
}
|
package/package.json
CHANGED
|
@@ -141,6 +141,90 @@ typescriptEslintTester.run(
|
|
|
141
141
|
`,
|
|
142
142
|
options: [],
|
|
143
143
|
},
|
|
144
|
+
{
|
|
145
|
+
name: 'Imported value in LHS of conditional expression in css prop',
|
|
146
|
+
code: `
|
|
147
|
+
import { css } from '@compiled/react';
|
|
148
|
+
import { ff } from '@atlassian/jira-feature-flagging';
|
|
149
|
+
import { IS_FF_ENABLED } from '../shared';
|
|
150
|
+
|
|
151
|
+
const baseStyles = css({ margin: 0 });
|
|
152
|
+
const styles = css({ color: token('color.text') });
|
|
153
|
+
const stylesFF = css({ color: token('color.text.danger') });
|
|
154
|
+
|
|
155
|
+
const Component = () => (
|
|
156
|
+
<>
|
|
157
|
+
<div css={IS_FF_ENABLED ? stylesFF : styles} />
|
|
158
|
+
<div css={[baseStyles, IS_FF_ENABLED ? stylesFF : styles]} />
|
|
159
|
+
<div css={ff('my-feature-flag') ? stylesFF : styles} />
|
|
160
|
+
<div css={[baseStyles, ff('my-feature-flag') ? stylesFF : styles]} />
|
|
161
|
+
</>
|
|
162
|
+
);
|
|
163
|
+
`,
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
name: 'Imported value as entire LHS of logical expression in css prop',
|
|
167
|
+
code: `
|
|
168
|
+
import { css } from '@compiled/react';
|
|
169
|
+
import { ff } from '@atlassian/jira-feature-flagging';
|
|
170
|
+
import { IS_FF_ENABLED } from '../shared';
|
|
171
|
+
|
|
172
|
+
const baseStyles = css({ margin: 0 });
|
|
173
|
+
const stylesFF = css({ color: token('color.text.danger') });
|
|
174
|
+
|
|
175
|
+
const Component = () => (
|
|
176
|
+
<>
|
|
177
|
+
<div css={IS_FF_ENABLED && stylesFF} />
|
|
178
|
+
<div css={[baseStyles, IS_FF_ENABLED && stylesFF]} />
|
|
179
|
+
<div css={ff('my-feature-flag') && stylesFF} />
|
|
180
|
+
<div css={[baseStyles, ff('my-feature-flag') && stylesFF]} />
|
|
181
|
+
</>
|
|
182
|
+
);
|
|
183
|
+
`,
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: 'Imported value in LHS of conditional expression in style prop',
|
|
187
|
+
code: `
|
|
188
|
+
import { css } from '@compiled/react';
|
|
189
|
+
import { ff } from '@atlassian/jira-feature-flagging';
|
|
190
|
+
import { IS_FF_ENABLED } from '../shared';
|
|
191
|
+
|
|
192
|
+
const Component = (props: { width: number }) => (
|
|
193
|
+
<>
|
|
194
|
+
<div style={{ width: IS_FF_ENABLED ? Math.abs(width) : width }} />
|
|
195
|
+
<div style={{ width: ff('my-feature-flag') ? Math.abs(width) : width }} />
|
|
196
|
+
</>
|
|
197
|
+
);
|
|
198
|
+
`,
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
name: 'Non-computed member access that collides with imported name',
|
|
202
|
+
code: `
|
|
203
|
+
import { abs } from '../shared';
|
|
204
|
+
|
|
205
|
+
const Component = (props: { width: number }) => (
|
|
206
|
+
<div style={{ width: Math.abs(width) }} />
|
|
207
|
+
);
|
|
208
|
+
`,
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
name: 'Non-computed property key that collides with imported name',
|
|
212
|
+
code: `
|
|
213
|
+
import { css } from '@compiled/react';
|
|
214
|
+
import { color } from '../shared';
|
|
215
|
+
|
|
216
|
+
const styles = css({
|
|
217
|
+
color: token('color.text'),
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const Component = (props: { color: string }) => (
|
|
221
|
+
<div
|
|
222
|
+
css={styles}
|
|
223
|
+
style={{ color: props.color }}
|
|
224
|
+
/>
|
|
225
|
+
);
|
|
226
|
+
`,
|
|
227
|
+
},
|
|
144
228
|
],
|
|
145
229
|
invalid: [
|
|
146
230
|
{
|
|
@@ -278,6 +362,130 @@ typescriptEslintTester.run(
|
|
|
278
362
|
`,
|
|
279
363
|
errors: [{ messageId: 'no-imported-style-values' }],
|
|
280
364
|
},
|
|
365
|
+
{
|
|
366
|
+
name: 'Imported value in LHS of conditional expression in style declaration',
|
|
367
|
+
code: `
|
|
368
|
+
import { css } from '@compiled/react';
|
|
369
|
+
import { ff } from '@atlassian/jira-feature-flagging';
|
|
370
|
+
import { IS_FF_ENABLED } from '../shared';
|
|
371
|
+
|
|
372
|
+
const stylesA = css({
|
|
373
|
+
color: IS_FF_ENABLED ? token('color.text.danger'): token('color.text'),
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
const stylesB = css({
|
|
377
|
+
color: ff('my-feature-flag') ? token('color.text.danger') : token('color.text'),
|
|
378
|
+
});
|
|
379
|
+
`,
|
|
380
|
+
errors: [
|
|
381
|
+
{ messageId: 'no-imported-style-values', line: 7 },
|
|
382
|
+
{ messageId: 'no-imported-style-values', line: 11 },
|
|
383
|
+
],
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
name: 'Imported value in RHS of conditional expression in css prop',
|
|
387
|
+
code: `
|
|
388
|
+
import { css } from '@compiled/react';
|
|
389
|
+
import { ff } from '@atlassian/jira-feature-flagging';
|
|
390
|
+
import { IS_FF_ENABLED, stylesFF } from '../shared';
|
|
391
|
+
|
|
392
|
+
const baseStyles = css({ margin: 0 });
|
|
393
|
+
const styles = css({ color: token('color.text' )});
|
|
394
|
+
|
|
395
|
+
const Component = () => (
|
|
396
|
+
<>
|
|
397
|
+
<div css={IS_FF_ENABLED ? stylesFF : styles} />
|
|
398
|
+
<div css={[baseStyles, IS_FF_ENABLED ? stylesFF : styles]} />
|
|
399
|
+
<div css={ff('my-feature-flag') ? stylesFF : styles} />
|
|
400
|
+
<div css={[baseStyles, ff('my-feature-flag') ? stylesFF : styles]} />
|
|
401
|
+
</>
|
|
402
|
+
);
|
|
403
|
+
`,
|
|
404
|
+
errors: [
|
|
405
|
+
{ messageId: 'no-imported-style-values', line: 11 },
|
|
406
|
+
{ messageId: 'no-imported-style-values', line: 12 },
|
|
407
|
+
{ messageId: 'no-imported-style-values', line: 13 },
|
|
408
|
+
{ messageId: 'no-imported-style-values', line: 14 },
|
|
409
|
+
],
|
|
410
|
+
},
|
|
411
|
+
{
|
|
412
|
+
name: 'Imported value in RHS of logical expression in css prop',
|
|
413
|
+
code: `
|
|
414
|
+
import { css } from '@compiled/react';
|
|
415
|
+
import { ff } from '@atlassian/jira-feature-flagging';
|
|
416
|
+
import { IS_FF_ENABLED, stylesFF } from '../shared';
|
|
417
|
+
|
|
418
|
+
const baseStyles = css({ margin: 0 });
|
|
419
|
+
const styles = css({ color: token('color.text' )});
|
|
420
|
+
|
|
421
|
+
const Component = () => (
|
|
422
|
+
<>
|
|
423
|
+
<div css={IS_FF_ENABLED && stylesFF} />
|
|
424
|
+
<div css={[baseStyles, IS_FF_ENABLED && stylesFF]} />
|
|
425
|
+
<div css={ff('my-feature-flag') && stylesFF} />
|
|
426
|
+
<div css={[baseStyles, ff('my-feature-flag') && stylesFF]} />
|
|
427
|
+
</>
|
|
428
|
+
);
|
|
429
|
+
`,
|
|
430
|
+
errors: [
|
|
431
|
+
{ messageId: 'no-imported-style-values', line: 11 },
|
|
432
|
+
{ messageId: 'no-imported-style-values', line: 12 },
|
|
433
|
+
{ messageId: 'no-imported-style-values', line: 13 },
|
|
434
|
+
{ messageId: 'no-imported-style-values', line: 14 },
|
|
435
|
+
],
|
|
436
|
+
},
|
|
437
|
+
{
|
|
438
|
+
name: 'Imported value in RHS of conditional expression in style prop',
|
|
439
|
+
code: `
|
|
440
|
+
import { css } from '@compiled/react';
|
|
441
|
+
import { ff } from '@atlassian/jira-feature-flagging';
|
|
442
|
+
import { IS_FF_ENABLED, abs } from '../shared';
|
|
443
|
+
|
|
444
|
+
const Component = (props: { width: number }) => (
|
|
445
|
+
<>
|
|
446
|
+
<div style={{ width: IS_FF_ENABLED ? abs(width) : width }} />
|
|
447
|
+
<div style={{ width: ff('my-feature-flag') ? abs(width) : width }} />
|
|
448
|
+
</>
|
|
449
|
+
);
|
|
450
|
+
`,
|
|
451
|
+
errors: [
|
|
452
|
+
{ messageId: 'no-imported-style-values', line: 8 },
|
|
453
|
+
{ messageId: 'no-imported-style-values', line: 9 },
|
|
454
|
+
],
|
|
455
|
+
},
|
|
456
|
+
{
|
|
457
|
+
name: 'Computed member access with imported name',
|
|
458
|
+
code: `
|
|
459
|
+
import { abs } from '../shared';
|
|
460
|
+
|
|
461
|
+
const Component = (props: { width: number }) => (
|
|
462
|
+
<div style={{ width: Math[abs](width) }} />
|
|
463
|
+
);
|
|
464
|
+
`,
|
|
465
|
+
errors: [{ messageId: 'no-imported-style-values' }],
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
name: 'Computed property key with imported name',
|
|
469
|
+
code: `
|
|
470
|
+
import { css } from '@compiled/react';
|
|
471
|
+
import { color } from '../shared';
|
|
472
|
+
|
|
473
|
+
const styles = css({
|
|
474
|
+
[color]: token('color.text'),
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
const Component = (props: { color: string }) => (
|
|
478
|
+
<div
|
|
479
|
+
css={styles}
|
|
480
|
+
style={{ [color]: props.color }}
|
|
481
|
+
/>
|
|
482
|
+
);
|
|
483
|
+
`,
|
|
484
|
+
errors: [
|
|
485
|
+
{ messageId: 'no-imported-style-values' },
|
|
486
|
+
{ messageId: 'no-imported-style-values' },
|
|
487
|
+
],
|
|
488
|
+
},
|
|
281
489
|
],
|
|
282
490
|
},
|
|
283
491
|
);
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
hasStyleObjectArguments,
|
|
5
5
|
} from '@atlaskit/eslint-utils/is-supported-import';
|
|
6
6
|
import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
|
|
7
|
+
import type { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
|
|
7
8
|
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
8
9
|
import type { Rule } from 'eslint';
|
|
9
10
|
import {
|
|
@@ -14,6 +15,7 @@ import {
|
|
|
14
15
|
} from '@atlaskit/eslint-utils/allowed-function-calls';
|
|
15
16
|
import type * as ESTree from 'eslint-codemod-utils';
|
|
16
17
|
import esquery from 'esquery';
|
|
18
|
+
import estraverse from 'estraverse';
|
|
17
19
|
|
|
18
20
|
const schema: JSONSchema4 = [
|
|
19
21
|
{
|
|
@@ -81,6 +83,22 @@ const checkIdentifier = (
|
|
|
81
83
|
functionAllowList: AllowList,
|
|
82
84
|
keysAllowList: AllowList,
|
|
83
85
|
) => {
|
|
86
|
+
const { parent } = identifier as Rule.Node;
|
|
87
|
+
|
|
88
|
+
// Even though `member` in `object.member` is syntactically an identifier,
|
|
89
|
+
// it should be ignored as `.member` is syntax sugar for `['member']` here.
|
|
90
|
+
// We should still lint `object[member]` (when `parent.computed` === true)
|
|
91
|
+
if (parent.type === 'MemberExpression' && identifier === parent.property && !parent.computed) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Even though `key` in `{ key: 'value' }` is syntactically an identifier,
|
|
96
|
+
// it should be ignored as `key` is syntax sugar for `'key'` here.
|
|
97
|
+
// We should still lint `[key]` (when `parent.computed` === true)
|
|
98
|
+
if (parent && parent.type === 'Property' && identifier === parent.key && !parent.computed) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
84
102
|
if (isIdentifierImported(identifier, context, functionAllowList, keysAllowList)) {
|
|
85
103
|
context.report({
|
|
86
104
|
node: identifier,
|
|
@@ -129,10 +147,39 @@ export const rule = createLintRule({
|
|
|
129
147
|
}
|
|
130
148
|
node.arguments.forEach(checkForIdentifiers);
|
|
131
149
|
},
|
|
132
|
-
'JSXAttribute[name.name=/^style|css$/]
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
150
|
+
'JSXAttribute[name.name=/^style|css$/]'(jsxAttribute: ESTree.Node) {
|
|
151
|
+
estraverse.traverse(jsxAttribute, {
|
|
152
|
+
fallback(node) {
|
|
153
|
+
// estraverse does not know about nodes not in the ESTree standard.
|
|
154
|
+
// This includes JSX nodes, so we must provide a fallback method to handle them.
|
|
155
|
+
// Using `fallback: 'iteration'` can cause cycles because it will iterate
|
|
156
|
+
// over the `parent` attribute on JSX nodes.
|
|
157
|
+
return Object.keys(node).filter((key) => key !== 'parent');
|
|
158
|
+
},
|
|
159
|
+
enter(node, parent) {
|
|
160
|
+
// Ignore type contexts
|
|
161
|
+
// Cast required because ESTree is not aware of TypeScript nodes
|
|
162
|
+
if ((node.type as AST_NODE_TYPES) === 'TSTypeReference') {
|
|
163
|
+
return estraverse.VisitorOption.Skip;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Ignore conditions of ternary expressions
|
|
167
|
+
if (parent?.type === 'ConditionalExpression' && node === parent.test) {
|
|
168
|
+
return estraverse.VisitorOption.Skip;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Ignore LHS of logical expressions (&&, ||)
|
|
172
|
+
if (parent?.type === 'LogicalExpression' && node === parent.left) {
|
|
173
|
+
return estraverse.VisitorOption.Skip;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (node.type !== 'Identifier') {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
checkIdentifier(node, context, functionAllowList, keysAllowList);
|
|
181
|
+
},
|
|
182
|
+
});
|
|
136
183
|
},
|
|
137
184
|
};
|
|
138
185
|
},
|