@atlaskit/eslint-plugin-ui-styling-standard 0.6.2 → 0.7.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,24 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 0.7.0
4
+
5
+ ### Minor 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
+
16
+ ## 0.6.3
17
+
18
+ ### Patch Changes
19
+
20
+ - [#83463](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/83463) [`fee29cf1335a`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/fee29cf1335a) - Extend convert-props-syntax to Emotion as well as it has a type issue.
21
+
3
22
  ## 0.6.2
4
23
 
5
24
  ### Patch Changes
@@ -1,8 +1,8 @@
1
1
  # convert-props-syntax
2
2
 
3
- Convert props syntax that is unsupported by `styled-components` <4.x to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by `styled-components`.
3
+ Convert props syntax that are unsupported by `styled-components@<4` and `@emotion/styled` to a props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax that results in a type error.
4
4
 
5
- `styled-components` <4.x does not support having an arrow function inside a CSS value:
5
+ These libraries do not support having an arrow function inside a CSS value:
6
6
 
7
7
  ```js
8
8
  styled.div({
@@ -20,7 +20,7 @@ styled.div((props) => ({
20
20
  }));
21
21
  ```
22
22
 
23
- This ESLint rule will only run on usages of the `styled` API when imported from `styled-components`.
23
+ This ESLint rule will only run on usages of the `styled` API when imported from libraries that are known to have this problem.
24
24
 
25
25
  ## About the autofixer
26
26
 
@@ -98,7 +98,7 @@ styled.div({
98
98
  ### Correct
99
99
 
100
100
  ```js
101
- import styled from 'styled-components';
101
+ import styled from '@emotion/styled';
102
102
 
103
103
  styled.div((props) => ({
104
104
  color: props.myColor,
@@ -112,4 +112,4 @@ styled.div((props) => ({
112
112
 
113
113
  ## When not to use it
114
114
 
115
- You do not need this rule if you are not using a codebase that has an old version of `styled-components` (<4.x).
115
+ You do not need this rule if you are not using a codebase that has a violating library as it will never do anything.
@@ -12,8 +12,6 @@ var _estraverse = _interopRequireDefault(require("estraverse"));
12
12
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
13
13
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
14
14
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
15
- var IMPORT_SOURCES = [_isSupportedImport.CSS_IN_JS_IMPORTS.styledComponents];
16
-
17
15
  /**
18
16
  * Represents some information about single line that contains some props, e.g.
19
17
  * the arrow function in
@@ -243,7 +241,7 @@ var lintArguments = function lintArguments(context, args) {
243
241
  })) {
244
242
  context.report({
245
243
  node: argument,
246
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer'
244
+ messageId: 'unsupported-prop-syntax-no-autofixer'
247
245
  });
248
246
  return {
249
247
  v: void 0
@@ -251,7 +249,7 @@ var lintArguments = function lintArguments(context, args) {
251
249
  }
252
250
  context.report({
253
251
  node: argument,
254
- messageId: 'unsupported-styled-components-prop-syntax',
252
+ messageId: 'unsupported-prop-syntax',
255
253
  fix: function fix(fixer) {
256
254
  var sourceCode = context.getSourceCode();
257
255
  return fixPropsObjectUsages(fixer, sourceCode, argument,
@@ -369,14 +367,14 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
369
367
  meta: {
370
368
  name: 'convert-props-syntax',
371
369
  docs: {
372
- description: 'Convert props syntax that is unsupported by `styled-components` <4.x to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by `styled-components`.',
370
+ description: 'Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries.',
373
371
  recommended: true,
374
372
  severity: 'error'
375
373
  },
376
374
  fixable: 'code',
377
375
  messages: {
378
- 'unsupported-styled-components-prop-syntax': 'This syntax is not supported by styled-components <4.x and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or use the autofixer to switch the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.',
379
- 'unsupported-styled-components-prop-syntax-no-autofixer': 'This syntax is not supported by styled-components <4.x and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or manually migrate the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.\n\nCheck out the rule documentation for examples.'
376
+ 'unsupported-prop-syntax': 'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or use the autofixer to switch the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.',
377
+ 'unsupported-prop-syntax-no-autofixer': 'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or manually migrate the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.\n\nCheck out the rule documentation for examples.'
380
378
  },
381
379
  type: 'problem'
382
380
  },
@@ -385,7 +383,7 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
385
383
  'CallExpression[callee.type="MemberExpression"]': function CallExpressionCalleeTypeMemberExpression(node) {
386
384
  var references = context.getScope().references;
387
385
  var callee = node.callee.type === 'MemberExpression' ? node.callee.object : node.callee;
388
- if (!(0, _isSupportedImport.isStyledComponents)(callee, references, IMPORT_SOURCES)) {
386
+ if (!(0, _isSupportedImport.isStyledComponents)(callee, references) && !(0, _isSupportedImport.isEmotion)(callee, references)) {
389
387
  return;
390
388
  }
391
389
  lintArguments(context, node.arguments);
@@ -128,8 +128,11 @@ var isStyled = exports.isStyled = isSupportedImportWrapper('styled', ['styled-co
128
128
  var isXcss = exports.isXcss = isSupportedImportWrapper('xcss');
129
129
  var isImportedFrom = exports.isImportedFrom = function isImportedFrom(moduleName) {
130
130
  var exactMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
131
- return function (nodeToCheck, referencesInScope, importSources) {
132
- if (!importSources.includes(moduleName)) {
131
+ return function (nodeToCheck, referencesInScope) {
132
+ var importSources = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
133
+ if (importSources && !importSources.some(function (importSource) {
134
+ return importSource === moduleName || !exactMatch && importSource.startsWith(moduleName);
135
+ })) {
133
136
  // Don't go through the trouble of checking the import sources does not include this
134
137
  // We'll assume this is skipped elsewhere.
135
138
  return false;
@@ -1,8 +1,7 @@
1
1
  import { analyze } from 'eslint-scope';
2
2
  import { createLintRule } from '../utils/create-rule';
3
- import { CSS_IN_JS_IMPORTS, isStyledComponents } from '../utils/is-supported-import';
3
+ import { isStyledComponents, isEmotion } from '../utils/is-supported-import';
4
4
  import ESTraverse from 'estraverse';
5
- const IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.styledComponents];
6
5
 
7
6
  /**
8
7
  * Represents some information about single line that contains some props, e.g.
@@ -179,13 +178,13 @@ const lintArguments = (context, args) => {
179
178
  if (allProps.some(propUsage => !propUsage.valid)) {
180
179
  context.report({
181
180
  node: argument,
182
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer'
181
+ messageId: 'unsupported-prop-syntax-no-autofixer'
183
182
  });
184
183
  return;
185
184
  }
186
185
  context.report({
187
186
  node: argument,
188
- messageId: 'unsupported-styled-components-prop-syntax',
187
+ messageId: 'unsupported-prop-syntax',
189
188
  fix(fixer) {
190
189
  const sourceCode = context.getSourceCode();
191
190
  return fixPropsObjectUsages(fixer, sourceCode, argument,
@@ -264,14 +263,14 @@ export const rule = createLintRule({
264
263
  meta: {
265
264
  name: 'convert-props-syntax',
266
265
  docs: {
267
- description: 'Convert props syntax that is unsupported by `styled-components` <4.x to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by `styled-components`.',
266
+ description: 'Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries.',
268
267
  recommended: true,
269
268
  severity: 'error'
270
269
  },
271
270
  fixable: 'code',
272
271
  messages: {
273
- 'unsupported-styled-components-prop-syntax': 'This syntax is not supported by styled-components <4.x and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or use the autofixer to switch the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.',
274
- 'unsupported-styled-components-prop-syntax-no-autofixer': 'This syntax is not supported by styled-components <4.x and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or manually migrate the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.\n\nCheck out the rule documentation for examples.'
272
+ 'unsupported-prop-syntax': 'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or use the autofixer to switch the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.',
273
+ 'unsupported-prop-syntax-no-autofixer': 'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or manually migrate the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.\n\nCheck out the rule documentation for examples.'
275
274
  },
276
275
  type: 'problem'
277
276
  },
@@ -280,7 +279,7 @@ export const rule = createLintRule({
280
279
  'CallExpression[callee.type="MemberExpression"]': node => {
281
280
  const references = context.getScope().references;
282
281
  const callee = node.callee.type === 'MemberExpression' ? node.callee.object : node.callee;
283
- if (!isStyledComponents(callee, references, IMPORT_SOURCES)) {
282
+ if (!isStyledComponents(callee, references) && !isEmotion(callee, references)) {
284
283
  return;
285
284
  }
286
285
  lintArguments(context, node.arguments);
@@ -117,8 +117,13 @@ export const isKeyframes = isSupportedImportWrapper('keyframes');
117
117
  // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
118
118
  export const isStyled = isSupportedImportWrapper('styled', ['styled-components', '@emotion/styled']);
119
119
  export const isXcss = isSupportedImportWrapper('xcss');
120
- export const isImportedFrom = (moduleName, exactMatch = true) => (nodeToCheck, referencesInScope, importSources) => {
121
- if (!importSources.includes(moduleName)) {
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))) {
122
127
  // Don't go through the trouble of checking the import sources does not include this
123
128
  // We'll assume this is skipped elsewhere.
124
129
  return false;
@@ -3,9 +3,8 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
3
3
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
4
4
  import { analyze } from 'eslint-scope';
5
5
  import { createLintRule } from '../utils/create-rule';
6
- import { CSS_IN_JS_IMPORTS, isStyledComponents } from '../utils/is-supported-import';
6
+ import { isStyledComponents, isEmotion } from '../utils/is-supported-import';
7
7
  import ESTraverse from 'estraverse';
8
- var IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.styledComponents];
9
8
 
10
9
  /**
11
10
  * Represents some information about single line that contains some props, e.g.
@@ -236,7 +235,7 @@ var lintArguments = function lintArguments(context, args) {
236
235
  })) {
237
236
  context.report({
238
237
  node: argument,
239
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer'
238
+ messageId: 'unsupported-prop-syntax-no-autofixer'
240
239
  });
241
240
  return {
242
241
  v: void 0
@@ -244,7 +243,7 @@ var lintArguments = function lintArguments(context, args) {
244
243
  }
245
244
  context.report({
246
245
  node: argument,
247
- messageId: 'unsupported-styled-components-prop-syntax',
246
+ messageId: 'unsupported-prop-syntax',
248
247
  fix: function fix(fixer) {
249
248
  var sourceCode = context.getSourceCode();
250
249
  return fixPropsObjectUsages(fixer, sourceCode, argument,
@@ -362,14 +361,14 @@ export var rule = createLintRule({
362
361
  meta: {
363
362
  name: 'convert-props-syntax',
364
363
  docs: {
365
- description: 'Convert props syntax that is unsupported by `styled-components` <4.x to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by `styled-components`.',
364
+ description: 'Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries.',
366
365
  recommended: true,
367
366
  severity: 'error'
368
367
  },
369
368
  fixable: 'code',
370
369
  messages: {
371
- 'unsupported-styled-components-prop-syntax': 'This syntax is not supported by styled-components <4.x and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or use the autofixer to switch the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.',
372
- 'unsupported-styled-components-prop-syntax-no-autofixer': 'This syntax is not supported by styled-components <4.x and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or manually migrate the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.\n\nCheck out the rule documentation for examples.'
370
+ 'unsupported-prop-syntax': 'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or use the autofixer to switch the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.',
371
+ 'unsupported-prop-syntax-no-autofixer': 'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or manually migrate the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.\n\nCheck out the rule documentation for examples.'
373
372
  },
374
373
  type: 'problem'
375
374
  },
@@ -378,7 +377,7 @@ export var rule = createLintRule({
378
377
  'CallExpression[callee.type="MemberExpression"]': function CallExpressionCalleeTypeMemberExpression(node) {
379
378
  var references = context.getScope().references;
380
379
  var callee = node.callee.type === 'MemberExpression' ? node.callee.object : node.callee;
381
- if (!isStyledComponents(callee, references, IMPORT_SOURCES)) {
380
+ if (!isStyledComponents(callee, references) && !isEmotion(callee, references)) {
382
381
  return;
383
382
  }
384
383
  lintArguments(context, node.arguments);
@@ -122,8 +122,11 @@ export var isStyled = isSupportedImportWrapper('styled', ['styled-components', '
122
122
  export var isXcss = isSupportedImportWrapper('xcss');
123
123
  export var isImportedFrom = function isImportedFrom(moduleName) {
124
124
  var exactMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
125
- return function (nodeToCheck, referencesInScope, importSources) {
126
- if (!importSources.includes(moduleName)) {
125
+ return function (nodeToCheck, referencesInScope) {
126
+ var importSources = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
127
+ if (importSources && !importSources.some(function (importSource) {
128
+ return importSource === moduleName || !exactMatch && importSource.startsWith(moduleName);
129
+ })) {
127
130
  // Don't go through the trouble of checking the import sources does not include this
128
131
  // We'll assume this is skipped elsewhere.
129
132
  return false;
@@ -32,7 +32,7 @@ export declare const isCssMap: SupportedNameChecker;
32
32
  export declare const isKeyframes: SupportedNameChecker;
33
33
  export declare const isStyled: SupportedNameChecker;
34
34
  export declare const isXcss: SupportedNameChecker;
35
- export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
35
+ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
36
36
  /**
37
37
  * Determine if this node is specifically from a `'styled-components'` import.
38
38
  * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
@@ -40,7 +40,7 @@ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean)
40
40
  *
41
41
  * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
42
42
  */
43
- export declare const isStyledComponents: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
44
- export declare const isCompiled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
45
- 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;
46
46
  export {};
@@ -32,7 +32,7 @@ export declare const isCssMap: SupportedNameChecker;
32
32
  export declare const isKeyframes: SupportedNameChecker;
33
33
  export declare const isStyled: SupportedNameChecker;
34
34
  export declare const isXcss: SupportedNameChecker;
35
- export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
35
+ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources?: ImportSource[] | null) => boolean;
36
36
  /**
37
37
  * Determine if this node is specifically from a `'styled-components'` import.
38
38
  * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
@@ -40,7 +40,7 @@ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean)
40
40
  *
41
41
  * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
42
42
  */
43
- export declare const isStyledComponents: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
44
- export declare const isCompiled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
45
- 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;
46
46
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-ui-styling-standard",
3
- "version": "0.6.2",
3
+ "version": "0.7.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",
@@ -44,11 +44,11 @@
44
44
  },
45
45
  "devDependencies": {
46
46
  "@atlaskit/ds-lib": "^2.2.0",
47
- "@atlaskit/eslint-plugin-design-system": "^8.37.0",
47
+ "@atlaskit/eslint-plugin-design-system": "^9.0.0",
48
48
  "@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
49
49
  "@atlassian/codegen": "*",
50
50
  "@compiled/eslint-plugin": "^0.13.6",
51
- "@types/eslint": "^8.4.5",
51
+ "@types/eslint": "^8.56.6",
52
52
  "@types/estraverse": "^5.1.7",
53
53
  "eslint": "^8.49.0",
54
54
  "lodash": "^4.17.21",
@@ -1,6 +1,6 @@
1
- Convert props syntax that is unsupported by `styled-components` <4.x to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by `styled-components`.
1
+ Convert props syntax that are unsupported by `styled-components@<4` and `@emotion/styled` to a props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax that results in a type error.
2
2
 
3
- `styled-components` <4.x does not support having an arrow function inside a CSS value:
3
+ These libraries do not support having an arrow function inside a CSS value:
4
4
 
5
5
  ```js
6
6
  styled.div({
@@ -18,7 +18,7 @@ styled.div((props) => ({
18
18
  }));
19
19
  ```
20
20
 
21
- This ESLint rule will only run on usages of the `styled` API when imported from `styled-components`.
21
+ This ESLint rule will only run on usages of the `styled` API when imported from libraries that are known to have this problem.
22
22
 
23
23
  ## About the autofixer
24
24
 
@@ -96,7 +96,7 @@ styled.div({
96
96
  ### Correct
97
97
 
98
98
  ```js
99
- import styled from 'styled-components';
99
+ import styled from '@emotion/styled';
100
100
 
101
101
  styled.div((props) => ({
102
102
  color: props.myColor,
@@ -110,4 +110,4 @@ styled.div((props) => ({
110
110
 
111
111
  ## When not to use it
112
112
 
113
- You do not need this rule if you are not using a codebase that has an old version of `styled-components` (<4.x).
113
+ You do not need this rule if you are not using a codebase that has a violating library as it will never do anything.
@@ -22,7 +22,7 @@ typescriptEslintTester.run(
22
22
  `,
23
23
  errors: [
24
24
  {
25
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
25
+ messageId: 'unsupported-prop-syntax-no-autofixer',
26
26
  },
27
27
  ],
28
28
  },
@@ -37,7 +37,7 @@ typescriptEslintTester.run(
37
37
  `,
38
38
  errors: [
39
39
  {
40
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
40
+ messageId: 'unsupported-prop-syntax-no-autofixer',
41
41
  },
42
42
  ],
43
43
  },
@@ -52,7 +52,7 @@ typescriptEslintTester.run(
52
52
  `,
53
53
  errors: [
54
54
  {
55
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
55
+ messageId: 'unsupported-prop-syntax-no-autofixer',
56
56
  },
57
57
  ],
58
58
  },
@@ -67,7 +67,7 @@ typescriptEslintTester.run(
67
67
  `,
68
68
  errors: [
69
69
  {
70
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
70
+ messageId: 'unsupported-prop-syntax-no-autofixer',
71
71
  },
72
72
  ],
73
73
  },
@@ -17,6 +17,16 @@ typescriptEslintTester.run(
17
17
  }));
18
18
  `,
19
19
  },
20
+ {
21
+ name: 'ignores @emotion/styled usages using supported props syntax',
22
+ code: `
23
+ import styled from '@emotion/styled';
24
+
25
+ styled.div((props) => ({
26
+ color: props.color,
27
+ }));
28
+ `,
29
+ },
20
30
  {
21
31
  name: 'ignores styled-components usages already using supported props syntax (with logical operator)',
22
32
  code: `
@@ -95,6 +105,26 @@ typescriptEslintTester.run(
95
105
  },
96
106
  ],
97
107
  invalid: [
108
+ {
109
+ name: 'converts @emotion/styled props argument',
110
+ code: `
111
+ import styled from '@emotion/styled';
112
+
113
+ styled.div({
114
+ color: (props) => props.color,
115
+ backgroundColor: (props) => props.backgroundColor,
116
+ });
117
+ `,
118
+ output: `
119
+ import styled from '@emotion/styled';
120
+
121
+ styled.div((props) => ({
122
+ color: props.color,
123
+ backgroundColor: props.backgroundColor,
124
+ }));
125
+ `,
126
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
127
+ },
98
128
  {
99
129
  name: 'converts styled-components props argument',
100
130
  code: `
@@ -113,7 +143,7 @@ typescriptEslintTester.run(
113
143
  backgroundColor: props.backgroundColor,
114
144
  }));
115
145
  `,
116
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
146
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
117
147
  },
118
148
  {
119
149
  name: 'converts styled-components destructured props',
@@ -133,7 +163,7 @@ typescriptEslintTester.run(
133
163
  backgroundColor: props.backgroundColor,
134
164
  }));
135
165
  `,
136
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
166
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
137
167
  },
138
168
  {
139
169
  name: 'converts mixture of props argument and destructured props',
@@ -153,7 +183,7 @@ typescriptEslintTester.run(
153
183
  backgroundColor: props.backgroundColor,
154
184
  }));
155
185
  `,
156
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
186
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
157
187
  },
158
188
  {
159
189
  name: 'converts mix of valid props syntax and invalid props syntax',
@@ -183,7 +213,7 @@ typescriptEslintTester.run(
183
213
  })
184
214
  );
185
215
  `,
186
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
216
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
187
217
  },
188
218
  {
189
219
  name: 'converts mix of renamed props argument and destructured props',
@@ -205,7 +235,7 @@ typescriptEslintTester.run(
205
235
  height: props.customHeight,
206
236
  }));
207
237
  `,
208
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
238
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
209
239
  },
210
240
  {
211
241
  name: 'converts props with same name',
@@ -227,7 +257,7 @@ typescriptEslintTester.run(
227
257
  height: props.customHeight,
228
258
  }));
229
259
  `,
230
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
260
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
231
261
  },
232
262
  {
233
263
  name: 'converts props with same name (variant two)',
@@ -249,7 +279,7 @@ typescriptEslintTester.run(
249
279
  height: props.customHeight,
250
280
  }));
251
281
  `,
252
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
282
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
253
283
  },
254
284
  {
255
285
  name: 'converts props with same name (variant three)',
@@ -271,7 +301,7 @@ typescriptEslintTester.run(
271
301
  height: props.customHeight,
272
302
  }));
273
303
  `,
274
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
304
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
275
305
  },
276
306
  {
277
307
  name: 'converts props inside template literal',
@@ -291,7 +321,7 @@ typescriptEslintTester.run(
291
321
  backgroundSize: 'cover',
292
322
  }));
293
323
  `,
294
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
324
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
295
325
  },
296
326
  {
297
327
  name: 'converts props with complicated expressions',
@@ -313,7 +343,7 @@ typescriptEslintTester.run(
313
343
  padding: \`\${props.width}px \${props.width + 3}px \${props.width + 2}px\`,
314
344
  }));
315
345
  `,
316
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
346
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
317
347
  },
318
348
  {
319
349
  name: 'converts props with overlapping names',
@@ -333,7 +363,7 @@ typescriptEslintTester.run(
333
363
  width: props.height + props.oldHeight,
334
364
  }));
335
365
  `,
336
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
366
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
337
367
  },
338
368
  {
339
369
  name: 'converts props mixed with token function',
@@ -353,7 +383,7 @@ typescriptEslintTester.run(
353
383
  padding: \`\${token('some-token', someFallback)} \${props.myPadding}\`,
354
384
  }));
355
385
  `,
356
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
386
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
357
387
  },
358
388
  {
359
389
  name: 'converts mix of styled variable arguments and object arguments',
@@ -393,7 +423,7 @@ typescriptEslintTester.run(
393
423
  })
394
424
  );
395
425
  `,
396
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
426
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
397
427
  },
398
428
  {
399
429
  name: 'converts mix of props and non-props',
@@ -415,7 +445,7 @@ typescriptEslintTester.run(
415
445
  margin: \`2px \${token('some-token')}\`
416
446
  }));
417
447
  `,
418
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
448
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
419
449
  },
420
450
  ],
421
451
  },
@@ -43,7 +43,7 @@ typescriptEslintTester.run(
43
43
  backgroundColor: props.backgroundColor,
44
44
  }));
45
45
  `,
46
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
46
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
47
47
  },
48
48
  {
49
49
  name: 'converts styled usages with type annotation on the outside (as a variable)',
@@ -71,7 +71,7 @@ typescriptEslintTester.run(
71
71
  backgroundColor: props.backgroundColor,
72
72
  }));
73
73
  `,
74
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
74
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
75
75
  },
76
76
  {
77
77
  name: 'disables autofixer when type annotation is inside props argument',
@@ -90,7 +90,7 @@ typescriptEslintTester.run(
90
90
  `,
91
91
  errors: [
92
92
  {
93
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
93
+ messageId: 'unsupported-prop-syntax-no-autofixer',
94
94
  },
95
95
  ],
96
96
  },
@@ -111,7 +111,7 @@ typescriptEslintTester.run(
111
111
  `,
112
112
  errors: [
113
113
  {
114
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
114
+ messageId: 'unsupported-prop-syntax-no-autofixer',
115
115
  },
116
116
  ],
117
117
  },
@@ -131,7 +131,7 @@ typescriptEslintTester.run(
131
131
  `,
132
132
  errors: [
133
133
  {
134
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
134
+ messageId: 'unsupported-prop-syntax-no-autofixer',
135
135
  },
136
136
  ],
137
137
  },
@@ -20,7 +20,7 @@ typescriptEslintTester.run(
20
20
  `,
21
21
  errors: [
22
22
  {
23
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
23
+ messageId: 'unsupported-prop-syntax-no-autofixer',
24
24
  },
25
25
  ],
26
26
  },
@@ -36,7 +36,7 @@ typescriptEslintTester.run(
36
36
  `,
37
37
  errors: [
38
38
  {
39
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
39
+ messageId: 'unsupported-prop-syntax-no-autofixer',
40
40
  },
41
41
  ],
42
42
  },
@@ -62,7 +62,7 @@ typescriptEslintTester.run(
62
62
  color: props.otherColor,
63
63
  }));
64
64
  `,
65
- errors: [{ messageId: 'unsupported-styled-components-prop-syntax' }],
65
+ errors: [{ messageId: 'unsupported-prop-syntax' }],
66
66
  },
67
67
  ],
68
68
  },
@@ -8,15 +8,10 @@ import type {
8
8
  ObjectExpression,
9
9
  } from 'estree-jsx';
10
10
  import { createLintRule } from '../utils/create-rule';
11
- import {
12
- CSS_IN_JS_IMPORTS,
13
- isStyledComponents,
14
- } from '../utils/is-supported-import';
11
+ import { isStyledComponents, isEmotion } from '../utils/is-supported-import';
15
12
  import type { Rule, Scope, SourceCode } from 'eslint';
16
13
  import ESTraverse from 'estraverse';
17
14
 
18
- const IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.styledComponents];
19
-
20
15
  type PropInfo = {
21
16
  /**
22
17
  * Name of the prop, e.g. props, backgroundColor.
@@ -240,7 +235,7 @@ const lintArguments = (
240
235
  if (allProps.some((propUsage) => !propUsage.valid)) {
241
236
  context.report({
242
237
  node: argument,
243
- messageId: 'unsupported-styled-components-prop-syntax-no-autofixer',
238
+ messageId: 'unsupported-prop-syntax-no-autofixer',
244
239
  });
245
240
 
246
241
  return;
@@ -248,7 +243,7 @@ const lintArguments = (
248
243
 
249
244
  context.report({
250
245
  node: argument,
251
- messageId: 'unsupported-styled-components-prop-syntax',
246
+ messageId: 'unsupported-prop-syntax',
252
247
  fix(fixer) {
253
248
  const sourceCode = context.getSourceCode();
254
249
  return fixPropsObjectUsages(
@@ -376,17 +371,17 @@ export const rule = createLintRule({
376
371
  name: 'convert-props-syntax',
377
372
  docs: {
378
373
  description:
379
- 'Convert props syntax that is unsupported by `styled-components` <4.x to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by `styled-components`.',
374
+ 'Convert props syntax that is unsupported by styled-components@<4 or @emotion/styled to props syntax that is supported. This is useful when used in conjunction with `no-styled-tagged-template-expression`, as output from the latter may use props syntax unsupported by those libraries.',
380
375
  recommended: true,
381
376
  severity: 'error',
382
377
  },
383
378
  fixable: 'code',
384
379
  messages: {
385
- 'unsupported-styled-components-prop-syntax':
386
- 'This syntax is not supported by styled-components <4.x and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or use the autofixer to switch the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.',
380
+ 'unsupported-prop-syntax':
381
+ 'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or use the autofixer to switch the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.',
387
382
 
388
- 'unsupported-styled-components-prop-syntax-no-autofixer':
389
- 'This syntax is not supported by styled-components <4.x and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or manually migrate the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.\n\nCheck out the rule documentation for examples.',
383
+ 'unsupported-prop-syntax-no-autofixer':
384
+ 'This syntax is not supported by styled-components@<4 or @emotion/styled and will fail at runtime!\n\nTo fix this, you can either migrate this to `@compiled/react`, or manually migrate the syntax from the `styled.div({ property: (props) => props.value })` syntax to the `styled.div(props => ({ property: props.value }))` syntax.\n\nCheck out the rule documentation for examples.',
390
385
  },
391
386
  type: 'problem',
392
387
  },
@@ -401,7 +396,10 @@ export const rule = createLintRule({
401
396
  ? node.callee.object
402
397
  : node.callee;
403
398
 
404
- if (!isStyledComponents(callee, references, IMPORT_SOURCES)) {
399
+ if (
400
+ !isStyledComponents(callee, references) &&
401
+ !isEmotion(callee, references)
402
+ ) {
405
403
  return;
406
404
  }
407
405
 
@@ -177,9 +177,20 @@ export const isImportedFrom =
177
177
  (
178
178
  nodeToCheck: Callee,
179
179
  referencesInScope: Reference[],
180
- importSources: ImportSource[],
180
+ /**
181
+ * If we strictly have specific import sources in the config scope, pass them to make this more performant.
182
+ * Pass `null` if you don't care if its configured or not.
183
+ */
184
+ importSources: ImportSource[] | null = null,
181
185
  ): boolean => {
182
- if (!importSources.includes(moduleName)) {
186
+ if (
187
+ importSources &&
188
+ !importSources.some(
189
+ (importSource) =>
190
+ importSource === moduleName ||
191
+ (!exactMatch && importSource.startsWith(moduleName)),
192
+ )
193
+ ) {
183
194
  // Don't go through the trouble of checking the import sources does not include this
184
195
  // We'll assume this is skipped elsewhere.
185
196
  return false;