@atlaskit/eslint-plugin-design-system 8.32.1 → 8.32.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atlaskit/eslint-plugin-design-system
2
2
 
3
+ ## 8.32.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#77485](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/77485) [`887b1a3193ce`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/887b1a3193ce) - For `no-styled-tagged-template-expression`, do not autofix component selectors for `styled-components` implementations
8
+
3
9
  ## 8.32.1
4
10
 
5
11
  ### Patch Changes
@@ -86,3 +86,45 @@ export const Component = styled.div``;
86
86
  ## Limitations
87
87
 
88
88
  - Comments are not fixable.
89
+ - Component selectors are not fixable for `styled-components`.
90
+
91
+ Do not migrate to this object syntax manually, it is invalid `styled-components`.
92
+
93
+ ```tsx
94
+ const Button = styled.button``;
95
+ const Wrapper = styled.div({
96
+ color: 'red',
97
+ [`${Button}`]: {
98
+ color: 'blue',
99
+ },
100
+ });
101
+ <Wrapper><Button /><Wrapper>
102
+ ```
103
+
104
+ Instead, style the button directly—make it clear that you're styling that button.
105
+
106
+ ```tsx
107
+ const buttonStyles = css({ color: 'blue' });
108
+ const Wrapper = styled.div({
109
+ color: 'red',
110
+ });
111
+
112
+ <Wrapper>
113
+ <button css={buttonStyles} />
114
+ </Wrapper>;
115
+ ```
116
+
117
+ If that's not feasible, you can use data attributes, but these will result in failing the UI Styling Standard around nested styles, pushing the error down the road.
118
+
119
+ ```tsx
120
+ const Wrapper = styled.div({
121
+ color: 'red',
122
+ '[data-component-selector="my.button"]': {
123
+ color: 'blue',
124
+ },
125
+ });
126
+
127
+ <Wrapper>
128
+ <button data-component-selector="my.button" />
129
+ </Wrapper>;
130
+ ```
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _createNoTaggedTemplateExpressionRule = require("../utils/create-no-tagged-template-expression-rule");
8
8
  var _createRule = require("../utils/create-rule");
9
- var _isStyled = require("./is-styled");
9
+ var _isSupportedImport = require("../utils/is-supported-import");
10
10
  var rule = (0, _createRule.createLintRule)({
11
11
  meta: {
12
12
  name: 'no-styled-tagged-template-expression',
@@ -22,6 +22,6 @@ var rule = (0, _createRule.createLintRule)({
22
22
  },
23
23
  schema: _createNoTaggedTemplateExpressionRule.noTaggedTemplateExpressionRuleSchema
24
24
  },
25
- create: (0, _createNoTaggedTemplateExpressionRule.createNoTaggedTemplateExpressionRule)(_isStyled.isStyled, 'unexpectedTaggedTemplate')
25
+ create: (0, _createNoTaggedTemplateExpressionRule.createNoTaggedTemplateExpressionRule)(_isSupportedImport.isStyled, 'unexpectedTaggedTemplate')
26
26
  });
27
27
  var _default = exports.default = rule;
@@ -43,6 +43,7 @@ var createNoTaggedTemplateExpressionRule = exports.createNoTaggedTemplateExpress
43
43
  if (!isUsage(node.tag, references, importSources)) {
44
44
  return;
45
45
  }
46
+ var isSC = (0, _isSupportedImport.isStyledComponents)(node.tag, references, importSources);
46
47
  context.report({
47
48
  messageId: messageId,
48
49
  node: node,
@@ -94,12 +95,18 @@ var createNoTaggedTemplateExpressionRule = exports.createNoTaggedTemplateExpress
94
95
  }
95
96
  return _context.abrupt("return");
96
97
  case 16:
97
- _context.next = 18;
98
- return fixer.insertTextBefore(node, newCode);
98
+ if (!(isSC && /\$\{.*:[\s]*\{/.test(newCode))) {
99
+ _context.next = 18;
100
+ break;
101
+ }
102
+ return _context.abrupt("return");
99
103
  case 18:
100
104
  _context.next = 20;
101
- return fixer.remove(node);
105
+ return fixer.insertTextBefore(node, newCode);
102
106
  case 20:
107
+ _context.next = 22;
108
+ return fixer.remove(node);
109
+ case 22:
103
110
  case "end":
104
111
  return _context.stop();
105
112
  }
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.isStyled = exports.isKeyframes = exports.isCxFunction = exports.isCssMap = exports.isCss = exports.getImportSources = exports.DEFAULT_IMPORT_SOURCES = exports.CSS_IN_JS_IMPORTS = void 0;
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;
7
7
  // eslint-disable-next-line import/no-extraneous-dependencies
8
8
 
9
9
  var CSS_IN_JS_IMPORTS = exports.CSS_IN_JS_IMPORTS = {
@@ -23,6 +23,21 @@ var CSS_IN_JS_IMPORTS = exports.CSS_IN_JS_IMPORTS = {
23
23
  * By default all known import sources are checked against.
24
24
  */
25
25
  var DEFAULT_IMPORT_SOURCES = exports.DEFAULT_IMPORT_SOURCES = Object.values(CSS_IN_JS_IMPORTS);
26
+ var getIdentifierNode = function getIdentifierNode(node) {
27
+ var identifierNode = node.type === 'Identifier' ? node : undefined;
28
+ if (!identifierNode) {
29
+ // Handles styled.div`` case
30
+ if (node.type === 'MemberExpression' && node.object.type === 'Identifier') {
31
+ identifierNode = node.object;
32
+ }
33
+
34
+ // Handles styled(Component)`` case
35
+ if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
36
+ identifierNode = node.callee;
37
+ }
38
+ }
39
+ return identifierNode;
40
+ };
26
41
 
27
42
  /**
28
43
  * Given the ESLint rule context, extract and parse the value of the importSources rule option.
@@ -43,6 +58,7 @@ var getImportSources = exports.getImportSources = function getImportSources(cont
43
58
  return DEFAULT_IMPORT_SOURCES;
44
59
  };
45
60
  var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
61
+ var defaultFromImportSources = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
46
62
  var checkDefinitionHasImport = function checkDefinitionHasImport(def, importSources) {
47
63
  if (def.type !== 'ImportBinding') {
48
64
  return false;
@@ -50,12 +66,19 @@ var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
50
66
  if (!def.parent || !importSources.includes(def.parent.source.value)) {
51
67
  return false;
52
68
  }
53
- return (
54
- // import { functionName } from 'import-source';
55
- def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName ||
56
- // import functionName from 'import-source';
57
- def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName
58
- );
69
+
70
+ // Matches the imported name from a named import
71
+ // import { functionName, functioName as otherName } from 'import-source';
72
+ var isNamedImport = def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName;
73
+
74
+ // Must explicitly match the local name from a default import
75
+ // import functionName from 'import-source';
76
+ var isDefaultImportMatchingLocal = def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName;
77
+
78
+ // Can match any local name from a default import
79
+ // import anything from 'import-source'
80
+ var isKnownDefaultImport = def.node.type === 'ImportDefaultSpecifier' && defaultFromImportSources.includes(def.parent.source.value);
81
+ return isNamedImport || isDefaultImportMatchingLocal || isKnownDefaultImport;
59
82
  };
60
83
 
61
84
  /**
@@ -75,9 +98,10 @@ var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
75
98
  * @returns Whether the above conditions are true.
76
99
  */
77
100
  var isSupportedImport = function isSupportedImport(nodeToCheck, referencesInScope, importSources) {
78
- return nodeToCheck.type === 'Identifier' && referencesInScope.some(function (reference) {
101
+ var identifierNode = getIdentifierNode(nodeToCheck);
102
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(function (reference) {
79
103
  var _reference$resolved;
80
- return reference.identifier === nodeToCheck && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(function (def) {
104
+ return reference.identifier === identifierNode && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(function (def) {
81
105
  return checkDefinitionHasImport(def, importSources);
82
106
  }));
83
107
  });
@@ -92,4 +116,34 @@ var isCss = exports.isCss = isSupportedImportWrapper('css');
92
116
  var isCxFunction = exports.isCxFunction = isSupportedImportWrapper('cx');
93
117
  var isCssMap = exports.isCssMap = isSupportedImportWrapper('cssMap');
94
118
  var isKeyframes = exports.isKeyframes = isSupportedImportWrapper('keyframes');
95
- var isStyled = exports.isStyled = isSupportedImportWrapper('styled');
119
+ // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
120
+ var isStyled = exports.isStyled = isSupportedImportWrapper('styled', ['styled-components', '@emotion/styled']);
121
+ var isImportedFrom = exports.isImportedFrom = function isImportedFrom(moduleName) {
122
+ var exactMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
123
+ return function (nodeToCheck, referencesInScope, importSources) {
124
+ if (!importSources.includes(moduleName)) {
125
+ // Don't go through the trouble of checking the import sources does not include this
126
+ // We'll assume this is skipped elsewhere.
127
+ return false;
128
+ }
129
+ var identifierNode = getIdentifierNode(nodeToCheck);
130
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(function (reference) {
131
+ var _reference$resolved2;
132
+ return reference.identifier === identifierNode && ((_reference$resolved2 = reference.resolved) === null || _reference$resolved2 === void 0 ? void 0 : _reference$resolved2.defs.some(function (def) {
133
+ var _def$parent, _String, _def$parent2;
134
+ return def.type === 'ImportBinding' && (((_def$parent = def.parent) === null || _def$parent === void 0 ? void 0 : _def$parent.source.value) === moduleName || !exactMatch && ((_String = String((_def$parent2 = def.parent) === null || _def$parent2 === void 0 ? void 0 : _def$parent2.source.value)) === null || _String === void 0 ? void 0 : _String.startsWith(moduleName)));
135
+ }));
136
+ });
137
+ };
138
+ };
139
+
140
+ /**
141
+ * Determine if this node is specifically from a `'styled-components'` import.
142
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
143
+ * we need to handle them differently in a few scenarios.
144
+ *
145
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
146
+ */
147
+ var isStyledComponents = exports.isStyledComponents = isImportedFrom('styled-components');
148
+ var isCompiled = exports.isCompiled = isImportedFrom('@compiled/', false);
149
+ var isEmotion = exports.isEmotion = isImportedFrom('@emotion/', false);
@@ -1,6 +1,6 @@
1
1
  import { createNoTaggedTemplateExpressionRule, noTaggedTemplateExpressionRuleSchema } from '../utils/create-no-tagged-template-expression-rule';
2
2
  import { createLintRule } from '../utils/create-rule';
3
- import { isStyled } from './is-styled';
3
+ import { isStyled } from '../utils/is-supported-import';
4
4
  const rule = createLintRule({
5
5
  meta: {
6
6
  name: 'no-styled-tagged-template-expression',
@@ -1,7 +1,7 @@
1
1
  // Original source from Compiled https://github.com/atlassian-labs/compiled/blob/master/packages/eslint-plugin/src/utils/create-no-tagged-template-expression-rule/index.ts
2
2
  // eslint-disable-next-line import/no-extraneous-dependencies
3
3
 
4
- import { getImportSources } from '../is-supported-import';
4
+ import { getImportSources, isStyledComponents } from '../is-supported-import';
5
5
  import { generate } from './generate';
6
6
  import { getTaggedTemplateExpressionOffset } from './get-tagged-template-expression-offset';
7
7
  import { toArguments } from './to-arguments';
@@ -35,6 +35,7 @@ export const createNoTaggedTemplateExpressionRule = (isUsage, messageId) => cont
35
35
  if (!isUsage(node.tag, references, importSources)) {
36
36
  return;
37
37
  }
38
+ const isSC = isStyledComponents(node.tag, references, importSources);
38
39
  context.report({
39
40
  messageId,
40
41
  node,
@@ -75,6 +76,30 @@ export const createNoTaggedTemplateExpressionRule = (isUsage, messageId) => cont
75
76
  if (oldCode === newCode) {
76
77
  return;
77
78
  }
79
+
80
+ // TODO: We might want to similarly disallow `styled.div({ color: props => props.color })` for SC as it's broken too (both type and functionality)
81
+ // Alternatively, autofix it to `styled.div(props => ({ color: props.color }))`?
82
+ if (isSC && /\$\{.*:[\s]*\{/.test(newCode)) {
83
+ /**
84
+ * If we find a variable in a selector when migrating `styled-components` code, we skip it.
85
+ * This is because `styled-components@3.x` does not support the syntax.
86
+ *
87
+ * @example
88
+ * ```tsx
89
+ * const Component = styled.div`
90
+ * & + ${Button} { color: red; }
91
+ * `;
92
+ * ```
93
+ * Becomes this code, which is not supported in `styled-components@3.x`:
94
+ * ```tsx
95
+ * const Component = styled.div({
96
+ * [`& + ${Button}`]: {
97
+ * color: 'red',
98
+ * });
99
+ * ```
100
+ */
101
+ return;
102
+ }
78
103
  yield fixer.insertTextBefore(node, newCode);
79
104
  yield fixer.remove(node);
80
105
  }
@@ -16,6 +16,21 @@ export const CSS_IN_JS_IMPORTS = {
16
16
  * By default all known import sources are checked against.
17
17
  */
18
18
  export const DEFAULT_IMPORT_SOURCES = Object.values(CSS_IN_JS_IMPORTS);
19
+ const getIdentifierNode = node => {
20
+ let identifierNode = node.type === 'Identifier' ? node : undefined;
21
+ if (!identifierNode) {
22
+ // Handles styled.div`` case
23
+ if (node.type === 'MemberExpression' && node.object.type === 'Identifier') {
24
+ identifierNode = node.object;
25
+ }
26
+
27
+ // Handles styled(Component)`` case
28
+ if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
29
+ identifierNode = node.callee;
30
+ }
31
+ }
32
+ return identifierNode;
33
+ };
19
34
 
20
35
  /**
21
36
  * Given the ESLint rule context, extract and parse the value of the importSources rule option.
@@ -35,7 +50,7 @@ export const getImportSources = context => {
35
50
  }
36
51
  return DEFAULT_IMPORT_SOURCES;
37
52
  };
38
- const isSupportedImportWrapper = functionName => {
53
+ const isSupportedImportWrapper = (functionName, defaultFromImportSources = []) => {
39
54
  const checkDefinitionHasImport = (def, importSources) => {
40
55
  if (def.type !== 'ImportBinding') {
41
56
  return false;
@@ -43,12 +58,19 @@ const isSupportedImportWrapper = functionName => {
43
58
  if (!def.parent || !importSources.includes(def.parent.source.value)) {
44
59
  return false;
45
60
  }
46
- return (
47
- // import { functionName } from 'import-source';
48
- def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName ||
49
- // import functionName from 'import-source';
50
- def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName
51
- );
61
+
62
+ // Matches the imported name from a named import
63
+ // import { functionName, functioName as otherName } from 'import-source';
64
+ const isNamedImport = def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName;
65
+
66
+ // Must explicitly match the local name from a default import
67
+ // import functionName from 'import-source';
68
+ const isDefaultImportMatchingLocal = def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName;
69
+
70
+ // Can match any local name from a default import
71
+ // import anything from 'import-source'
72
+ const isKnownDefaultImport = def.node.type === 'ImportDefaultSpecifier' && defaultFromImportSources.includes(def.parent.source.value);
73
+ return isNamedImport || isDefaultImportMatchingLocal || isKnownDefaultImport;
52
74
  };
53
75
 
54
76
  /**
@@ -68,9 +90,10 @@ const isSupportedImportWrapper = functionName => {
68
90
  * @returns Whether the above conditions are true.
69
91
  */
70
92
  const isSupportedImport = (nodeToCheck, referencesInScope, importSources) => {
71
- return nodeToCheck.type === 'Identifier' && referencesInScope.some(reference => {
93
+ const identifierNode = getIdentifierNode(nodeToCheck);
94
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(reference => {
72
95
  var _reference$resolved;
73
- return reference.identifier === nodeToCheck && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(def => checkDefinitionHasImport(def, importSources)));
96
+ return reference.identifier === identifierNode && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(def => checkDefinitionHasImport(def, importSources)));
74
97
  });
75
98
  };
76
99
  return isSupportedImport;
@@ -83,4 +106,31 @@ export const isCss = isSupportedImportWrapper('css');
83
106
  export const isCxFunction = isSupportedImportWrapper('cx');
84
107
  export const isCssMap = isSupportedImportWrapper('cssMap');
85
108
  export const isKeyframes = isSupportedImportWrapper('keyframes');
86
- export const isStyled = isSupportedImportWrapper('styled');
109
+ // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
110
+ export const isStyled = isSupportedImportWrapper('styled', ['styled-components', '@emotion/styled']);
111
+ export const isImportedFrom = (moduleName, exactMatch = true) => (nodeToCheck, referencesInScope, importSources) => {
112
+ if (!importSources.includes(moduleName)) {
113
+ // Don't go through the trouble of checking the import sources does not include this
114
+ // We'll assume this is skipped elsewhere.
115
+ return false;
116
+ }
117
+ const identifierNode = getIdentifierNode(nodeToCheck);
118
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(reference => {
119
+ var _reference$resolved2;
120
+ return reference.identifier === identifierNode && ((_reference$resolved2 = reference.resolved) === null || _reference$resolved2 === void 0 ? void 0 : _reference$resolved2.defs.some(def => {
121
+ var _def$parent, _String, _def$parent2;
122
+ return def.type === 'ImportBinding' && (((_def$parent = def.parent) === null || _def$parent === void 0 ? void 0 : _def$parent.source.value) === moduleName || !exactMatch && ((_String = String((_def$parent2 = def.parent) === null || _def$parent2 === void 0 ? void 0 : _def$parent2.source.value)) === null || _String === void 0 ? void 0 : _String.startsWith(moduleName)));
123
+ }));
124
+ });
125
+ };
126
+
127
+ /**
128
+ * Determine if this node is specifically from a `'styled-components'` import.
129
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
130
+ * we need to handle them differently in a few scenarios.
131
+ *
132
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
133
+ */
134
+ export const isStyledComponents = isImportedFrom('styled-components');
135
+ export const isCompiled = isImportedFrom('@compiled/', false);
136
+ export const isEmotion = isImportedFrom('@emotion/', false);
@@ -1,6 +1,6 @@
1
1
  import { createNoTaggedTemplateExpressionRule, noTaggedTemplateExpressionRuleSchema } from '../utils/create-no-tagged-template-expression-rule';
2
2
  import { createLintRule } from '../utils/create-rule';
3
- import { isStyled } from './is-styled';
3
+ import { isStyled } from '../utils/is-supported-import';
4
4
  var rule = createLintRule({
5
5
  meta: {
6
6
  name: 'no-styled-tagged-template-expression',
@@ -2,7 +2,7 @@ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
2
  // Original source from Compiled https://github.com/atlassian-labs/compiled/blob/master/packages/eslint-plugin/src/utils/create-no-tagged-template-expression-rule/index.ts
3
3
  // eslint-disable-next-line import/no-extraneous-dependencies
4
4
 
5
- import { getImportSources } from '../is-supported-import';
5
+ import { getImportSources, isStyledComponents } from '../is-supported-import';
6
6
  import { generate } from './generate';
7
7
  import { getTaggedTemplateExpressionOffset } from './get-tagged-template-expression-offset';
8
8
  import { toArguments } from './to-arguments';
@@ -36,6 +36,7 @@ export var createNoTaggedTemplateExpressionRule = function createNoTaggedTemplat
36
36
  if (!isUsage(node.tag, references, importSources)) {
37
37
  return;
38
38
  }
39
+ var isSC = isStyledComponents(node.tag, references, importSources);
39
40
  context.report({
40
41
  messageId: messageId,
41
42
  node: node,
@@ -87,12 +88,18 @@ export var createNoTaggedTemplateExpressionRule = function createNoTaggedTemplat
87
88
  }
88
89
  return _context.abrupt("return");
89
90
  case 16:
90
- _context.next = 18;
91
- return fixer.insertTextBefore(node, newCode);
91
+ if (!(isSC && /\$\{.*:[\s]*\{/.test(newCode))) {
92
+ _context.next = 18;
93
+ break;
94
+ }
95
+ return _context.abrupt("return");
92
96
  case 18:
93
97
  _context.next = 20;
94
- return fixer.remove(node);
98
+ return fixer.insertTextBefore(node, newCode);
95
99
  case 20:
100
+ _context.next = 22;
101
+ return fixer.remove(node);
102
+ case 22:
96
103
  case "end":
97
104
  return _context.stop();
98
105
  }
@@ -16,6 +16,21 @@ export var CSS_IN_JS_IMPORTS = {
16
16
  * By default all known import sources are checked against.
17
17
  */
18
18
  export var DEFAULT_IMPORT_SOURCES = Object.values(CSS_IN_JS_IMPORTS);
19
+ var getIdentifierNode = function getIdentifierNode(node) {
20
+ var identifierNode = node.type === 'Identifier' ? node : undefined;
21
+ if (!identifierNode) {
22
+ // Handles styled.div`` case
23
+ if (node.type === 'MemberExpression' && node.object.type === 'Identifier') {
24
+ identifierNode = node.object;
25
+ }
26
+
27
+ // Handles styled(Component)`` case
28
+ if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
29
+ identifierNode = node.callee;
30
+ }
31
+ }
32
+ return identifierNode;
33
+ };
19
34
 
20
35
  /**
21
36
  * Given the ESLint rule context, extract and parse the value of the importSources rule option.
@@ -36,6 +51,7 @@ export var getImportSources = function getImportSources(context) {
36
51
  return DEFAULT_IMPORT_SOURCES;
37
52
  };
38
53
  var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
54
+ var defaultFromImportSources = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
39
55
  var checkDefinitionHasImport = function checkDefinitionHasImport(def, importSources) {
40
56
  if (def.type !== 'ImportBinding') {
41
57
  return false;
@@ -43,12 +59,19 @@ var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
43
59
  if (!def.parent || !importSources.includes(def.parent.source.value)) {
44
60
  return false;
45
61
  }
46
- return (
47
- // import { functionName } from 'import-source';
48
- def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName ||
49
- // import functionName from 'import-source';
50
- def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName
51
- );
62
+
63
+ // Matches the imported name from a named import
64
+ // import { functionName, functioName as otherName } from 'import-source';
65
+ var isNamedImport = def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName;
66
+
67
+ // Must explicitly match the local name from a default import
68
+ // import functionName from 'import-source';
69
+ var isDefaultImportMatchingLocal = def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName;
70
+
71
+ // Can match any local name from a default import
72
+ // import anything from 'import-source'
73
+ var isKnownDefaultImport = def.node.type === 'ImportDefaultSpecifier' && defaultFromImportSources.includes(def.parent.source.value);
74
+ return isNamedImport || isDefaultImportMatchingLocal || isKnownDefaultImport;
52
75
  };
53
76
 
54
77
  /**
@@ -68,9 +91,10 @@ var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
68
91
  * @returns Whether the above conditions are true.
69
92
  */
70
93
  var isSupportedImport = function isSupportedImport(nodeToCheck, referencesInScope, importSources) {
71
- return nodeToCheck.type === 'Identifier' && referencesInScope.some(function (reference) {
94
+ var identifierNode = getIdentifierNode(nodeToCheck);
95
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(function (reference) {
72
96
  var _reference$resolved;
73
- return reference.identifier === nodeToCheck && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(function (def) {
97
+ return reference.identifier === identifierNode && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(function (def) {
74
98
  return checkDefinitionHasImport(def, importSources);
75
99
  }));
76
100
  });
@@ -85,4 +109,34 @@ export var isCss = isSupportedImportWrapper('css');
85
109
  export var isCxFunction = isSupportedImportWrapper('cx');
86
110
  export var isCssMap = isSupportedImportWrapper('cssMap');
87
111
  export var isKeyframes = isSupportedImportWrapper('keyframes');
88
- export var isStyled = isSupportedImportWrapper('styled');
112
+ // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
113
+ export var isStyled = isSupportedImportWrapper('styled', ['styled-components', '@emotion/styled']);
114
+ export var isImportedFrom = function isImportedFrom(moduleName) {
115
+ var exactMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
116
+ return function (nodeToCheck, referencesInScope, importSources) {
117
+ if (!importSources.includes(moduleName)) {
118
+ // Don't go through the trouble of checking the import sources does not include this
119
+ // We'll assume this is skipped elsewhere.
120
+ return false;
121
+ }
122
+ var identifierNode = getIdentifierNode(nodeToCheck);
123
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(function (reference) {
124
+ var _reference$resolved2;
125
+ return reference.identifier === identifierNode && ((_reference$resolved2 = reference.resolved) === null || _reference$resolved2 === void 0 ? void 0 : _reference$resolved2.defs.some(function (def) {
126
+ var _def$parent, _String, _def$parent2;
127
+ return def.type === 'ImportBinding' && (((_def$parent = def.parent) === null || _def$parent === void 0 ? void 0 : _def$parent.source.value) === moduleName || !exactMatch && ((_String = String((_def$parent2 = def.parent) === null || _def$parent2 === void 0 ? void 0 : _def$parent2.source.value)) === null || _String === void 0 ? void 0 : _String.startsWith(moduleName)));
128
+ }));
129
+ });
130
+ };
131
+ };
132
+
133
+ /**
134
+ * Determine if this node is specifically from a `'styled-components'` import.
135
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
136
+ * we need to handle them differently in a few scenarios.
137
+ *
138
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
139
+ */
140
+ export var isStyledComponents = isImportedFrom('styled-components');
141
+ export var isCompiled = isImportedFrom('@compiled/', false);
142
+ export var isEmotion = isImportedFrom('@emotion/', false);
@@ -31,4 +31,15 @@ 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;
35
+ /**
36
+ * Determine if this node is specifically from a `'styled-components'` import.
37
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
38
+ * we need to handle them differently in a few scenarios.
39
+ *
40
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
41
+ */
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;
34
45
  export {};
@@ -31,4 +31,15 @@ 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;
35
+ /**
36
+ * Determine if this node is specifically from a `'styled-components'` import.
37
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
38
+ * we need to handle them differently in a few scenarios.
39
+ *
40
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
41
+ */
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;
34
45
  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.32.1",
4
+ "version": "8.32.2",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "publishConfig": {
7
7
  "registry": "https://registry.npmjs.org/"
@@ -1,53 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.isStyled = void 0;
7
- var _isSupportedImport = require("../utils/is-supported-import");
8
- // eslint-disable-next-line import/no-extraneous-dependencies
9
-
10
- var functionName = 'styled';
11
- var checkDefinitionHasImport = function checkDefinitionHasImport(def, importSources) {
12
- if (def.type !== 'ImportBinding') {
13
- return false;
14
- }
15
- if (!def.parent || !importSources.includes(def.parent.source.value)) {
16
- return false;
17
- }
18
-
19
- // `@compiled/react` only exposes styled as a named export
20
- if (importSources.includes(_isSupportedImport.CSS_IN_JS_IMPORTS.compiled) && def.parent.source.value === _isSupportedImport.CSS_IN_JS_IMPORTS.compiled) {
21
- return def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName;
22
- }
23
-
24
- // `@emotion/styled` only exposes styled as a default export
25
- if (importSources.includes(_isSupportedImport.CSS_IN_JS_IMPORTS.emotionStyled) && def.parent.source.value === _isSupportedImport.CSS_IN_JS_IMPORTS.emotionStyled) {
26
- return def.node.type === 'ImportDefaultSpecifier';
27
- }
28
-
29
- // `styled-components` only exposes styled as a default export
30
- if (importSources.includes(_isSupportedImport.CSS_IN_JS_IMPORTS.styledComponents) && def.parent.source.value === _isSupportedImport.CSS_IN_JS_IMPORTS.styledComponents) {
31
- return def.node.type === 'ImportDefaultSpecifier';
32
- }
33
- return false;
34
- };
35
- var isStyled = exports.isStyled = function isStyled(nodeToCheck, referencesInScope, importSources) {
36
- var nodeIdentifier = null;
37
-
38
- // Handles styled.div`` case
39
- if (nodeToCheck.type === 'MemberExpression' && nodeToCheck.object.type === 'Identifier') {
40
- nodeIdentifier = nodeToCheck.object;
41
- }
42
-
43
- // Handles styled(Base)`` case
44
- if (nodeToCheck.type === 'CallExpression' && nodeToCheck.callee.type === 'Identifier') {
45
- nodeIdentifier = nodeToCheck.callee;
46
- }
47
- return referencesInScope.some(function (reference) {
48
- var _reference$resolved;
49
- return reference.identifier === nodeIdentifier && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(function (def) {
50
- return checkDefinitionHasImport(def, importSources);
51
- }));
52
- });
53
- };
@@ -1,45 +0,0 @@
1
- // eslint-disable-next-line import/no-extraneous-dependencies
2
-
3
- import { CSS_IN_JS_IMPORTS } from '../utils/is-supported-import';
4
- const functionName = 'styled';
5
- const checkDefinitionHasImport = (def, importSources) => {
6
- if (def.type !== 'ImportBinding') {
7
- return false;
8
- }
9
- if (!def.parent || !importSources.includes(def.parent.source.value)) {
10
- return false;
11
- }
12
-
13
- // `@compiled/react` only exposes styled as a named export
14
- if (importSources.includes(CSS_IN_JS_IMPORTS.compiled) && def.parent.source.value === CSS_IN_JS_IMPORTS.compiled) {
15
- return def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName;
16
- }
17
-
18
- // `@emotion/styled` only exposes styled as a default export
19
- if (importSources.includes(CSS_IN_JS_IMPORTS.emotionStyled) && def.parent.source.value === CSS_IN_JS_IMPORTS.emotionStyled) {
20
- return def.node.type === 'ImportDefaultSpecifier';
21
- }
22
-
23
- // `styled-components` only exposes styled as a default export
24
- if (importSources.includes(CSS_IN_JS_IMPORTS.styledComponents) && def.parent.source.value === CSS_IN_JS_IMPORTS.styledComponents) {
25
- return def.node.type === 'ImportDefaultSpecifier';
26
- }
27
- return false;
28
- };
29
- export const isStyled = (nodeToCheck, referencesInScope, importSources) => {
30
- let nodeIdentifier = null;
31
-
32
- // Handles styled.div`` case
33
- if (nodeToCheck.type === 'MemberExpression' && nodeToCheck.object.type === 'Identifier') {
34
- nodeIdentifier = nodeToCheck.object;
35
- }
36
-
37
- // Handles styled(Base)`` case
38
- if (nodeToCheck.type === 'CallExpression' && nodeToCheck.callee.type === 'Identifier') {
39
- nodeIdentifier = nodeToCheck.callee;
40
- }
41
- return referencesInScope.some(reference => {
42
- var _reference$resolved;
43
- return reference.identifier === nodeIdentifier && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(def => checkDefinitionHasImport(def, importSources)));
44
- });
45
- };
@@ -1,47 +0,0 @@
1
- // eslint-disable-next-line import/no-extraneous-dependencies
2
-
3
- import { CSS_IN_JS_IMPORTS } from '../utils/is-supported-import';
4
- var functionName = 'styled';
5
- var checkDefinitionHasImport = function checkDefinitionHasImport(def, importSources) {
6
- if (def.type !== 'ImportBinding') {
7
- return false;
8
- }
9
- if (!def.parent || !importSources.includes(def.parent.source.value)) {
10
- return false;
11
- }
12
-
13
- // `@compiled/react` only exposes styled as a named export
14
- if (importSources.includes(CSS_IN_JS_IMPORTS.compiled) && def.parent.source.value === CSS_IN_JS_IMPORTS.compiled) {
15
- return def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName;
16
- }
17
-
18
- // `@emotion/styled` only exposes styled as a default export
19
- if (importSources.includes(CSS_IN_JS_IMPORTS.emotionStyled) && def.parent.source.value === CSS_IN_JS_IMPORTS.emotionStyled) {
20
- return def.node.type === 'ImportDefaultSpecifier';
21
- }
22
-
23
- // `styled-components` only exposes styled as a default export
24
- if (importSources.includes(CSS_IN_JS_IMPORTS.styledComponents) && def.parent.source.value === CSS_IN_JS_IMPORTS.styledComponents) {
25
- return def.node.type === 'ImportDefaultSpecifier';
26
- }
27
- return false;
28
- };
29
- export var isStyled = function isStyled(nodeToCheck, referencesInScope, importSources) {
30
- var nodeIdentifier = null;
31
-
32
- // Handles styled.div`` case
33
- if (nodeToCheck.type === 'MemberExpression' && nodeToCheck.object.type === 'Identifier') {
34
- nodeIdentifier = nodeToCheck.object;
35
- }
36
-
37
- // Handles styled(Base)`` case
38
- if (nodeToCheck.type === 'CallExpression' && nodeToCheck.callee.type === 'Identifier') {
39
- nodeIdentifier = nodeToCheck.callee;
40
- }
41
- return referencesInScope.some(function (reference) {
42
- var _reference$resolved;
43
- return reference.identifier === nodeIdentifier && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(function (def) {
44
- return checkDefinitionHasImport(def, importSources);
45
- }));
46
- });
47
- };
@@ -1,7 +0,0 @@
1
- import type { Scope } from 'eslint';
2
- import type { CallExpression } from 'estree';
3
- import { ImportSource } from '../utils/is-supported-import';
4
- type Callee = CallExpression['callee'];
5
- type Reference = Scope.Reference;
6
- export declare const isStyled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
7
- export {};
@@ -1,7 +0,0 @@
1
- import type { Scope } from 'eslint';
2
- import type { CallExpression } from 'estree';
3
- import { ImportSource } from '../utils/is-supported-import';
4
- type Callee = CallExpression['callee'];
5
- type Reference = Scope.Reference;
6
- export declare const isStyled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
7
- export {};