@atlaskit/eslint-plugin-ui-styling-standard 1.5.1 → 1.6.1

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,21 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 1.6.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [`000d0a2d3aca2`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/000d0a2d3aca2) -
8
+ Remove non-erasable syntax (e.g. enums, namespaces with runtime code) from package
9
+ - Updated dependencies
10
+
11
+ ## 1.6.0
12
+
13
+ ### Minor Changes
14
+
15
+ - [`5db9e3f21a52f`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/5db9e3f21a52f) -
16
+ Changes the `@atlaskit/ui-styling-standard/enforce-style-prop` ESLint rule to allow pass-through
17
+ styles.
18
+
3
19
  ## 1.5.1
4
20
 
5
21
  ### Patch Changes
@@ -141,6 +141,40 @@ function isPropertyValueAllowed(property, context) {
141
141
  }
142
142
  return true;
143
143
  }
144
+
145
+ /**
146
+ * Returns true if the property is not nested within another property.
147
+ */
148
+ function isShallowProperty(property) {
149
+ var objectPattern = property.parent;
150
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(objectPattern, 'ObjectPattern')) {
151
+ return false;
152
+ }
153
+ return (0, _eslintCodemodUtils.isNodeOfType)(objectPattern.parent, 'VariableDeclarator') || (0, _eslintCodemodUtils.isNodeOfType)(objectPattern.parent, 'FunctionDeclaration') || (0, _eslintCodemodUtils.isNodeOfType)(objectPattern.parent, 'FunctionExpression') || (0, _eslintCodemodUtils.isNodeOfType)(objectPattern.parent, 'ArrowFunctionExpression');
154
+ }
155
+ function isAllowedName(name) {
156
+ return name.name === 'style';
157
+ }
158
+ function isAllowedDefinition(definition) {
159
+ var name = definition.name;
160
+ var parent = name.parent;
161
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(parent, 'Property')) {
162
+ return false;
163
+ }
164
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(parent.key, 'Identifier') || !(0, _eslintCodemodUtils.isNodeOfType)(parent.value, 'Identifier') || !isAllowedName(parent.key) || !isAllowedName(parent.value)) {
165
+ return false;
166
+ }
167
+ if (!isShallowProperty(parent)) {
168
+ return false;
169
+ }
170
+ return true;
171
+ }
172
+ function isFirstParameter(identifier) {
173
+ if (!(0, _eslintCodemodUtils.isNodeOfType)(identifier.parent, 'FunctionDeclaration') && !(0, _eslintCodemodUtils.isNodeOfType)(identifier.parent, 'ArrowFunctionExpression') && !(0, _eslintCodemodUtils.isNodeOfType)(identifier.parent, 'FunctionExpression')) {
174
+ return false;
175
+ }
176
+ return identifier.parent.params[0] === identifier;
177
+ }
144
178
  var rule = exports.rule = (0, _createRule.createLintRule)({
145
179
  meta: {
146
180
  name: 'enforce-style-prop',
@@ -178,7 +212,7 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
178
212
  // Typical `style={{ … }}` scenario
179
213
  expression = node.expression;
180
214
  } else if (node.expression.type === 'Identifier') {
181
- var _variable$defs;
215
+ var _variable$defs, _definition$node$init, _variable$defs2;
182
216
  /*
183
217
  * Example:
184
218
  * ```tsx
@@ -190,15 +224,66 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
190
224
  sourceCode: (0, _contextCompat.getSourceCode)(context),
191
225
  identifier: node.expression
192
226
  });
227
+ var definition = variable === null || variable === void 0 || (_variable$defs = variable.defs) === null || _variable$defs === void 0 ? void 0 : _variable$defs[0];
228
+
229
+ /**
230
+ * Allow passing a function parameter directly to the style prop
231
+ *
232
+ * @example
233
+ * function Component({ style }) { return <div style={style} />; }
234
+ *
235
+ * @example
236
+ * <Popper>
237
+ * {({ ref, style }) => <div ref={ref} style={style} />}
238
+ * </Popper>
239
+ */
240
+ if ((definition === null || definition === void 0 ? void 0 : definition.type) === 'Parameter' && isAllowedDefinition(definition)) {
241
+ return;
242
+ }
243
+
244
+ /**
245
+ * Allow passing a value destructured in the body from a function parameter to the style prop
246
+ *
247
+ * @example
248
+ * function Component(props) {
249
+ * const { style } = props;
250
+ * return <div style={style} />;
251
+ * }
252
+ */
253
+ if ((definition === null || definition === void 0 ? void 0 : definition.type) === 'Variable' && ((_definition$node$init = definition.node.init) === null || _definition$node$init === void 0 ? void 0 : _definition$node$init.type) === 'Identifier') {
254
+ var sourceVariable = (0, _findVariable2.findVariable)({
255
+ identifier: definition.node.init,
256
+ sourceCode: (0, _contextCompat.getSourceCode)(context)
257
+ });
258
+ var sourceDefinition = sourceVariable === null || sourceVariable === void 0 ? void 0 : sourceVariable.defs[0];
259
+ if ((sourceDefinition === null || sourceDefinition === void 0 ? void 0 : sourceDefinition.type) === 'Parameter' && isAllowedDefinition(definition)) {
260
+ return;
261
+ }
262
+ }
193
263
 
194
264
  // Types are alluding us here, but we really only want an ObjectExpression here
195
265
  // Additionally, if we have multiple variable definitions, we're in some unhandled scenario
196
266
  // so we're only going to look at the first one…
197
- 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;
267
+ var hopefulObjectExpression = variable === null || variable === void 0 || (_variable$defs2 = variable.defs) === null || _variable$defs2 === void 0 || (_variable$defs2 = _variable$defs2[0]) === null || _variable$defs2 === void 0 || (_variable$defs2 = _variable$defs2.node) === null || _variable$defs2 === void 0 ? void 0 : _variable$defs2.init;
198
268
  if ((hopefulObjectExpression === null || hopefulObjectExpression === void 0 ? void 0 : hopefulObjectExpression.type) === 'ObjectExpression') {
199
269
  expression = hopefulObjectExpression;
200
270
  }
201
271
  reportOnNodeExpression = true;
272
+ } else if (node.expression.type === 'MemberExpression') {
273
+ /**
274
+ * Allow passing `props.style` to the style prop
275
+ */
276
+ if (node.expression.object.type === 'Identifier' && node.expression.property.type === 'Identifier' && isAllowedName(node.expression.property)) {
277
+ var _variable2$defs;
278
+ var _variable2 = (0, _findVariable2.findVariable)({
279
+ sourceCode: (0, _contextCompat.getSourceCode)(context),
280
+ identifier: node.expression.object
281
+ });
282
+ var _definition = _variable2 === null || _variable2 === void 0 || (_variable2$defs = _variable2.defs) === null || _variable2$defs === void 0 ? void 0 : _variable2$defs[0];
283
+ if ((_definition === null || _definition === void 0 ? void 0 : _definition.type) === 'Parameter' && isFirstParameter(_definition.name)) {
284
+ return;
285
+ }
286
+ }
202
287
  }
203
288
  if (!((_expression = expression) !== null && _expression !== void 0 && _expression.properties)) {
204
289
  context.report({
@@ -135,6 +135,42 @@ function isPropertyValueAllowed(property, context) {
135
135
  }
136
136
  return true;
137
137
  }
138
+
139
+ /**
140
+ * Returns true if the property is not nested within another property.
141
+ */
142
+ function isShallowProperty(property) {
143
+ const objectPattern = property.parent;
144
+ if (!isNodeOfType(objectPattern, 'ObjectPattern')) {
145
+ return false;
146
+ }
147
+ return isNodeOfType(objectPattern.parent, 'VariableDeclarator') || isNodeOfType(objectPattern.parent, 'FunctionDeclaration') || isNodeOfType(objectPattern.parent, 'FunctionExpression') || isNodeOfType(objectPattern.parent, 'ArrowFunctionExpression');
148
+ }
149
+ function isAllowedName(name) {
150
+ return name.name === 'style';
151
+ }
152
+ function isAllowedDefinition(definition) {
153
+ const name = definition.name;
154
+ const {
155
+ parent
156
+ } = name;
157
+ if (!isNodeOfType(parent, 'Property')) {
158
+ return false;
159
+ }
160
+ if (!isNodeOfType(parent.key, 'Identifier') || !isNodeOfType(parent.value, 'Identifier') || !isAllowedName(parent.key) || !isAllowedName(parent.value)) {
161
+ return false;
162
+ }
163
+ if (!isShallowProperty(parent)) {
164
+ return false;
165
+ }
166
+ return true;
167
+ }
168
+ function isFirstParameter(identifier) {
169
+ if (!isNodeOfType(identifier.parent, 'FunctionDeclaration') && !isNodeOfType(identifier.parent, 'ArrowFunctionExpression') && !isNodeOfType(identifier.parent, 'FunctionExpression')) {
170
+ return false;
171
+ }
172
+ return identifier.parent.params[0] === identifier;
173
+ }
138
174
  export const rule = createLintRule({
139
175
  meta: {
140
176
  name: 'enforce-style-prop',
@@ -172,7 +208,7 @@ export const rule = createLintRule({
172
208
  // Typical `style={{ … }}` scenario
173
209
  expression = node.expression;
174
210
  } else if (node.expression.type === 'Identifier') {
175
- var _variable$defs, _variable$defs$, _variable$defs$$node;
211
+ var _variable$defs, _definition$node$init, _variable$defs2, _variable$defs2$, _variable$defs2$$node;
176
212
  /*
177
213
  * Example:
178
214
  * ```tsx
@@ -184,15 +220,66 @@ export const rule = createLintRule({
184
220
  sourceCode: getSourceCode(context),
185
221
  identifier: node.expression
186
222
  });
223
+ const definition = variable === null || variable === void 0 ? void 0 : (_variable$defs = variable.defs) === null || _variable$defs === void 0 ? void 0 : _variable$defs[0];
224
+
225
+ /**
226
+ * Allow passing a function parameter directly to the style prop
227
+ *
228
+ * @example
229
+ * function Component({ style }) { return <div style={style} />; }
230
+ *
231
+ * @example
232
+ * <Popper>
233
+ * {({ ref, style }) => <div ref={ref} style={style} />}
234
+ * </Popper>
235
+ */
236
+ if ((definition === null || definition === void 0 ? void 0 : definition.type) === 'Parameter' && isAllowedDefinition(definition)) {
237
+ return;
238
+ }
239
+
240
+ /**
241
+ * Allow passing a value destructured in the body from a function parameter to the style prop
242
+ *
243
+ * @example
244
+ * function Component(props) {
245
+ * const { style } = props;
246
+ * return <div style={style} />;
247
+ * }
248
+ */
249
+ if ((definition === null || definition === void 0 ? void 0 : definition.type) === 'Variable' && ((_definition$node$init = definition.node.init) === null || _definition$node$init === void 0 ? void 0 : _definition$node$init.type) === 'Identifier') {
250
+ const sourceVariable = findVariable({
251
+ identifier: definition.node.init,
252
+ sourceCode: getSourceCode(context)
253
+ });
254
+ const sourceDefinition = sourceVariable === null || sourceVariable === void 0 ? void 0 : sourceVariable.defs[0];
255
+ if ((sourceDefinition === null || sourceDefinition === void 0 ? void 0 : sourceDefinition.type) === 'Parameter' && isAllowedDefinition(definition)) {
256
+ return;
257
+ }
258
+ }
187
259
 
188
260
  // Types are alluding us here, but we really only want an ObjectExpression here
189
261
  // Additionally, if we have multiple variable definitions, we're in some unhandled scenario
190
262
  // so we're only going to look at the first one…
191
- 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;
263
+ const hopefulObjectExpression = variable === null || variable === void 0 ? void 0 : (_variable$defs2 = variable.defs) === null || _variable$defs2 === void 0 ? void 0 : (_variable$defs2$ = _variable$defs2[0]) === null || _variable$defs2$ === void 0 ? void 0 : (_variable$defs2$$node = _variable$defs2$.node) === null || _variable$defs2$$node === void 0 ? void 0 : _variable$defs2$$node.init;
192
264
  if ((hopefulObjectExpression === null || hopefulObjectExpression === void 0 ? void 0 : hopefulObjectExpression.type) === 'ObjectExpression') {
193
265
  expression = hopefulObjectExpression;
194
266
  }
195
267
  reportOnNodeExpression = true;
268
+ } else if (node.expression.type === 'MemberExpression') {
269
+ /**
270
+ * Allow passing `props.style` to the style prop
271
+ */
272
+ if (node.expression.object.type === 'Identifier' && node.expression.property.type === 'Identifier' && isAllowedName(node.expression.property)) {
273
+ var _variable$defs3;
274
+ const variable = findVariable({
275
+ sourceCode: getSourceCode(context),
276
+ identifier: node.expression.object
277
+ });
278
+ const definition = variable === null || variable === void 0 ? void 0 : (_variable$defs3 = variable.defs) === null || _variable$defs3 === void 0 ? void 0 : _variable$defs3[0];
279
+ if ((definition === null || definition === void 0 ? void 0 : definition.type) === 'Parameter' && isFirstParameter(definition.name)) {
280
+ return;
281
+ }
282
+ }
196
283
  }
197
284
  if (!((_expression = expression) !== null && _expression !== void 0 && _expression.properties)) {
198
285
  context.report({
@@ -135,6 +135,40 @@ function isPropertyValueAllowed(property, context) {
135
135
  }
136
136
  return true;
137
137
  }
138
+
139
+ /**
140
+ * Returns true if the property is not nested within another property.
141
+ */
142
+ function isShallowProperty(property) {
143
+ var objectPattern = property.parent;
144
+ if (!isNodeOfType(objectPattern, 'ObjectPattern')) {
145
+ return false;
146
+ }
147
+ return isNodeOfType(objectPattern.parent, 'VariableDeclarator') || isNodeOfType(objectPattern.parent, 'FunctionDeclaration') || isNodeOfType(objectPattern.parent, 'FunctionExpression') || isNodeOfType(objectPattern.parent, 'ArrowFunctionExpression');
148
+ }
149
+ function isAllowedName(name) {
150
+ return name.name === 'style';
151
+ }
152
+ function isAllowedDefinition(definition) {
153
+ var name = definition.name;
154
+ var parent = name.parent;
155
+ if (!isNodeOfType(parent, 'Property')) {
156
+ return false;
157
+ }
158
+ if (!isNodeOfType(parent.key, 'Identifier') || !isNodeOfType(parent.value, 'Identifier') || !isAllowedName(parent.key) || !isAllowedName(parent.value)) {
159
+ return false;
160
+ }
161
+ if (!isShallowProperty(parent)) {
162
+ return false;
163
+ }
164
+ return true;
165
+ }
166
+ function isFirstParameter(identifier) {
167
+ if (!isNodeOfType(identifier.parent, 'FunctionDeclaration') && !isNodeOfType(identifier.parent, 'ArrowFunctionExpression') && !isNodeOfType(identifier.parent, 'FunctionExpression')) {
168
+ return false;
169
+ }
170
+ return identifier.parent.params[0] === identifier;
171
+ }
138
172
  export var rule = createLintRule({
139
173
  meta: {
140
174
  name: 'enforce-style-prop',
@@ -172,7 +206,7 @@ export var rule = createLintRule({
172
206
  // Typical `style={{ … }}` scenario
173
207
  expression = node.expression;
174
208
  } else if (node.expression.type === 'Identifier') {
175
- var _variable$defs;
209
+ var _variable$defs, _definition$node$init, _variable$defs2;
176
210
  /*
177
211
  * Example:
178
212
  * ```tsx
@@ -184,15 +218,66 @@ export var rule = createLintRule({
184
218
  sourceCode: getSourceCode(context),
185
219
  identifier: node.expression
186
220
  });
221
+ var definition = variable === null || variable === void 0 || (_variable$defs = variable.defs) === null || _variable$defs === void 0 ? void 0 : _variable$defs[0];
222
+
223
+ /**
224
+ * Allow passing a function parameter directly to the style prop
225
+ *
226
+ * @example
227
+ * function Component({ style }) { return <div style={style} />; }
228
+ *
229
+ * @example
230
+ * <Popper>
231
+ * {({ ref, style }) => <div ref={ref} style={style} />}
232
+ * </Popper>
233
+ */
234
+ if ((definition === null || definition === void 0 ? void 0 : definition.type) === 'Parameter' && isAllowedDefinition(definition)) {
235
+ return;
236
+ }
237
+
238
+ /**
239
+ * Allow passing a value destructured in the body from a function parameter to the style prop
240
+ *
241
+ * @example
242
+ * function Component(props) {
243
+ * const { style } = props;
244
+ * return <div style={style} />;
245
+ * }
246
+ */
247
+ if ((definition === null || definition === void 0 ? void 0 : definition.type) === 'Variable' && ((_definition$node$init = definition.node.init) === null || _definition$node$init === void 0 ? void 0 : _definition$node$init.type) === 'Identifier') {
248
+ var sourceVariable = findVariable({
249
+ identifier: definition.node.init,
250
+ sourceCode: getSourceCode(context)
251
+ });
252
+ var sourceDefinition = sourceVariable === null || sourceVariable === void 0 ? void 0 : sourceVariable.defs[0];
253
+ if ((sourceDefinition === null || sourceDefinition === void 0 ? void 0 : sourceDefinition.type) === 'Parameter' && isAllowedDefinition(definition)) {
254
+ return;
255
+ }
256
+ }
187
257
 
188
258
  // Types are alluding us here, but we really only want an ObjectExpression here
189
259
  // Additionally, if we have multiple variable definitions, we're in some unhandled scenario
190
260
  // so we're only going to look at the first one…
191
- 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;
261
+ var hopefulObjectExpression = variable === null || variable === void 0 || (_variable$defs2 = variable.defs) === null || _variable$defs2 === void 0 || (_variable$defs2 = _variable$defs2[0]) === null || _variable$defs2 === void 0 || (_variable$defs2 = _variable$defs2.node) === null || _variable$defs2 === void 0 ? void 0 : _variable$defs2.init;
192
262
  if ((hopefulObjectExpression === null || hopefulObjectExpression === void 0 ? void 0 : hopefulObjectExpression.type) === 'ObjectExpression') {
193
263
  expression = hopefulObjectExpression;
194
264
  }
195
265
  reportOnNodeExpression = true;
266
+ } else if (node.expression.type === 'MemberExpression') {
267
+ /**
268
+ * Allow passing `props.style` to the style prop
269
+ */
270
+ if (node.expression.object.type === 'Identifier' && node.expression.property.type === 'Identifier' && isAllowedName(node.expression.property)) {
271
+ var _variable2$defs;
272
+ var _variable2 = findVariable({
273
+ sourceCode: getSourceCode(context),
274
+ identifier: node.expression.object
275
+ });
276
+ var _definition = _variable2 === null || _variable2 === void 0 || (_variable2$defs = _variable2.defs) === null || _variable2$defs === void 0 ? void 0 : _variable2$defs[0];
277
+ if ((_definition === null || _definition === void 0 ? void 0 : _definition.type) === 'Parameter' && isFirstParameter(_definition.name)) {
278
+ return;
279
+ }
280
+ }
196
281
  }
197
282
  if (!((_expression = expression) !== null && _expression !== void 0 && _expression.properties)) {
198
283
  context.report({
@@ -1,10 +1,10 @@
1
1
  import type { Rule } from 'eslint';
2
2
  import type { CallExpression } from 'estree-jsx';
3
3
  export declare class Linter {
4
- private readonly context;
5
- private readonly baseNode;
6
4
  private readonly allowedDynamicKeys;
7
5
  private readonly allowedFunctionCalls;
6
+ private readonly context;
7
+ private readonly baseNode;
8
8
  constructor(context: Rule.RuleContext, baseNode: CallExpression);
9
9
  /**
10
10
  * Wrapper around `context.report` to provide type checking on the message ID.
@@ -1,10 +1,10 @@
1
1
  import type { Rule } from 'eslint';
2
2
  import type { CallExpression } from 'estree-jsx';
3
3
  export declare class Linter {
4
- private readonly context;
5
- private readonly baseNode;
6
4
  private readonly allowedDynamicKeys;
7
5
  private readonly allowedFunctionCalls;
6
+ private readonly context;
7
+ private readonly baseNode;
8
8
  constructor(context: Rule.RuleContext, baseNode: CallExpression);
9
9
  /**
10
10
  * Wrapper around `context.report` to provide type checking on the message ID.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-ui-styling-standard",
3
- "version": "1.5.1",
3
+ "version": "1.6.1",
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",