@atlaskit/eslint-plugin-design-system 8.38.0 → 9.0.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,18 @@
1
1
  # @atlaskit/eslint-plugin-design-system
2
2
 
3
+ ## 9.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [#83454](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/83454) [`be8b7ad6ff8e`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/be8b7ad6ff8e) - Remove name autofixer from `consistent-css-prop-usage`. Variables for css / xcss / cssMap functions will no longer be required to end with "Styles".
8
+
9
+ [BREAKING] Some rule options have been changed:
10
+
11
+ - `fixNamesOnly` and `autoFixNames` have been removed, as there is no longer an autofixer that enforces variable names.
12
+ - If you use `fixNamesOnly: true`, we recommend switching to using `autoFix: false`.
13
+ - Users of the `autoFixNames` option should remove this from their configuration.
14
+ - `autoFix` option has been added. This controls whether the remaining autofixers should run or not (e.g. hoisting `css` function calls, wrapping objects in `css` function calls), and is `true` by default.
15
+
3
16
  ## 8.38.0
4
17
 
5
18
  ### Minor Changes
@@ -16,7 +16,6 @@ This rule checks for the following cases:
16
16
  - Styles must be defined in the same file as their usage, and not be imported.
17
17
  - Styles should not contain spread operators (e.g. `css({ ...spreadStyles })`).
18
18
  - Styles must all be defined at the top of the file, or at the bottom of the file.
19
- - Styles must be in a variable whose name ends in `styles` (or `Styles`).
20
19
 
21
20
  This rule also has an autofixer that enforces and fixes the code (where practical) to meet the above requirements.
22
21
 
@@ -81,7 +80,7 @@ function Button({ children }) {
81
80
 
82
81
  ### Correct
83
82
 
84
- **Using the css() function to create a style object that follows the naming convention (ends in Styles) and passing it as a variable into the css={...} JSX attribute.**
83
+ **Using the css() function to create a style object and passing it as a variable into the css={...} JSX attribute.**
85
84
 
86
85
  With the following options turned on:
87
86
 
@@ -181,23 +180,8 @@ Whether to exclude `css` attributes of React components from being affected by t
181
180
 
182
181
  This is `false` by default.
183
182
 
184
- ### autoFixNames
183
+ ### autoFix
185
184
 
186
- When set to `true`, this rule will _turn on_ an autofixer that adds `Styles` at the end of existing style variables.
187
-
188
- ```tsx
189
- // vvvvv will be renamed to `myCssStyles`
190
- const myCss = { color: 'blue' };
191
- ```
192
-
193
- Note that when hoisting styles, the name of the newly created style variable follows its own naming scheme (`styles`, `styles_1`, `styles_2`, ...) and is not affected by the value of `autoFixNames`.
185
+ When set to `true`, this rule will turn on the autofixer. Set this to `false` if you do not want the autofixers to run.
194
186
 
195
187
  This is `true` by default.
196
-
197
- ### fixNamesOnly
198
-
199
- When set to `true`, this rule will _turn off_ all autofixers other than the autofixer adding `Styles` to the end of existing style variables (which is controlled by `autoFixNames`). In other words, styles will no longer be hoisted and styles will no longer be wrapped in a `css`/`xcss` function call.
200
-
201
- This is `false` by default.
202
-
203
- If `autoFixNames` is `false` and `fixNamesOnly` is `true`, all autofixers will be turned off.
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
7
  exports.default = void 0;
8
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
8
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10
9
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
10
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
@@ -40,6 +39,18 @@ var getProgramNode = function getProgramNode(expression) {
40
39
  }
41
40
  return expression.parent;
42
41
  };
42
+ var isDeclaredInsideComponent = function isDeclaredInsideComponent(expression) {
43
+ // These nodes imply that there is a distinct own scope (function scope / block scope),
44
+ // and so the presence of them means that expression was not defined in the module scope.
45
+ var NOT_MODULE_SCOPE = ['ArrowFunctionExpression', 'BlockStatement', 'ClassDeclaration', 'FunctionExpression'];
46
+ while (expression.type !== 'Program') {
47
+ if (NOT_MODULE_SCOPE.includes(expression.type)) {
48
+ return true;
49
+ }
50
+ expression = expression.parent;
51
+ }
52
+ return false;
53
+ };
43
54
  var JSXExpressionLinter = /*#__PURE__*/function () {
44
55
  // File-level tracking of styles hoisted from the cssAtTopOfModule/cssAtBottomOfModule fixers.
45
56
 
@@ -119,13 +130,13 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
119
130
  });
120
131
  return;
121
132
  }
122
- if (identifier.parent.parent.parent.type !== 'Program') {
133
+ if (isDeclaredInsideComponent(identifier)) {
123
134
  // When variable is declared inside the component
124
135
  this.context.report({
125
136
  node: sourceIdentifier,
126
137
  messageId: this.configuration.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule',
127
138
  fix: function fix(fixer) {
128
- if (_this.configuration.fixNamesOnly) {
139
+ if (!_this.configuration.autoFix) {
129
140
  return [];
130
141
  }
131
142
  return _this.fixCssNotInModuleScope(fixer, identifier, false);
@@ -147,7 +158,7 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
147
158
  node: identifier,
148
159
  messageId: 'cssObjectTypeOnly',
149
160
  fix: function fix(fixer) {
150
- if (_this.configuration.fixNamesOnly) {
161
+ if (!_this.configuration.autoFix) {
151
162
  return [];
152
163
  }
153
164
  return _this.addCssFunctionCall(fixer, identifier.parent);
@@ -323,10 +334,11 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
323
334
  // The last value is the bottom of the file
324
335
  fixerNodePlacement = programNode.body[programNode.body.length - 1];
325
336
  } else {
337
+ var _ref3;
326
338
  // Place after the last ImportDeclaration
327
- fixerNodePlacement = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(function (node) {
339
+ fixerNodePlacement = (_ref3 = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(function (node) {
328
340
  return node.type !== 'ImportDeclaration';
329
- });
341
+ })) !== null && _ref3 !== void 0 ? _ref3 : fixerNodePlacement;
330
342
  }
331
343
  var moduleString;
332
344
  var fixes = [];
@@ -410,7 +422,7 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
410
422
  node: expression,
411
423
  messageId: this.configuration.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule',
412
424
  fix: function fix(fixer) {
413
- if (_this3.configuration.fixNamesOnly) {
425
+ if (!_this3.configuration.autoFix) {
414
426
  return [];
415
427
  }
416
428
 
@@ -455,8 +467,7 @@ var defaultConfig = {
455
467
  cssImportSource: _isSupportedImport.CSS_IN_JS_IMPORTS.compiled,
456
468
  xcssImportSource: _isSupportedImport.CSS_IN_JS_IMPORTS.atlaskitPrimitives,
457
469
  excludeReactComponents: false,
458
- fixNamesOnly: false,
459
- autoFixNames: true
470
+ autoFix: true
460
471
  };
461
472
  var rule = (0, _createRule.createLintRule)({
462
473
  meta: {
@@ -475,8 +486,7 @@ var rule = (0, _createRule.createLintRule)({
475
486
  cssObjectTypeOnly: "Create styles using objects passed to a css function call, e.g. `css({ textAlign: 'center'; })`.",
476
487
  cssInModule: "Imported styles should not be used; instead define in the module, import a component, or use a design token.",
477
488
  cssArrayStylesOnly: "Compose styles with an array on the css prop instead of using object spread.",
478
- noMemberExpressions: "Styles should be a regular variable (e.g. 'buttonStyles'), not a member of an object (e.g. 'myObject.styles').",
479
- shouldEndInStyles: 'Declared styles should end in "styles".'
489
+ noMemberExpressions: "Styles should be a regular variable (e.g. 'buttonStyles'), not a member of an object (e.g. 'myObject.styles')."
480
490
  },
481
491
  schema: [{
482
492
  type: 'object',
@@ -500,10 +510,7 @@ var rule = (0, _createRule.createLintRule)({
500
510
  excludeReactComponents: {
501
511
  type: 'boolean'
502
512
  },
503
- autoFixNames: {
504
- type: 'boolean'
505
- },
506
- fixNamesOnly: {
513
+ autoFix: {
507
514
  type: 'boolean'
508
515
  }
509
516
  },
@@ -511,80 +518,41 @@ var rule = (0, _createRule.createLintRule)({
511
518
  }]
512
519
  },
513
520
  create: function create(context) {
514
- var _ref3;
515
521
  var mergedConfig = (0, _assign.default)({}, defaultConfig, context.options[0]);
516
- var declarationSuffix = 'Styles';
517
- var callSelectorFunctions = [].concat((0, _toConsumableArray2.default)(mergedConfig.cssFunctions), ['cssMap']);
518
- var callSelector = callSelectorFunctions.map(function (fn) {
519
- return "CallExpression[callee.name=".concat(fn, "]");
520
- }).join(',');
521
- return _ref3 = {}, (0, _defineProperty2.default)(_ref3, callSelector, function (node) {
522
- if (node.parent.type !== 'VariableDeclarator') {
523
- // We aren't interested in these that don't have a parent.
524
- return;
525
- }
526
- var identifier = node.parent.id;
527
- if (identifier.type === 'Identifier' && (identifier.name.endsWith(declarationSuffix) || identifier.name.startsWith(declarationSuffix.toLowerCase()) || identifier.name === declarationSuffix.toLowerCase())) {
528
- // Already prefixed! Nothing to do.
529
- return;
530
- }
531
- if (!mergedConfig.autoFixNames) {
532
- return;
533
- }
534
- context.report({
535
- node: identifier,
536
- messageId: 'shouldEndInStyles',
537
- fix: function fix(fixer) {
538
- var _context$getScope$var;
539
- var identifierName = identifier.type === 'Identifier' ? identifier.name : '';
540
- var references = ((_context$getScope$var = context.getScope().variables.find(function (varb) {
541
- return varb.name === identifierName;
542
- })) === null || _context$getScope$var === void 0 ? void 0 : _context$getScope$var.references) || [];
543
- var newIdentifierName = "".concat(identifierName
544
- // Remove "Style" if it is already defined.
545
- .replace(/Style$/, '')).concat(declarationSuffix);
546
- return references.filter(function (ref) {
547
- return ref.identifier.name === identifierName;
548
- }).map(function (ref) {
549
- if (ref.identifier.parent && ref.identifier.parent.shorthand) {
550
- return fixer.replaceText(ref.identifier, "".concat(identifierName, ": ").concat(newIdentifierName));
551
- }
552
- return fixer.replaceText(ref.identifier, newIdentifierName);
553
- });
554
- }
555
- });
556
- }), (0, _defineProperty2.default)(_ref3, "JSXAttribute", function JSXAttribute(nodeOriginal) {
557
- var node = nodeOriginal;
558
- var name = node.name,
559
- value = node.value;
560
- if (mergedConfig.excludeReactComponents && node.parent.type === 'JSXOpeningElement') {
561
- // e.g. <item.before />
562
- if (node.parent.name.type === 'JSXMemberExpression') {
563
- return;
564
- }
565
- // e.g. <div />, <MenuItem />
566
- if (node.parent.name.type === 'JSXIdentifier' && !isDOMElementName(node.parent.name.name)) {
567
- return;
568
- }
569
- }
570
- if (name.type === 'JSXIdentifier' && mergedConfig.cssFunctions.includes(name.name)) {
571
- // When not a jsx expression. For eg. css=""
572
- if ((value === null || value === void 0 ? void 0 : value.type) !== 'JSXExpressionContainer') {
573
- context.report({
574
- node: node,
575
- messageId: mergedConfig.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule'
576
- });
577
- return;
522
+ return {
523
+ JSXAttribute: function JSXAttribute(nodeOriginal) {
524
+ var node = nodeOriginal;
525
+ var name = node.name,
526
+ value = node.value;
527
+ if (mergedConfig.excludeReactComponents && node.parent.type === 'JSXOpeningElement') {
528
+ // e.g. <item.before />
529
+ if (node.parent.name.type === 'JSXMemberExpression') {
530
+ return;
531
+ }
532
+ // e.g. <div />, <MenuItem />
533
+ if (node.parent.name.type === 'JSXIdentifier' && !isDOMElementName(node.parent.name.name)) {
534
+ return;
535
+ }
578
536
  }
579
- if (value.expression.type === 'JSXEmptyExpression') {
580
- // e.g. the comment in
581
- // <div css={/* Hello there */} />
582
- return;
537
+ if (name.type === 'JSXIdentifier' && mergedConfig.cssFunctions.includes(name.name)) {
538
+ // When not a jsx expression. For eg. css=""
539
+ if ((value === null || value === void 0 ? void 0 : value.type) !== 'JSXExpressionContainer') {
540
+ context.report({
541
+ node: node,
542
+ messageId: mergedConfig.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule'
543
+ });
544
+ return;
545
+ }
546
+ if (value.expression.type === 'JSXEmptyExpression') {
547
+ // e.g. the comment in
548
+ // <div css={/* Hello there */} />
549
+ return;
550
+ }
551
+ var linter = new JSXExpressionLinter(context, name.name, mergedConfig, value.expression);
552
+ linter.run();
583
553
  }
584
- var linter = new JSXExpressionLinter(context, name.name, mergedConfig, value.expression);
585
- linter.run();
586
554
  }
587
- }), _ref3;
555
+ };
588
556
  }
589
557
  });
590
558
  var _default = exports.default = rule;
@@ -3,7 +3,14 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.isStyledComponents = exports.isStyled = exports.isKeyframes = exports.isImportedFrom = exports.isEmotion = exports.isCxFunction = exports.isCssMap = exports.isCss = exports.isCompiled = exports.getImportSources = exports.DEFAULT_IMPORT_SOURCES = exports.CSS_IN_JS_IMPORTS = void 0;
6
+ exports.isXcss = exports.isStyledComponents = exports.isStyled = exports.isKeyframes = exports.isImportedFrom = exports.isEmotion = exports.isCxFunction = exports.isCssMap = exports.isCss = exports.isCompiled = exports.getImportSources = exports.DEFAULT_IMPORT_SOURCES = exports.CSS_IN_JS_IMPORTS = void 0;
7
+ // This should be kept in sync with
8
+ // packages/design-system/eslint-plugin-ui-styling-standard/src/rules/utils/is-supported-import.tsx
9
+ // whenever possible.
10
+ //
11
+ // TODO: would having an @atlassian/eslint-plugin-design-system-common
12
+ // package be useful?
13
+
7
14
  // eslint-disable-next-line import/no-extraneous-dependencies
8
15
 
9
16
  var CSS_IN_JS_IMPORTS = exports.CSS_IN_JS_IMPORTS = {
@@ -118,10 +125,12 @@ var isCssMap = exports.isCssMap = isSupportedImportWrapper('cssMap');
118
125
  var isKeyframes = exports.isKeyframes = isSupportedImportWrapper('keyframes');
119
126
  // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
120
127
  var isStyled = exports.isStyled = isSupportedImportWrapper('styled', ['styled-components', '@emotion/styled']);
128
+ var isXcss = exports.isXcss = isSupportedImportWrapper('xcss');
121
129
  var isImportedFrom = exports.isImportedFrom = function isImportedFrom(moduleName) {
122
130
  var exactMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
123
- return function (nodeToCheck, referencesInScope, importSources) {
124
- if (!importSources.some(function (importSource) {
131
+ return function (nodeToCheck, referencesInScope) {
132
+ var importSources = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
133
+ if (importSources && !importSources.some(function (importSource) {
125
134
  return importSource === moduleName || !exactMatch && importSource.startsWith(moduleName);
126
135
  })) {
127
136
  // Don't go through the trouble of checking the import sources does not include this
@@ -24,6 +24,18 @@ const getProgramNode = expression => {
24
24
  }
25
25
  return expression.parent;
26
26
  };
27
+ const isDeclaredInsideComponent = expression => {
28
+ // These nodes imply that there is a distinct own scope (function scope / block scope),
29
+ // and so the presence of them means that expression was not defined in the module scope.
30
+ const NOT_MODULE_SCOPE = ['ArrowFunctionExpression', 'BlockStatement', 'ClassDeclaration', 'FunctionExpression'];
31
+ while (expression.type !== 'Program') {
32
+ if (NOT_MODULE_SCOPE.includes(expression.type)) {
33
+ return true;
34
+ }
35
+ expression = expression.parent;
36
+ }
37
+ return false;
38
+ };
27
39
  class JSXExpressionLinter {
28
40
  // File-level tracking of styles hoisted from the cssAtTopOfModule/cssAtBottomOfModule fixers.
29
41
 
@@ -92,13 +104,13 @@ class JSXExpressionLinter {
92
104
  });
93
105
  return;
94
106
  }
95
- if (identifier.parent.parent.parent.type !== 'Program') {
107
+ if (isDeclaredInsideComponent(identifier)) {
96
108
  // When variable is declared inside the component
97
109
  this.context.report({
98
110
  node: sourceIdentifier,
99
111
  messageId: this.configuration.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule',
100
112
  fix: fixer => {
101
- if (this.configuration.fixNamesOnly) {
113
+ if (!this.configuration.autoFix) {
102
114
  return [];
103
115
  }
104
116
  return this.fixCssNotInModuleScope(fixer, identifier, false);
@@ -120,7 +132,7 @@ class JSXExpressionLinter {
120
132
  node: identifier,
121
133
  messageId: 'cssObjectTypeOnly',
122
134
  fix: fixer => {
123
- if (this.configuration.fixNamesOnly) {
135
+ if (!this.configuration.autoFix) {
124
136
  return [];
125
137
  }
126
138
  return this.addCssFunctionCall(fixer, identifier.parent);
@@ -285,8 +297,9 @@ class JSXExpressionLinter {
285
297
  // The last value is the bottom of the file
286
298
  fixerNodePlacement = programNode.body[programNode.body.length - 1];
287
299
  } else {
300
+ var _ref;
288
301
  // Place after the last ImportDeclaration
289
- fixerNodePlacement = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(node => node.type !== 'ImportDeclaration');
302
+ fixerNodePlacement = (_ref = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(node => node.type !== 'ImportDeclaration')) !== null && _ref !== void 0 ? _ref : fixerNodePlacement;
290
303
  }
291
304
  let moduleString;
292
305
  let fixes = [];
@@ -363,7 +376,7 @@ class JSXExpressionLinter {
363
376
  node: expression,
364
377
  messageId: this.configuration.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule',
365
378
  fix: fixer => {
366
- if (this.configuration.fixNamesOnly) {
379
+ if (!this.configuration.autoFix) {
367
380
  return [];
368
381
  }
369
382
 
@@ -404,8 +417,7 @@ const defaultConfig = {
404
417
  cssImportSource: CSS_IN_JS_IMPORTS.compiled,
405
418
  xcssImportSource: CSS_IN_JS_IMPORTS.atlaskitPrimitives,
406
419
  excludeReactComponents: false,
407
- fixNamesOnly: false,
408
- autoFixNames: true
420
+ autoFix: true
409
421
  };
410
422
  const rule = createLintRule({
411
423
  meta: {
@@ -424,8 +436,7 @@ const rule = createLintRule({
424
436
  cssObjectTypeOnly: `Create styles using objects passed to a css function call, e.g. \`css({ textAlign: 'center'; })\`.`,
425
437
  cssInModule: `Imported styles should not be used; instead define in the module, import a component, or use a design token.`,
426
438
  cssArrayStylesOnly: `Compose styles with an array on the css prop instead of using object spread.`,
427
- noMemberExpressions: `Styles should be a regular variable (e.g. 'buttonStyles'), not a member of an object (e.g. 'myObject.styles').`,
428
- shouldEndInStyles: 'Declared styles should end in "styles".'
439
+ noMemberExpressions: `Styles should be a regular variable (e.g. 'buttonStyles'), not a member of an object (e.g. 'myObject.styles').`
429
440
  },
430
441
  schema: [{
431
442
  type: 'object',
@@ -449,10 +460,7 @@ const rule = createLintRule({
449
460
  excludeReactComponents: {
450
461
  type: 'boolean'
451
462
  },
452
- autoFixNames: {
453
- type: 'boolean'
454
- },
455
- fixNamesOnly: {
463
+ autoFix: {
456
464
  type: 'boolean'
457
465
  }
458
466
  },
@@ -461,42 +469,7 @@ const rule = createLintRule({
461
469
  },
462
470
  create(context) {
463
471
  const mergedConfig = assign({}, defaultConfig, context.options[0]);
464
- const declarationSuffix = 'Styles';
465
- const callSelectorFunctions = [...mergedConfig.cssFunctions, 'cssMap'];
466
- const callSelector = callSelectorFunctions.map(fn => `CallExpression[callee.name=${fn}]`).join(',');
467
472
  return {
468
- [callSelector]: node => {
469
- if (node.parent.type !== 'VariableDeclarator') {
470
- // We aren't interested in these that don't have a parent.
471
- return;
472
- }
473
- const identifier = node.parent.id;
474
- if (identifier.type === 'Identifier' && (identifier.name.endsWith(declarationSuffix) || identifier.name.startsWith(declarationSuffix.toLowerCase()) || identifier.name === declarationSuffix.toLowerCase())) {
475
- // Already prefixed! Nothing to do.
476
- return;
477
- }
478
- if (!mergedConfig.autoFixNames) {
479
- return;
480
- }
481
- context.report({
482
- node: identifier,
483
- messageId: 'shouldEndInStyles',
484
- fix: fixer => {
485
- var _context$getScope$var;
486
- const identifierName = identifier.type === 'Identifier' ? identifier.name : '';
487
- const references = ((_context$getScope$var = context.getScope().variables.find(varb => varb.name === identifierName)) === null || _context$getScope$var === void 0 ? void 0 : _context$getScope$var.references) || [];
488
- const newIdentifierName = `${identifierName
489
- // Remove "Style" if it is already defined.
490
- .replace(/Style$/, '')}${declarationSuffix}`;
491
- return references.filter(ref => ref.identifier.name === identifierName).map(ref => {
492
- if (ref.identifier.parent && ref.identifier.parent.shorthand) {
493
- return fixer.replaceText(ref.identifier, `${identifierName}: ${newIdentifierName}`);
494
- }
495
- return fixer.replaceText(ref.identifier, newIdentifierName);
496
- });
497
- }
498
- });
499
- },
500
473
  JSXAttribute(nodeOriginal) {
501
474
  const node = nodeOriginal;
502
475
  const {
@@ -1,3 +1,10 @@
1
+ // This should be kept in sync with
2
+ // packages/design-system/eslint-plugin-ui-styling-standard/src/rules/utils/is-supported-import.tsx
3
+ // whenever possible.
4
+ //
5
+ // TODO: would having an @atlassian/eslint-plugin-design-system-common
6
+ // package be useful?
7
+
1
8
  // eslint-disable-next-line import/no-extraneous-dependencies
2
9
 
3
10
  export const CSS_IN_JS_IMPORTS = {
@@ -109,8 +116,14 @@ export const isCssMap = isSupportedImportWrapper('cssMap');
109
116
  export const isKeyframes = isSupportedImportWrapper('keyframes');
110
117
  // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
111
118
  export const isStyled = isSupportedImportWrapper('styled', ['styled-components', '@emotion/styled']);
112
- export const isImportedFrom = (moduleName, exactMatch = true) => (nodeToCheck, referencesInScope, importSources) => {
113
- if (!importSources.some(importSource => importSource === moduleName || !exactMatch && importSource.startsWith(moduleName))) {
119
+ export const isXcss = isSupportedImportWrapper('xcss');
120
+ export const isImportedFrom = (moduleName, exactMatch = true) => (nodeToCheck, referencesInScope,
121
+ /**
122
+ * If we strictly have specific import sources in the config scope, pass them to make this more performant.
123
+ * Pass `null` if you don't care if its configured or not.
124
+ */
125
+ importSources = null) => {
126
+ if (importSources && !importSources.some(importSource => importSource === moduleName || !exactMatch && importSource.startsWith(moduleName))) {
114
127
  // Don't go through the trouble of checking the import sources does not include this
115
128
  // We'll assume this is skipped elsewhere.
116
129
  return false;
@@ -1,4 +1,3 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
1
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
2
  import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
3
  import _createClass from "@babel/runtime/helpers/createClass";
@@ -33,6 +32,18 @@ var getProgramNode = function getProgramNode(expression) {
33
32
  }
34
33
  return expression.parent;
35
34
  };
35
+ var isDeclaredInsideComponent = function isDeclaredInsideComponent(expression) {
36
+ // These nodes imply that there is a distinct own scope (function scope / block scope),
37
+ // and so the presence of them means that expression was not defined in the module scope.
38
+ var NOT_MODULE_SCOPE = ['ArrowFunctionExpression', 'BlockStatement', 'ClassDeclaration', 'FunctionExpression'];
39
+ while (expression.type !== 'Program') {
40
+ if (NOT_MODULE_SCOPE.includes(expression.type)) {
41
+ return true;
42
+ }
43
+ expression = expression.parent;
44
+ }
45
+ return false;
46
+ };
36
47
  var JSXExpressionLinter = /*#__PURE__*/function () {
37
48
  // File-level tracking of styles hoisted from the cssAtTopOfModule/cssAtBottomOfModule fixers.
38
49
 
@@ -112,13 +123,13 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
112
123
  });
113
124
  return;
114
125
  }
115
- if (identifier.parent.parent.parent.type !== 'Program') {
126
+ if (isDeclaredInsideComponent(identifier)) {
116
127
  // When variable is declared inside the component
117
128
  this.context.report({
118
129
  node: sourceIdentifier,
119
130
  messageId: this.configuration.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule',
120
131
  fix: function fix(fixer) {
121
- if (_this.configuration.fixNamesOnly) {
132
+ if (!_this.configuration.autoFix) {
122
133
  return [];
123
134
  }
124
135
  return _this.fixCssNotInModuleScope(fixer, identifier, false);
@@ -140,7 +151,7 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
140
151
  node: identifier,
141
152
  messageId: 'cssObjectTypeOnly',
142
153
  fix: function fix(fixer) {
143
- if (_this.configuration.fixNamesOnly) {
154
+ if (!_this.configuration.autoFix) {
144
155
  return [];
145
156
  }
146
157
  return _this.addCssFunctionCall(fixer, identifier.parent);
@@ -316,10 +327,11 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
316
327
  // The last value is the bottom of the file
317
328
  fixerNodePlacement = programNode.body[programNode.body.length - 1];
318
329
  } else {
330
+ var _ref3;
319
331
  // Place after the last ImportDeclaration
320
- fixerNodePlacement = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(function (node) {
332
+ fixerNodePlacement = (_ref3 = programNode.body.length === 1 ? programNode.body[0] : programNode.body.find(function (node) {
321
333
  return node.type !== 'ImportDeclaration';
322
- });
334
+ })) !== null && _ref3 !== void 0 ? _ref3 : fixerNodePlacement;
323
335
  }
324
336
  var moduleString;
325
337
  var fixes = [];
@@ -403,7 +415,7 @@ var JSXExpressionLinter = /*#__PURE__*/function () {
403
415
  node: expression,
404
416
  messageId: this.configuration.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule',
405
417
  fix: function fix(fixer) {
406
- if (_this3.configuration.fixNamesOnly) {
418
+ if (!_this3.configuration.autoFix) {
407
419
  return [];
408
420
  }
409
421
 
@@ -448,8 +460,7 @@ var defaultConfig = {
448
460
  cssImportSource: CSS_IN_JS_IMPORTS.compiled,
449
461
  xcssImportSource: CSS_IN_JS_IMPORTS.atlaskitPrimitives,
450
462
  excludeReactComponents: false,
451
- fixNamesOnly: false,
452
- autoFixNames: true
463
+ autoFix: true
453
464
  };
454
465
  var rule = createLintRule({
455
466
  meta: {
@@ -468,8 +479,7 @@ var rule = createLintRule({
468
479
  cssObjectTypeOnly: "Create styles using objects passed to a css function call, e.g. `css({ textAlign: 'center'; })`.",
469
480
  cssInModule: "Imported styles should not be used; instead define in the module, import a component, or use a design token.",
470
481
  cssArrayStylesOnly: "Compose styles with an array on the css prop instead of using object spread.",
471
- noMemberExpressions: "Styles should be a regular variable (e.g. 'buttonStyles'), not a member of an object (e.g. 'myObject.styles').",
472
- shouldEndInStyles: 'Declared styles should end in "styles".'
482
+ noMemberExpressions: "Styles should be a regular variable (e.g. 'buttonStyles'), not a member of an object (e.g. 'myObject.styles')."
473
483
  },
474
484
  schema: [{
475
485
  type: 'object',
@@ -493,10 +503,7 @@ var rule = createLintRule({
493
503
  excludeReactComponents: {
494
504
  type: 'boolean'
495
505
  },
496
- autoFixNames: {
497
- type: 'boolean'
498
- },
499
- fixNamesOnly: {
506
+ autoFix: {
500
507
  type: 'boolean'
501
508
  }
502
509
  },
@@ -504,80 +511,41 @@ var rule = createLintRule({
504
511
  }]
505
512
  },
506
513
  create: function create(context) {
507
- var _ref3;
508
514
  var mergedConfig = assign({}, defaultConfig, context.options[0]);
509
- var declarationSuffix = 'Styles';
510
- var callSelectorFunctions = [].concat(_toConsumableArray(mergedConfig.cssFunctions), ['cssMap']);
511
- var callSelector = callSelectorFunctions.map(function (fn) {
512
- return "CallExpression[callee.name=".concat(fn, "]");
513
- }).join(',');
514
- return _ref3 = {}, _defineProperty(_ref3, callSelector, function (node) {
515
- if (node.parent.type !== 'VariableDeclarator') {
516
- // We aren't interested in these that don't have a parent.
517
- return;
518
- }
519
- var identifier = node.parent.id;
520
- if (identifier.type === 'Identifier' && (identifier.name.endsWith(declarationSuffix) || identifier.name.startsWith(declarationSuffix.toLowerCase()) || identifier.name === declarationSuffix.toLowerCase())) {
521
- // Already prefixed! Nothing to do.
522
- return;
523
- }
524
- if (!mergedConfig.autoFixNames) {
525
- return;
526
- }
527
- context.report({
528
- node: identifier,
529
- messageId: 'shouldEndInStyles',
530
- fix: function fix(fixer) {
531
- var _context$getScope$var;
532
- var identifierName = identifier.type === 'Identifier' ? identifier.name : '';
533
- var references = ((_context$getScope$var = context.getScope().variables.find(function (varb) {
534
- return varb.name === identifierName;
535
- })) === null || _context$getScope$var === void 0 ? void 0 : _context$getScope$var.references) || [];
536
- var newIdentifierName = "".concat(identifierName
537
- // Remove "Style" if it is already defined.
538
- .replace(/Style$/, '')).concat(declarationSuffix);
539
- return references.filter(function (ref) {
540
- return ref.identifier.name === identifierName;
541
- }).map(function (ref) {
542
- if (ref.identifier.parent && ref.identifier.parent.shorthand) {
543
- return fixer.replaceText(ref.identifier, "".concat(identifierName, ": ").concat(newIdentifierName));
544
- }
545
- return fixer.replaceText(ref.identifier, newIdentifierName);
546
- });
547
- }
548
- });
549
- }), _defineProperty(_ref3, "JSXAttribute", function JSXAttribute(nodeOriginal) {
550
- var node = nodeOriginal;
551
- var name = node.name,
552
- value = node.value;
553
- if (mergedConfig.excludeReactComponents && node.parent.type === 'JSXOpeningElement') {
554
- // e.g. <item.before />
555
- if (node.parent.name.type === 'JSXMemberExpression') {
556
- return;
557
- }
558
- // e.g. <div />, <MenuItem />
559
- if (node.parent.name.type === 'JSXIdentifier' && !isDOMElementName(node.parent.name.name)) {
560
- return;
561
- }
562
- }
563
- if (name.type === 'JSXIdentifier' && mergedConfig.cssFunctions.includes(name.name)) {
564
- // When not a jsx expression. For eg. css=""
565
- if ((value === null || value === void 0 ? void 0 : value.type) !== 'JSXExpressionContainer') {
566
- context.report({
567
- node: node,
568
- messageId: mergedConfig.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule'
569
- });
570
- return;
515
+ return {
516
+ JSXAttribute: function JSXAttribute(nodeOriginal) {
517
+ var node = nodeOriginal;
518
+ var name = node.name,
519
+ value = node.value;
520
+ if (mergedConfig.excludeReactComponents && node.parent.type === 'JSXOpeningElement') {
521
+ // e.g. <item.before />
522
+ if (node.parent.name.type === 'JSXMemberExpression') {
523
+ return;
524
+ }
525
+ // e.g. <div />, <MenuItem />
526
+ if (node.parent.name.type === 'JSXIdentifier' && !isDOMElementName(node.parent.name.name)) {
527
+ return;
528
+ }
571
529
  }
572
- if (value.expression.type === 'JSXEmptyExpression') {
573
- // e.g. the comment in
574
- // <div css={/* Hello there */} />
575
- return;
530
+ if (name.type === 'JSXIdentifier' && mergedConfig.cssFunctions.includes(name.name)) {
531
+ // When not a jsx expression. For eg. css=""
532
+ if ((value === null || value === void 0 ? void 0 : value.type) !== 'JSXExpressionContainer') {
533
+ context.report({
534
+ node: node,
535
+ messageId: mergedConfig.stylesPlacement === 'bottom' ? 'cssAtBottomOfModule' : 'cssAtTopOfModule'
536
+ });
537
+ return;
538
+ }
539
+ if (value.expression.type === 'JSXEmptyExpression') {
540
+ // e.g. the comment in
541
+ // <div css={/* Hello there */} />
542
+ return;
543
+ }
544
+ var linter = new JSXExpressionLinter(context, name.name, mergedConfig, value.expression);
545
+ linter.run();
576
546
  }
577
- var linter = new JSXExpressionLinter(context, name.name, mergedConfig, value.expression);
578
- linter.run();
579
547
  }
580
- }), _ref3;
548
+ };
581
549
  }
582
550
  });
583
551
  export default rule;
@@ -1,3 +1,10 @@
1
+ // This should be kept in sync with
2
+ // packages/design-system/eslint-plugin-ui-styling-standard/src/rules/utils/is-supported-import.tsx
3
+ // whenever possible.
4
+ //
5
+ // TODO: would having an @atlassian/eslint-plugin-design-system-common
6
+ // package be useful?
7
+
1
8
  // eslint-disable-next-line import/no-extraneous-dependencies
2
9
 
3
10
  export var CSS_IN_JS_IMPORTS = {
@@ -112,10 +119,12 @@ export var isCssMap = isSupportedImportWrapper('cssMap');
112
119
  export var isKeyframes = isSupportedImportWrapper('keyframes');
113
120
  // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
114
121
  export var isStyled = isSupportedImportWrapper('styled', ['styled-components', '@emotion/styled']);
122
+ export var isXcss = isSupportedImportWrapper('xcss');
115
123
  export var isImportedFrom = function isImportedFrom(moduleName) {
116
124
  var exactMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
117
- return function (nodeToCheck, referencesInScope, importSources) {
118
- if (!importSources.some(function (importSource) {
125
+ return function (nodeToCheck, referencesInScope) {
126
+ var importSources = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
127
+ if (importSources && !importSources.some(function (importSource) {
119
128
  return importSource === moduleName || !exactMatch && importSource.startsWith(moduleName);
120
129
  })) {
121
130
  // Don't go through the trouble of checking the import sources does not include this
@@ -5,6 +5,5 @@ export type RuleConfig = {
5
5
  cssImportSource: ImportSource;
6
6
  xcssImportSource: ImportSource;
7
7
  excludeReactComponents: boolean;
8
- autoFixNames: boolean;
9
- fixNamesOnly: boolean;
8
+ autoFix: boolean;
10
9
  };
@@ -31,7 +31,8 @@ export declare const isCxFunction: SupportedNameChecker;
31
31
  export declare const isCssMap: SupportedNameChecker;
32
32
  export declare const isKeyframes: SupportedNameChecker;
33
33
  export declare const isStyled: SupportedNameChecker;
34
- export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
34
+ export declare const isXcss: SupportedNameChecker;
35
+ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
35
36
  /**
36
37
  * Determine if this node is specifically from a `'styled-components'` import.
37
38
  * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
@@ -39,7 +40,7 @@ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean)
39
40
  *
40
41
  * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
41
42
  */
42
- export declare const isStyledComponents: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
43
- export declare const isCompiled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
44
- export declare const isEmotion: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
43
+ export declare const isStyledComponents: (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
44
+ export declare const isCompiled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
45
+ export declare const isEmotion: (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
45
46
  export {};
@@ -5,6 +5,5 @@ export type RuleConfig = {
5
5
  cssImportSource: ImportSource;
6
6
  xcssImportSource: ImportSource;
7
7
  excludeReactComponents: boolean;
8
- autoFixNames: boolean;
9
- fixNamesOnly: boolean;
8
+ autoFix: boolean;
10
9
  };
@@ -31,7 +31,8 @@ export declare const isCxFunction: SupportedNameChecker;
31
31
  export declare const isCssMap: SupportedNameChecker;
32
32
  export declare const isKeyframes: SupportedNameChecker;
33
33
  export declare const isStyled: SupportedNameChecker;
34
- export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
34
+ export declare const isXcss: SupportedNameChecker;
35
+ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
35
36
  /**
36
37
  * Determine if this node is specifically from a `'styled-components'` import.
37
38
  * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
@@ -39,7 +40,7 @@ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean)
39
40
  *
40
41
  * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
41
42
  */
42
- export declare const isStyledComponents: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
43
- export declare const isCompiled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
44
- export declare const isEmotion: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
43
+ export declare const isStyledComponents: (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
44
+ export declare const isCompiled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
45
+ export declare const isEmotion: (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
45
46
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-design-system",
3
3
  "description": "The essential plugin for use with the Atlassian Design System.",
4
- "version": "8.38.0",
4
+ "version": "9.0.0",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
7
7
  "publishConfig": {
@@ -54,7 +54,7 @@
54
54
  "@atlassian/eslint-utils": "^0.4.0",
55
55
  "@emotion/react": "^11.7.1",
56
56
  "@emotion/styled": "^11.0.0",
57
- "@types/eslint": "^8.4.5",
57
+ "@types/eslint": "^8.56.6",
58
58
  "@types/estraverse": "^5.1.7",
59
59
  "eslint": "^8.49.0",
60
60
  "jscodeshift": "^0.13.0",