@atlaskit/eslint-plugin-ui-styling-standard 1.0.3 → 1.1.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 CHANGED
@@ -1,5 +1,22 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#178147](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/178147)
8
+ [`06f80813d818b`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/06f80813d818b) -
9
+ Handle the false-positive of enforce-style-prop when assigned via `style={style}` passing around
10
+ full objects, rather than simply inline `style={{ … }}` objects.
11
+
12
+ ## 1.0.4
13
+
14
+ ### Patch Changes
15
+
16
+ - [#171997](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/171997)
17
+ [`a433756c2ec76`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/a433756c2ec76) -
18
+ Remove dependency not longer needed
19
+
3
20
  ## 1.0.3
4
21
 
5
22
  ### Patch Changes
@@ -31,8 +31,6 @@ var isIdentifierAllowed = function isIdentifierAllowed(context, identifier) {
31
31
  }
32
32
  var variableDefinition = variable.defs[0].node;
33
33
 
34
- // console.log('@@variableDefinition', variableDefinition);
35
-
36
34
  // If identifier is a RestElement, report warning
37
35
  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') {
38
36
  return false;
@@ -77,6 +75,7 @@ function isPropertyValueAllowed(property, context) {
77
75
  if (property.type === 'SpreadElement') {
78
76
  return false;
79
77
  }
78
+
80
79
  // property.type === 'Property', moving on to check if value is static
81
80
  if (property.value.type === 'Literal') {
82
81
  return false;
@@ -141,14 +140,15 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
141
140
  create: function create(context) {
142
141
  return {
143
142
  'JSXAttribute[name.name="style"] > JSXExpressionContainer': function JSXAttributeNameNameStyleJSXExpressionContainer(node) {
144
- var _node$expression$expr;
143
+ var _node$expression$expr, _expression;
145
144
  if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'JSXExpressionContainer')) {
146
145
  return;
147
146
  }
148
147
 
149
148
  // we've reached an attribute named style
150
149
 
151
- var expression;
150
+ var expression = null;
151
+ var reportOnNodeExpression = false;
152
152
  if (
153
153
  // @ts-expect-error -- TSAsExpression is not valid in estree
154
154
  node.expression.type === 'TSAsExpression' &&
@@ -160,21 +160,59 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
160
160
  } else if (node.expression.type === 'ObjectExpression') {
161
161
  // Typical `style={{ … }}` scenario
162
162
  expression = node.expression;
163
- } else {
163
+ } else if (node.expression.type === 'Identifier') {
164
+ var _variable$defs;
165
+ /*
166
+ * Example:
167
+ * ```tsx
168
+ * const style = { width: 100 };
169
+ * <div style={style} />
170
+ * ```
171
+ */
172
+ var variable = (0, _findVariable3.findVariable)({
173
+ sourceCode: context.getSourceCode(),
174
+ identifier: node.expression
175
+ });
176
+
177
+ // Types are alluding us here, but we really only want an ObjectExpression here
178
+ // Additionally, if we have multiple variable definitions, we're in some unhandled scenario
179
+ // so we're only going to look at the first one…
180
+ var hopefulObjectExpression = variable === null || variable === void 0 || (_variable$defs = variable.defs) === null || _variable$defs === void 0 || (_variable$defs = _variable$defs[0]) === null || _variable$defs === void 0 || (_variable$defs = _variable$defs.node) === null || _variable$defs === void 0 ? void 0 : _variable$defs.init;
181
+ if ((hopefulObjectExpression === null || hopefulObjectExpression === void 0 ? void 0 : hopefulObjectExpression.type) === 'ObjectExpression') {
182
+ expression = hopefulObjectExpression;
183
+ }
184
+ reportOnNodeExpression = true;
185
+ }
186
+ if (!((_expression = expression) !== null && _expression !== void 0 && _expression.properties)) {
164
187
  context.report({
165
188
  node: node,
166
189
  messageId: 'enforce-style-prop'
167
190
  });
168
191
  return;
169
192
  }
170
- expression.properties.forEach(function (value) {
171
- if (!isPropertyValueAllowed(value, context)) {
193
+ if (reportOnNodeExpression) {
194
+ // In some cases we want to report on the `node.expression` itself, eg.
195
+ // `styles` in `style={styles}` should get the error
196
+ if (expression.properties.some(function (value) {
197
+ return !isPropertyValueAllowed(value, context);
198
+ })) {
172
199
  context.report({
173
- node: value,
200
+ node: node.expression,
174
201
  messageId: 'enforce-style-prop'
175
202
  });
176
203
  }
177
- });
204
+ } else {
205
+ // Typically we want to report on the individual styles, eg.
206
+ // `color: 'red'` in `style={{ color: 'red' }}` should get the error
207
+ expression.properties.forEach(function (value) {
208
+ if (!isPropertyValueAllowed(value, context)) {
209
+ context.report({
210
+ node: value,
211
+ messageId: 'enforce-style-prop'
212
+ });
213
+ }
214
+ });
215
+ }
178
216
  }
179
217
  };
180
218
  }
@@ -38,7 +38,7 @@ var Linter = exports.Linter = /*#__PURE__*/function () {
38
38
  /**
39
39
  * Wrapper around `context.report` to provide type checking on the message ID.
40
40
  */
41
- (0, _createClass2.default)(Linter, [{
41
+ return (0, _createClass2.default)(Linter, [{
42
42
  key: "report",
43
43
  value: function report(descriptor) {
44
44
  this.context.report(descriptor);
@@ -433,5 +433,4 @@ var Linter = exports.Linter = /*#__PURE__*/function () {
433
433
  this.lintArguments(this.baseNode.arguments);
434
434
  }
435
435
  }]);
436
- return Linter;
437
436
  }();
@@ -25,8 +25,6 @@ const isIdentifierAllowed = (context, identifier) => {
25
25
  }
26
26
  const variableDefinition = variable.defs[0].node;
27
27
 
28
- // console.log('@@variableDefinition', variableDefinition);
29
-
30
28
  // If identifier is a RestElement, report warning
31
29
  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') {
32
30
  return false;
@@ -71,6 +69,7 @@ function isPropertyValueAllowed(property, context) {
71
69
  if (property.type === 'SpreadElement') {
72
70
  return false;
73
71
  }
72
+
74
73
  // property.type === 'Property', moving on to check if value is static
75
74
  if (property.value.type === 'Literal') {
76
75
  return false;
@@ -135,14 +134,15 @@ export const rule = createLintRule({
135
134
  create(context) {
136
135
  return {
137
136
  'JSXAttribute[name.name="style"] > JSXExpressionContainer': node => {
138
- var _node$expression$expr;
137
+ var _node$expression$expr, _expression;
139
138
  if (!isNodeOfType(node, 'JSXExpressionContainer')) {
140
139
  return;
141
140
  }
142
141
 
143
142
  // we've reached an attribute named style
144
143
 
145
- let expression;
144
+ let expression = null;
145
+ let reportOnNodeExpression = false;
146
146
  if (
147
147
  // @ts-expect-error -- TSAsExpression is not valid in estree
148
148
  node.expression.type === 'TSAsExpression' &&
@@ -154,21 +154,57 @@ export const rule = createLintRule({
154
154
  } else if (node.expression.type === 'ObjectExpression') {
155
155
  // Typical `style={{ … }}` scenario
156
156
  expression = node.expression;
157
- } else {
157
+ } else if (node.expression.type === 'Identifier') {
158
+ var _variable$defs, _variable$defs$, _variable$defs$$node;
159
+ /*
160
+ * Example:
161
+ * ```tsx
162
+ * const style = { width: 100 };
163
+ * <div style={style} />
164
+ * ```
165
+ */
166
+ const variable = findVariable({
167
+ sourceCode: context.getSourceCode(),
168
+ identifier: node.expression
169
+ });
170
+
171
+ // Types are alluding us here, but we really only want an ObjectExpression here
172
+ // Additionally, if we have multiple variable definitions, we're in some unhandled scenario
173
+ // so we're only going to look at the first one…
174
+ const hopefulObjectExpression = variable === null || variable === void 0 ? void 0 : (_variable$defs = variable.defs) === null || _variable$defs === void 0 ? void 0 : (_variable$defs$ = _variable$defs[0]) === null || _variable$defs$ === void 0 ? void 0 : (_variable$defs$$node = _variable$defs$.node) === null || _variable$defs$$node === void 0 ? void 0 : _variable$defs$$node.init;
175
+ if ((hopefulObjectExpression === null || hopefulObjectExpression === void 0 ? void 0 : hopefulObjectExpression.type) === 'ObjectExpression') {
176
+ expression = hopefulObjectExpression;
177
+ }
178
+ reportOnNodeExpression = true;
179
+ }
180
+ if (!((_expression = expression) !== null && _expression !== void 0 && _expression.properties)) {
158
181
  context.report({
159
182
  node: node,
160
183
  messageId: 'enforce-style-prop'
161
184
  });
162
185
  return;
163
186
  }
164
- expression.properties.forEach(value => {
165
- if (!isPropertyValueAllowed(value, context)) {
187
+ if (reportOnNodeExpression) {
188
+ // In some cases we want to report on the `node.expression` itself, eg.
189
+ // `styles` in `style={styles}` should get the error
190
+ if (expression.properties.some(value => !isPropertyValueAllowed(value, context))) {
166
191
  context.report({
167
- node: value,
192
+ node: node.expression,
168
193
  messageId: 'enforce-style-prop'
169
194
  });
170
195
  }
171
- });
196
+ } else {
197
+ // Typically we want to report on the individual styles, eg.
198
+ // `color: 'red'` in `style={{ color: 'red' }}` should get the error
199
+ expression.properties.forEach(value => {
200
+ if (!isPropertyValueAllowed(value, context)) {
201
+ context.report({
202
+ node: value,
203
+ messageId: 'enforce-style-prop'
204
+ });
205
+ }
206
+ });
207
+ }
172
208
  }
173
209
  };
174
210
  }
@@ -1,5 +1,4 @@
1
1
  // eslint-disable-next-line @atlassian/tangerine/import/entry-points
2
-
3
2
  import esquery from 'esquery';
4
3
  import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
5
4
  import { createLintRule } from '../utils/create-rule';
@@ -25,8 +25,6 @@ var isIdentifierAllowed = function isIdentifierAllowed(context, identifier) {
25
25
  }
26
26
  var variableDefinition = variable.defs[0].node;
27
27
 
28
- // console.log('@@variableDefinition', variableDefinition);
29
-
30
28
  // If identifier is a RestElement, report warning
31
29
  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') {
32
30
  return false;
@@ -71,6 +69,7 @@ function isPropertyValueAllowed(property, context) {
71
69
  if (property.type === 'SpreadElement') {
72
70
  return false;
73
71
  }
72
+
74
73
  // property.type === 'Property', moving on to check if value is static
75
74
  if (property.value.type === 'Literal') {
76
75
  return false;
@@ -135,14 +134,15 @@ export var rule = createLintRule({
135
134
  create: function create(context) {
136
135
  return {
137
136
  'JSXAttribute[name.name="style"] > JSXExpressionContainer': function JSXAttributeNameNameStyleJSXExpressionContainer(node) {
138
- var _node$expression$expr;
137
+ var _node$expression$expr, _expression;
139
138
  if (!isNodeOfType(node, 'JSXExpressionContainer')) {
140
139
  return;
141
140
  }
142
141
 
143
142
  // we've reached an attribute named style
144
143
 
145
- var expression;
144
+ var expression = null;
145
+ var reportOnNodeExpression = false;
146
146
  if (
147
147
  // @ts-expect-error -- TSAsExpression is not valid in estree
148
148
  node.expression.type === 'TSAsExpression' &&
@@ -154,21 +154,59 @@ export var rule = createLintRule({
154
154
  } else if (node.expression.type === 'ObjectExpression') {
155
155
  // Typical `style={{ … }}` scenario
156
156
  expression = node.expression;
157
- } else {
157
+ } else if (node.expression.type === 'Identifier') {
158
+ var _variable$defs;
159
+ /*
160
+ * Example:
161
+ * ```tsx
162
+ * const style = { width: 100 };
163
+ * <div style={style} />
164
+ * ```
165
+ */
166
+ var variable = findVariable({
167
+ sourceCode: context.getSourceCode(),
168
+ identifier: node.expression
169
+ });
170
+
171
+ // Types are alluding us here, but we really only want an ObjectExpression here
172
+ // Additionally, if we have multiple variable definitions, we're in some unhandled scenario
173
+ // so we're only going to look at the first one…
174
+ var hopefulObjectExpression = variable === null || variable === void 0 || (_variable$defs = variable.defs) === null || _variable$defs === void 0 || (_variable$defs = _variable$defs[0]) === null || _variable$defs === void 0 || (_variable$defs = _variable$defs.node) === null || _variable$defs === void 0 ? void 0 : _variable$defs.init;
175
+ if ((hopefulObjectExpression === null || hopefulObjectExpression === void 0 ? void 0 : hopefulObjectExpression.type) === 'ObjectExpression') {
176
+ expression = hopefulObjectExpression;
177
+ }
178
+ reportOnNodeExpression = true;
179
+ }
180
+ if (!((_expression = expression) !== null && _expression !== void 0 && _expression.properties)) {
158
181
  context.report({
159
182
  node: node,
160
183
  messageId: 'enforce-style-prop'
161
184
  });
162
185
  return;
163
186
  }
164
- expression.properties.forEach(function (value) {
165
- if (!isPropertyValueAllowed(value, context)) {
187
+ if (reportOnNodeExpression) {
188
+ // In some cases we want to report on the `node.expression` itself, eg.
189
+ // `styles` in `style={styles}` should get the error
190
+ if (expression.properties.some(function (value) {
191
+ return !isPropertyValueAllowed(value, context);
192
+ })) {
166
193
  context.report({
167
- node: value,
194
+ node: node.expression,
168
195
  messageId: 'enforce-style-prop'
169
196
  });
170
197
  }
171
- });
198
+ } else {
199
+ // Typically we want to report on the individual styles, eg.
200
+ // `color: 'red'` in `style={{ color: 'red' }}` should get the error
201
+ expression.properties.forEach(function (value) {
202
+ if (!isPropertyValueAllowed(value, context)) {
203
+ context.report({
204
+ node: value,
205
+ messageId: 'enforce-style-prop'
206
+ });
207
+ }
208
+ });
209
+ }
172
210
  }
173
211
  };
174
212
  }
@@ -2,7 +2,6 @@ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol
2
2
  function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
3
3
  function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
4
4
  // eslint-disable-next-line @atlassian/tangerine/import/entry-points
5
-
6
5
  import esquery from 'esquery';
7
6
  import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
8
7
  import { createLintRule } from '../utils/create-rule';
@@ -31,7 +31,7 @@ export var Linter = /*#__PURE__*/function () {
31
31
  /**
32
32
  * Wrapper around `context.report` to provide type checking on the message ID.
33
33
  */
34
- _createClass(Linter, [{
34
+ return _createClass(Linter, [{
35
35
  key: "report",
36
36
  value: function report(descriptor) {
37
37
  this.context.report(descriptor);
@@ -426,5 +426,4 @@ export var Linter = /*#__PURE__*/function () {
426
426
  this.lintArguments(this.baseNode.arguments);
427
427
  }
428
428
  }]);
429
- return Linter;
430
429
  }();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-ui-styling-standard",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
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",
@@ -40,10 +40,10 @@
40
40
  "./package.json": "./package.json"
41
41
  },
42
42
  "dependencies": {
43
- "@atlaskit/eslint-plugin-design-system": "^11.0.0",
43
+ "@atlaskit/eslint-plugin-design-system": "^11.4.0",
44
44
  "@atlaskit/eslint-utils": "^1.7.0",
45
45
  "@babel/runtime": "^7.0.0",
46
- "@typescript-eslint/utils": "^5.48.1",
46
+ "@typescript-eslint/utils": "^7.1.0",
47
47
  "ajv": "^6.12.6",
48
48
  "eslint-codemod-utils": "^1.8.6",
49
49
  "eslint-scope": "^8.0.0",
@@ -58,18 +58,15 @@
58
58
  "@atlaskit/ds-lib": "^3.3.0",
59
59
  "@atlassian/codegen": "*",
60
60
  "@atlassian/ts-loader": "*",
61
- "@compiled/eslint-plugin": "^0.17.0",
62
61
  "@eslint/eslintrc": "^3.0.2",
63
62
  "@types/eslint": "^8.56.6",
64
63
  "@types/eslint__eslintrc": "^2.1.2",
65
64
  "@types/estraverse": "^5.1.7",
65
+ "@types/json-schema": "^7.0.15",
66
66
  "eslint": "^8.57.0",
67
67
  "lodash": "^4.17.21",
68
68
  "typescript": "~5.4.2"
69
69
  },
70
- "peerDependencies": {
71
- "@compiled/eslint-plugin": "^0.17.0"
72
- },
73
70
  "scripts": {
74
71
  "codegen": "run-ts ./scripts/codegen.tsx"
75
72
  }