@atlaskit/eslint-plugin-ui-styling-standard 1.4.1 → 1.4.2

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 CHANGED
@@ -1,5 +1,14 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 1.4.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#120084](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/120084)
8
+ [`ee978d7b2ba83`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/ee978d7b2ba83) -
9
+ Support BinaryExpression and ConditionalExpression logic when inspecting if an Identifier is
10
+ static.
11
+
3
12
  ## 1.4.1
4
13
 
5
14
  ### Patch Changes
@@ -7,70 +7,86 @@ exports.rule = exports.default = void 0;
7
7
  var _eslintCodemodUtils = require("eslint-codemod-utils");
8
8
  var _allowedFunctionCalls = require("@atlaskit/eslint-utils/allowed-function-calls");
9
9
  var _contextCompat = require("@atlaskit/eslint-utils/context-compat");
10
- var _findVariable3 = require("@atlaskit/eslint-utils/find-variable");
10
+ var _findVariable2 = require("@atlaskit/eslint-utils/find-variable");
11
11
  var _createRule = require("../utils/create-rule");
12
- var isParameter = function isParameter(context, identifier) {
13
- var _findVariable;
14
- var definitions = (_findVariable = (0, _findVariable3.findVariable)({
15
- sourceCode: (0, _contextCompat.getSourceCode)(context),
16
- identifier: identifier
17
- })) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
18
- if (!definitions || !definitions.length) {
19
- return false;
20
- }
21
- var result = definitions[0];
22
- return result.type === 'Parameter';
23
- };
24
- var isIdentifierAllowed = function isIdentifierAllowed(context, identifier) {
12
+ var _isIdentifierAllowed = function isIdentifierAllowed(context, identifier) {
25
13
  var _variable$identifiers;
26
- var variable = (0, _findVariable3.findVariable)({
14
+ var variable = (0, _findVariable2.findVariable)({
27
15
  sourceCode: (0, _contextCompat.getSourceCode)(context),
28
16
  identifier: identifier
29
17
  });
30
18
  if (!variable) {
31
19
  return false;
32
20
  }
33
- var variableDefinition = variable.defs[0].node;
34
21
 
35
22
  // If identifier is a RestElement, report warning
36
23
  if (((_variable$identifiers = variable.identifiers[0]) === null || _variable$identifiers === void 0 || (_variable$identifiers = _variable$identifiers.parent) === null || _variable$identifiers === void 0 ? void 0 : _variable$identifiers.type) === 'RestElement') {
37
24
  return false;
38
25
  }
26
+ var firstDef = variable.defs[0];
27
+ if (firstDef.type === 'Parameter') {
28
+ // This ultimately comes from a function parameter, eg. `const value = props.value`
29
+ // ultimately finds this `function (props)` or `function({ width })` and those are parameters
30
+ return true;
31
+ }
32
+ if (firstDef.type !== 'Variable') {
33
+ // If this isn't a variable, eg. not `const value = props.value`, it's not an identifier we can track
34
+ return false;
35
+ }
36
+ var variableDefinition = firstDef.node;
37
+ if (variableDefinition.type !== 'VariableDeclarator') {
38
+ return false;
39
+ }
39
40
 
40
41
  // Checking if identifier has been destructured from props within function
41
- if (variableDefinition.type === 'VariableDeclarator') {
42
- var _init, _init2, _init3, _init4;
43
- // If it's a let variable, ignore
44
- if (variableDefinition.parent.kind === 'let') {
45
- return true;
46
- }
42
+ // If it's a `let var = …`, ignore, we assume it's dynamically set (but it could be static)
43
+ if ('kind' in variableDefinition.parent && variableDefinition.parent.kind === 'let') {
44
+ return true;
45
+ }
47
46
 
48
- // Allow for `const { width } = props as Props` (etc)
49
- var init = variableDefinition.init;
50
- if (((_init = init) === null || _init === void 0 ? void 0 : _init.type) === 'TSAsExpression') {
51
- init = init.expression;
52
- }
47
+ // Allow for `const { width } = props as Props` (etc)
48
+ var init = variableDefinition.init;
49
+
50
+ // This is not typed for TS types
51
+ if (init.type === 'TSAsExpression' && 'expression' in init && typeof init.expression !== 'boolean') {
52
+ init = init.expression;
53
+ }
53
54
 
54
- // If it's initialised by a call expression, ignore
55
- if (((_init2 = init) === null || _init2 === void 0 ? void 0 : _init2.type) === 'CallExpression') {
56
- return true;
55
+ // If it's initialised by a call expression, ignore
56
+ if (init.type === 'CallExpression') {
57
+ return true;
58
+ }
59
+ if (init.type === 'BinaryExpression') {
60
+ // If it's initialized like `const var = 1 * props.value`, validate either side
61
+ if (init.left.type === 'MemberExpression') {
62
+ return _isIdentifierAllowed(context, init.left.object);
57
63
  }
58
- // Destructured from member expression
59
- // e.g. const width = props.width
60
- if (((_init3 = init) === null || _init3 === void 0 ? void 0 : _init3.type) === 'MemberExpression') {
61
- return isParameter(context, init.object);
64
+ if (init.right.type === 'MemberExpression') {
65
+ return _isIdentifierAllowed(context, init.right.object);
62
66
  }
63
- // Destructured from an asserted
64
- // e.g. const width = props.width
65
- if (((_init4 = init) === null || _init4 === void 0 ? void 0 : _init4.type) === 'MemberExpression') {
66
- return isParameter(context, init.object);
67
+ }
68
+ if (init.type === 'ConditionalExpression') {
69
+ // If it's initialized like `const var = props.value ? 42 : 1`, validate either side
70
+ if (init.test.type === 'MemberExpression') {
71
+ return _isIdentifierAllowed(context, init.test.object);
72
+ }
73
+ if (init.consequent.type === 'MemberExpression') {
74
+ return _isIdentifierAllowed(context, init.consequent.object);
75
+ }
76
+ if (init.alternate.type === 'MemberExpression') {
77
+ return _isIdentifierAllowed(context, init.alternate.object);
67
78
  }
68
- // Destructured from object pattern
69
- // e.g. const { width } = props
70
- return isParameter(context, init);
71
79
  }
72
- // If identifier has not been deconstructed from parameter or is a RestElement, report warning
73
- return isParameter(context, identifier);
80
+
81
+ // Destructured from member expression
82
+ // e.g. const width = props.width
83
+ if (init.type === 'MemberExpression') {
84
+ return _isIdentifierAllowed(context, init.object);
85
+ }
86
+
87
+ // Destructured from object pattern
88
+ // e.g. const { width } = props
89
+ return _isIdentifierAllowed(context, init);
74
90
  };
75
91
  function isPropertyValueAllowed(property, context) {
76
92
  if (property.type === 'SpreadElement') {
@@ -81,9 +97,9 @@ function isPropertyValueAllowed(property, context) {
81
97
  if (property.value.type === 'Literal') {
82
98
  return false;
83
99
  } else if (property.value.type === 'Identifier') {
84
- return isIdentifierAllowed(context, property.value);
100
+ return _isIdentifierAllowed(context, property.value);
85
101
  } else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier') {
86
- var variable = (0, _findVariable3.findVariable)({
102
+ var variable = (0, _findVariable2.findVariable)({
87
103
  sourceCode: (0, _contextCompat.getSourceCode)(context),
88
104
  identifier: property.value.callee
89
105
  });
@@ -96,11 +112,11 @@ function isPropertyValueAllowed(property, context) {
96
112
  return false;
97
113
  }
98
114
  } else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
99
- var _findVariable2;
100
- var definitions = (_findVariable2 = (0, _findVariable3.findVariable)({
115
+ var _findVariable;
116
+ var definitions = (_findVariable = (0, _findVariable2.findVariable)({
101
117
  sourceCode: (0, _contextCompat.getSourceCode)(context),
102
118
  identifier: property.value.object
103
- })) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
119
+ })) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
104
120
  if (!definitions || !definitions.length) {
105
121
  return false;
106
122
  }
@@ -170,7 +186,7 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
170
186
  * <div style={style} />
171
187
  * ```
172
188
  */
173
- var variable = (0, _findVariable3.findVariable)({
189
+ var variable = (0, _findVariable2.findVariable)({
174
190
  sourceCode: (0, _contextCompat.getSourceCode)(context),
175
191
  identifier: node.expression
176
192
  });
@@ -3,18 +3,6 @@ import { getAllowedFunctionCalls, isAllowListedVariable } from '@atlaskit/eslint
3
3
  import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
4
4
  import { findVariable } from '@atlaskit/eslint-utils/find-variable';
5
5
  import { createLintRule } from '../utils/create-rule';
6
- const isParameter = (context, identifier) => {
7
- var _findVariable;
8
- const definitions = (_findVariable = findVariable({
9
- sourceCode: getSourceCode(context),
10
- identifier: identifier
11
- })) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
12
- if (!definitions || !definitions.length) {
13
- return false;
14
- }
15
- const result = definitions[0];
16
- return result.type === 'Parameter';
17
- };
18
6
  const isIdentifierAllowed = (context, identifier) => {
19
7
  var _variable$identifiers, _variable$identifiers2;
20
8
  const variable = findVariable({
@@ -24,47 +12,75 @@ const isIdentifierAllowed = (context, identifier) => {
24
12
  if (!variable) {
25
13
  return false;
26
14
  }
27
- const variableDefinition = variable.defs[0].node;
28
15
 
29
16
  // If identifier is a RestElement, report warning
30
17
  if (((_variable$identifiers = variable.identifiers[0]) === null || _variable$identifiers === void 0 ? void 0 : (_variable$identifiers2 = _variable$identifiers.parent) === null || _variable$identifiers2 === void 0 ? void 0 : _variable$identifiers2.type) === 'RestElement') {
31
18
  return false;
32
19
  }
20
+ const firstDef = variable.defs[0];
21
+ if (firstDef.type === 'Parameter') {
22
+ // This ultimately comes from a function parameter, eg. `const value = props.value`
23
+ // ultimately finds this `function (props)` or `function({ width })` and those are parameters
24
+ return true;
25
+ }
26
+ if (firstDef.type !== 'Variable') {
27
+ // If this isn't a variable, eg. not `const value = props.value`, it's not an identifier we can track
28
+ return false;
29
+ }
30
+ const variableDefinition = firstDef.node;
31
+ if (variableDefinition.type !== 'VariableDeclarator') {
32
+ return false;
33
+ }
33
34
 
34
35
  // Checking if identifier has been destructured from props within function
35
- if (variableDefinition.type === 'VariableDeclarator') {
36
- var _init, _init2, _init3, _init4;
37
- // If it's a let variable, ignore
38
- if (variableDefinition.parent.kind === 'let') {
39
- return true;
40
- }
36
+ // If it's a `let var = …`, ignore, we assume it's dynamically set (but it could be static)
37
+ if ('kind' in variableDefinition.parent && variableDefinition.parent.kind === 'let') {
38
+ return true;
39
+ }
41
40
 
42
- // Allow for `const { width } = props as Props` (etc)
43
- let init = variableDefinition.init;
44
- if (((_init = init) === null || _init === void 0 ? void 0 : _init.type) === 'TSAsExpression') {
45
- init = init.expression;
46
- }
41
+ // Allow for `const { width } = props as Props` (etc)
42
+ let init = variableDefinition.init;
43
+
44
+ // This is not typed for TS types
45
+ if (init.type === 'TSAsExpression' && 'expression' in init && typeof init.expression !== 'boolean') {
46
+ init = init.expression;
47
+ }
47
48
 
48
- // If it's initialised by a call expression, ignore
49
- if (((_init2 = init) === null || _init2 === void 0 ? void 0 : _init2.type) === 'CallExpression') {
50
- return true;
49
+ // If it's initialised by a call expression, ignore
50
+ if (init.type === 'CallExpression') {
51
+ return true;
52
+ }
53
+ if (init.type === 'BinaryExpression') {
54
+ // If it's initialized like `const var = 1 * props.value`, validate either side
55
+ if (init.left.type === 'MemberExpression') {
56
+ return isIdentifierAllowed(context, init.left.object);
51
57
  }
52
- // Destructured from member expression
53
- // e.g. const width = props.width
54
- if (((_init3 = init) === null || _init3 === void 0 ? void 0 : _init3.type) === 'MemberExpression') {
55
- return isParameter(context, init.object);
58
+ if (init.right.type === 'MemberExpression') {
59
+ return isIdentifierAllowed(context, init.right.object);
56
60
  }
57
- // Destructured from an asserted
58
- // e.g. const width = props.width
59
- if (((_init4 = init) === null || _init4 === void 0 ? void 0 : _init4.type) === 'MemberExpression') {
60
- return isParameter(context, init.object);
61
+ }
62
+ if (init.type === 'ConditionalExpression') {
63
+ // If it's initialized like `const var = props.value ? 42 : 1`, validate either side
64
+ if (init.test.type === 'MemberExpression') {
65
+ return isIdentifierAllowed(context, init.test.object);
66
+ }
67
+ if (init.consequent.type === 'MemberExpression') {
68
+ return isIdentifierAllowed(context, init.consequent.object);
69
+ }
70
+ if (init.alternate.type === 'MemberExpression') {
71
+ return isIdentifierAllowed(context, init.alternate.object);
61
72
  }
62
- // Destructured from object pattern
63
- // e.g. const { width } = props
64
- return isParameter(context, init);
65
73
  }
66
- // If identifier has not been deconstructed from parameter or is a RestElement, report warning
67
- return isParameter(context, identifier);
74
+
75
+ // Destructured from member expression
76
+ // e.g. const width = props.width
77
+ if (init.type === 'MemberExpression') {
78
+ return isIdentifierAllowed(context, init.object);
79
+ }
80
+
81
+ // Destructured from object pattern
82
+ // e.g. const { width } = props
83
+ return isIdentifierAllowed(context, init);
68
84
  };
69
85
  function isPropertyValueAllowed(property, context) {
70
86
  if (property.type === 'SpreadElement') {
@@ -90,11 +106,11 @@ function isPropertyValueAllowed(property, context) {
90
106
  return false;
91
107
  }
92
108
  } else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
93
- var _findVariable2;
94
- const definitions = (_findVariable2 = findVariable({
109
+ var _findVariable;
110
+ const definitions = (_findVariable = findVariable({
95
111
  sourceCode: getSourceCode(context),
96
112
  identifier: property.value.object
97
- })) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
113
+ })) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
98
114
  if (!definitions || !definitions.length) {
99
115
  return false;
100
116
  }
@@ -3,19 +3,7 @@ import { getAllowedFunctionCalls, isAllowListedVariable } from '@atlaskit/eslint
3
3
  import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
4
4
  import { findVariable } from '@atlaskit/eslint-utils/find-variable';
5
5
  import { createLintRule } from '../utils/create-rule';
6
- var isParameter = function isParameter(context, identifier) {
7
- var _findVariable;
8
- var definitions = (_findVariable = findVariable({
9
- sourceCode: getSourceCode(context),
10
- identifier: identifier
11
- })) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
12
- if (!definitions || !definitions.length) {
13
- return false;
14
- }
15
- var result = definitions[0];
16
- return result.type === 'Parameter';
17
- };
18
- var isIdentifierAllowed = function isIdentifierAllowed(context, identifier) {
6
+ var _isIdentifierAllowed = function isIdentifierAllowed(context, identifier) {
19
7
  var _variable$identifiers;
20
8
  var variable = findVariable({
21
9
  sourceCode: getSourceCode(context),
@@ -24,47 +12,75 @@ var isIdentifierAllowed = function isIdentifierAllowed(context, identifier) {
24
12
  if (!variable) {
25
13
  return false;
26
14
  }
27
- var variableDefinition = variable.defs[0].node;
28
15
 
29
16
  // If identifier is a RestElement, report warning
30
17
  if (((_variable$identifiers = variable.identifiers[0]) === null || _variable$identifiers === void 0 || (_variable$identifiers = _variable$identifiers.parent) === null || _variable$identifiers === void 0 ? void 0 : _variable$identifiers.type) === 'RestElement') {
31
18
  return false;
32
19
  }
20
+ var firstDef = variable.defs[0];
21
+ if (firstDef.type === 'Parameter') {
22
+ // This ultimately comes from a function parameter, eg. `const value = props.value`
23
+ // ultimately finds this `function (props)` or `function({ width })` and those are parameters
24
+ return true;
25
+ }
26
+ if (firstDef.type !== 'Variable') {
27
+ // If this isn't a variable, eg. not `const value = props.value`, it's not an identifier we can track
28
+ return false;
29
+ }
30
+ var variableDefinition = firstDef.node;
31
+ if (variableDefinition.type !== 'VariableDeclarator') {
32
+ return false;
33
+ }
33
34
 
34
35
  // Checking if identifier has been destructured from props within function
35
- if (variableDefinition.type === 'VariableDeclarator') {
36
- var _init, _init2, _init3, _init4;
37
- // If it's a let variable, ignore
38
- if (variableDefinition.parent.kind === 'let') {
39
- return true;
40
- }
36
+ // If it's a `let var = …`, ignore, we assume it's dynamically set (but it could be static)
37
+ if ('kind' in variableDefinition.parent && variableDefinition.parent.kind === 'let') {
38
+ return true;
39
+ }
41
40
 
42
- // Allow for `const { width } = props as Props` (etc)
43
- var init = variableDefinition.init;
44
- if (((_init = init) === null || _init === void 0 ? void 0 : _init.type) === 'TSAsExpression') {
45
- init = init.expression;
46
- }
41
+ // Allow for `const { width } = props as Props` (etc)
42
+ var init = variableDefinition.init;
43
+
44
+ // This is not typed for TS types
45
+ if (init.type === 'TSAsExpression' && 'expression' in init && typeof init.expression !== 'boolean') {
46
+ init = init.expression;
47
+ }
47
48
 
48
- // If it's initialised by a call expression, ignore
49
- if (((_init2 = init) === null || _init2 === void 0 ? void 0 : _init2.type) === 'CallExpression') {
50
- return true;
49
+ // If it's initialised by a call expression, ignore
50
+ if (init.type === 'CallExpression') {
51
+ return true;
52
+ }
53
+ if (init.type === 'BinaryExpression') {
54
+ // If it's initialized like `const var = 1 * props.value`, validate either side
55
+ if (init.left.type === 'MemberExpression') {
56
+ return _isIdentifierAllowed(context, init.left.object);
51
57
  }
52
- // Destructured from member expression
53
- // e.g. const width = props.width
54
- if (((_init3 = init) === null || _init3 === void 0 ? void 0 : _init3.type) === 'MemberExpression') {
55
- return isParameter(context, init.object);
58
+ if (init.right.type === 'MemberExpression') {
59
+ return _isIdentifierAllowed(context, init.right.object);
56
60
  }
57
- // Destructured from an asserted
58
- // e.g. const width = props.width
59
- if (((_init4 = init) === null || _init4 === void 0 ? void 0 : _init4.type) === 'MemberExpression') {
60
- return isParameter(context, init.object);
61
+ }
62
+ if (init.type === 'ConditionalExpression') {
63
+ // If it's initialized like `const var = props.value ? 42 : 1`, validate either side
64
+ if (init.test.type === 'MemberExpression') {
65
+ return _isIdentifierAllowed(context, init.test.object);
66
+ }
67
+ if (init.consequent.type === 'MemberExpression') {
68
+ return _isIdentifierAllowed(context, init.consequent.object);
69
+ }
70
+ if (init.alternate.type === 'MemberExpression') {
71
+ return _isIdentifierAllowed(context, init.alternate.object);
61
72
  }
62
- // Destructured from object pattern
63
- // e.g. const { width } = props
64
- return isParameter(context, init);
65
73
  }
66
- // If identifier has not been deconstructed from parameter or is a RestElement, report warning
67
- return isParameter(context, identifier);
74
+
75
+ // Destructured from member expression
76
+ // e.g. const width = props.width
77
+ if (init.type === 'MemberExpression') {
78
+ return _isIdentifierAllowed(context, init.object);
79
+ }
80
+
81
+ // Destructured from object pattern
82
+ // e.g. const { width } = props
83
+ return _isIdentifierAllowed(context, init);
68
84
  };
69
85
  function isPropertyValueAllowed(property, context) {
70
86
  if (property.type === 'SpreadElement') {
@@ -75,7 +91,7 @@ function isPropertyValueAllowed(property, context) {
75
91
  if (property.value.type === 'Literal') {
76
92
  return false;
77
93
  } else if (property.value.type === 'Identifier') {
78
- return isIdentifierAllowed(context, property.value);
94
+ return _isIdentifierAllowed(context, property.value);
79
95
  } else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier') {
80
96
  var variable = findVariable({
81
97
  sourceCode: getSourceCode(context),
@@ -90,11 +106,11 @@ function isPropertyValueAllowed(property, context) {
90
106
  return false;
91
107
  }
92
108
  } else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
93
- var _findVariable2;
94
- var definitions = (_findVariable2 = findVariable({
109
+ var _findVariable;
110
+ var definitions = (_findVariable = findVariable({
95
111
  sourceCode: getSourceCode(context),
96
112
  identifier: property.value.object
97
- })) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
113
+ })) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
98
114
  if (!definitions || !definitions.length) {
99
115
  return false;
100
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-ui-styling-standard",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "author": "Atlassian Pty Ltd",
5
5
  "description": "The eslint plugin to enforce and educate on Atlassian's UI Styling Standard",
6
6
  "license": "Apache-2.0",