@atlaskit/eslint-plugin-ui-styling-standard 0.4.0 → 0.5.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -0
  3. package/constellation/consistent-css-prop-usage/usage.mdx +2 -2
  4. package/constellation/index/overview.mdx +9 -11
  5. package/constellation/no-css-tagged-template-expression/usage.mdx +2 -2
  6. package/constellation/no-empty-styled-expression/usage.mdx +2 -2
  7. package/constellation/no-exported-css/usage.mdx +2 -2
  8. package/constellation/no-exported-keyframes/usage.mdx +2 -2
  9. package/constellation/no-important-styles/usage.mdx +29 -0
  10. package/constellation/no-invalid-css-map/usage.mdx +2 -2
  11. package/constellation/no-js-xcss/usage.mdx +1 -19
  12. package/constellation/no-keyframes-tagged-template-expression/usage.mdx +2 -2
  13. package/constellation/no-styled-tagged-template-expression/usage.mdx +2 -2
  14. package/dist/cjs/presets/all.codegen.js +5 -2
  15. package/dist/cjs/presets/recommended.codegen.js +5 -2
  16. package/dist/cjs/rules/external-rules.js +27 -1
  17. package/dist/cjs/rules/index.codegen.js +4 -2
  18. package/dist/cjs/rules/no-important-styles/index.js +75 -0
  19. package/dist/cjs/rules/utils/is-supported-import.js +72 -20
  20. package/dist/es2019/presets/all.codegen.js +5 -2
  21. package/dist/es2019/presets/recommended.codegen.js +5 -2
  22. package/dist/es2019/rules/external-rules.js +27 -1
  23. package/dist/es2019/rules/index.codegen.js +4 -2
  24. package/dist/es2019/rules/no-important-styles/index.js +69 -0
  25. package/dist/es2019/rules/utils/is-supported-import.js +69 -18
  26. package/dist/esm/presets/all.codegen.js +5 -2
  27. package/dist/esm/presets/recommended.codegen.js +5 -2
  28. package/dist/esm/rules/external-rules.js +27 -1
  29. package/dist/esm/rules/index.codegen.js +4 -2
  30. package/dist/esm/rules/no-important-styles/index.js +68 -0
  31. package/dist/esm/rules/utils/is-supported-import.js +72 -18
  32. package/dist/types/index.codegen.d.ts +8 -2
  33. package/dist/types/presets/all.codegen.d.ts +5 -2
  34. package/dist/types/presets/recommended.codegen.d.ts +5 -2
  35. package/dist/types/rules/index.codegen.d.ts +1 -0
  36. package/dist/types/rules/no-important-styles/index.d.ts +2 -0
  37. package/dist/types/rules/utils/is-supported-import.d.ts +17 -5
  38. package/dist/types-ts4.5/index.codegen.d.ts +8 -2
  39. package/dist/types-ts4.5/presets/all.codegen.d.ts +5 -2
  40. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +5 -2
  41. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  42. package/dist/types-ts4.5/rules/no-important-styles/index.d.ts +2 -0
  43. package/dist/types-ts4.5/rules/utils/is-supported-import.d.ts +17 -5
  44. package/package.json +5 -3
  45. package/scripts/codegen.tsx +1 -1
  46. package/src/presets/all.codegen.tsx +14 -2
  47. package/src/presets/recommended.codegen.tsx +14 -2
  48. package/src/rules/external-rules.tsx +30 -0
  49. package/src/rules/index.codegen.tsx +3 -1
  50. package/src/rules/no-important-styles/README.md +27 -0
  51. package/src/rules/no-important-styles/__tests__/rule.test.tsx +140 -0
  52. package/src/rules/no-important-styles/index.tsx +88 -0
  53. package/src/rules/utils/is-supported-import.tsx +99 -20
@@ -3,6 +3,29 @@ import { getRuleUrl } from './utils/create-rule';
3
3
  /** External rules must be scoped, have a display name, and external urls. */
4
4
 
5
5
  const importSources = ['@compiled/react', '@atlaskit/css', '@emotion/core', '@emotion/react', '@emotion/styled', 'styled-components'];
6
+
7
+ /**
8
+ * Same as importSources, but excludes `styled-components` because the type-checking
9
+ * for `styled-components` does not support the `no-styled-tagged-template-expression`
10
+ * lint output for this:
11
+ *
12
+ * export const Container = styled(Section)`
13
+ * max-height: ${gridSize * 46}px;
14
+ * overflow: auto;
15
+ * `;
16
+ *
17
+ * export const ErrorStateContainer = styled(Container)`
18
+ * margin: ${
19
+ * (props: any) =>
20
+ * props.canManageAutomations // Use negative margin to reduce whitespace when the Footer component is shown
21
+ * ? `${gridSize * -4}px ${gridSize * 2}px`
22
+ * : `0 ${gridSize * 2}px`
23
+ * };
24
+ * `;
25
+ */
26
+ const noStyledTaggedTemplateExpressionImportSources = ['@compiled/react', '@atlaskit/css',
27
+ // doesn't do anything because @atlaskit/css doesn't expose a styled API
28
+ '@emotion/core', '@emotion/react', '@emotion/styled'];
6
29
  export const externalRules = [{
7
30
  name: '@atlaskit/design-system/consistent-css-prop-usage',
8
31
  displayName: 'consistent-css-prop-usage',
@@ -55,7 +78,10 @@ export const externalRules = [{
55
78
  url: getRuleUrl('no-styled-tagged-template-expression'),
56
79
  externalUrl: 'https://atlassian.design/components/eslint-plugin-design-system/no-styled-tagged-template-expression/usage',
57
80
  recommended: true,
58
- severity: 'warn'
81
+ severity: 'warn',
82
+ pluginConfig: {
83
+ importSources: noStyledTaggedTemplateExpressionImportSources
84
+ }
59
85
  }
60
86
  }, {
61
87
  name: '@compiled/no-suppress-xcss',
@@ -1,9 +1,11 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::f907e3f7ab0b7145dcb8318732ae1fec>>
3
+ * @codegen <<SignedSource::4e0d0cd3ebc010cebc70fb1564055574>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import localCxXcss from './local-cx-xcss';
7
+ import noImportantStyles from './no-important-styles';
7
8
  export default {
8
- 'local-cx-xcss': localCxXcss
9
+ 'local-cx-xcss': localCxXcss,
10
+ 'no-important-styles': noImportantStyles
9
11
  };
@@ -0,0 +1,69 @@
1
+ import estraverse from 'estraverse';
2
+ import { createLintRule } from '../utils/create-rule';
3
+ import { isCss, getImportSources, isStyled, isKeyframes, isCssMap, isXcss } from '../utils/is-supported-import';
4
+ const schema = [{
5
+ type: 'object',
6
+ properties: {
7
+ importSources: {
8
+ type: 'array',
9
+ items: {
10
+ type: 'string'
11
+ },
12
+ uniqueItems: true
13
+ }
14
+ }
15
+ }];
16
+ export const rule = createLintRule({
17
+ meta: {
18
+ name: 'no-important-styles',
19
+ docs: {
20
+ description: 'Disallows important style declarations',
21
+ recommended: true,
22
+ severity: 'warn'
23
+ },
24
+ messages: {
25
+ 'no-important-styles': 'Important style declarations are disallowed. Refactor so the `!important` flag is not needed.'
26
+ },
27
+ type: 'problem',
28
+ schema
29
+ },
30
+ create(context) {
31
+ const importSources = getImportSources(context);
32
+ return {
33
+ CallExpression(node) {
34
+ const {
35
+ references
36
+ } = context.getScope();
37
+ if (isCss(node.callee, references, importSources) || isStyled(node.callee, references, importSources) || isKeyframes(node.callee, references, importSources) || isCssMap(node.callee, references, importSources) || isXcss(node.callee, references, importSources)) {
38
+ estraverse.traverse(node, {
39
+ enter(node) {
40
+ var _node$value$value;
41
+ if (node.type !== 'Property') {
42
+ return;
43
+ }
44
+ if (node.value.type !== 'Literal') {
45
+ return;
46
+ }
47
+
48
+ /**
49
+ * There can be whitespace between the `!` and the `important` keyword.
50
+ * There can also be whitespace after the `important` keyword.
51
+ */
52
+ if ((_node$value$value = node.value.value) !== null && _node$value$value !== void 0 && _node$value$value.toString().match(/!\s*important\s*$/gm)) {
53
+ context.report({
54
+ node: node.value,
55
+ messageId: 'no-important-styles'
56
+ });
57
+ }
58
+ },
59
+ /**
60
+ * This is needed to handle unknown node types. Otherwise an error is thrown.
61
+ */
62
+ fallback: 'iteration'
63
+ });
64
+ }
65
+ }
66
+ };
67
+ }
68
+ });
69
+ export default rule;
@@ -4,6 +4,7 @@ export const CSS_IN_JS_IMPORTS = {
4
4
  compiled: '@compiled/react',
5
5
  emotionReact: '@emotion/react',
6
6
  emotionCore: '@emotion/core',
7
+ emotionStyled: '@emotion/styled',
7
8
  styledComponents: 'styled-components',
8
9
  atlaskitCss: '@atlaskit/css',
9
10
  atlaskitPrimitives: '@atlaskit/primitives'
@@ -11,16 +12,30 @@ export const CSS_IN_JS_IMPORTS = {
11
12
 
12
13
  // A CSS-in-JS library an import of a valid css, cx, cssMap, etc.
13
14
  // function might originate from, e.g. @compiled/react, @emotion/core.
14
- // All ESLint rules originating from `@compiled/eslint-plugin` should apply to these libraries.
15
- export const DEFAULT_IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.compiled, CSS_IN_JS_IMPORTS.atlaskitCss];
15
+
16
+ /**
17
+ * By default all known import sources are checked against.
18
+ */
19
+ export const DEFAULT_IMPORT_SOURCES = Object.values(CSS_IN_JS_IMPORTS);
20
+ const getIdentifierNode = node => {
21
+ let identifierNode = node.type === 'Identifier' ? node : undefined;
22
+ if (!identifierNode) {
23
+ // Handles styled.div`` case
24
+ if (node.type === 'MemberExpression' && node.object.type === 'Identifier') {
25
+ identifierNode = node.object;
26
+ }
27
+
28
+ // Handles styled(Component)`` case
29
+ if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
30
+ identifierNode = node.callee;
31
+ }
32
+ }
33
+ return identifierNode;
34
+ };
16
35
 
17
36
  /**
18
37
  * Given the ESLint rule context, extract and parse the value of the importSources rule option.
19
- * The importSources option is used to define additional libraries for which an ESLint rule
20
- * should apply to.
21
- *
22
- * Note that `@compiled/react` and `@atlaskit/css` are always included in importSources, regardless
23
- * of what importSources is configured to by the user.
38
+ * The importSources option is used to override which libraries an ESLint rule applies to.
24
39
  *
25
40
  * @param context The rule context.
26
41
  * @returns An array of strings representing what CSS-in-JS packages that should be checked, based
@@ -32,11 +47,11 @@ export const getImportSources = context => {
32
47
  return DEFAULT_IMPORT_SOURCES;
33
48
  }
34
49
  if (options[0].importSources && Array.isArray(options[0].importSources)) {
35
- return [...DEFAULT_IMPORT_SOURCES, ...options[0].importSources];
50
+ return options[0].importSources;
36
51
  }
37
52
  return DEFAULT_IMPORT_SOURCES;
38
53
  };
39
- const isSupportedImportWrapper = functionName => {
54
+ const isSupportedImportWrapper = (functionName, defaultFromImportSources = []) => {
40
55
  const checkDefinitionHasImport = (def, importSources) => {
41
56
  if (def.type !== 'ImportBinding') {
42
57
  return false;
@@ -44,12 +59,19 @@ const isSupportedImportWrapper = functionName => {
44
59
  if (!def.parent || !importSources.includes(def.parent.source.value)) {
45
60
  return false;
46
61
  }
47
- return (
48
- // import { functionName } from 'import-source';
49
- def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName ||
50
- // import functionName from 'import-source';
51
- def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName
52
- );
62
+
63
+ // Matches the imported name from a named import
64
+ // import { functionName, functioName as otherName } from 'import-source';
65
+ const 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
+ const 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
+ const isKnownDefaultImport = def.node.type === 'ImportDefaultSpecifier' && defaultFromImportSources.includes(def.parent.source.value);
74
+ return isNamedImport || isDefaultImportMatchingLocal || isKnownDefaultImport;
53
75
  };
54
76
 
55
77
  /**
@@ -69,9 +91,10 @@ const isSupportedImportWrapper = functionName => {
69
91
  * @returns Whether the above conditions are true.
70
92
  */
71
93
  const isSupportedImport = (nodeToCheck, referencesInScope, importSources) => {
72
- return nodeToCheck.type === 'Identifier' && referencesInScope.some(reference => {
94
+ const identifierNode = getIdentifierNode(nodeToCheck);
95
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(reference => {
73
96
  var _reference$resolved;
74
- return reference.identifier === nodeToCheck && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(def => checkDefinitionHasImport(def, importSources)));
97
+ return reference.identifier === identifierNode && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(def => checkDefinitionHasImport(def, importSources)));
75
98
  });
76
99
  };
77
100
  return isSupportedImport;
@@ -84,4 +107,32 @@ export const isCss = isSupportedImportWrapper('css');
84
107
  export const isCxFunction = isSupportedImportWrapper('cx');
85
108
  export const isCssMap = isSupportedImportWrapper('cssMap');
86
109
  export const isKeyframes = isSupportedImportWrapper('keyframes');
87
- export const isStyled = isSupportedImportWrapper('styled');
110
+ // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
111
+ export const isStyled = isSupportedImportWrapper('styled', ['styled-components', '@emotion/styled']);
112
+ export const isXcss = isSupportedImportWrapper('xcss');
113
+ export const isImportedFrom = (moduleName, exactMatch = true) => (nodeToCheck, referencesInScope, importSources) => {
114
+ if (!importSources.includes(moduleName)) {
115
+ // Don't go through the trouble of checking the import sources does not include this
116
+ // We'll assume this is skipped elsewhere.
117
+ return false;
118
+ }
119
+ const identifierNode = getIdentifierNode(nodeToCheck);
120
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(reference => {
121
+ var _reference$resolved2;
122
+ return reference.identifier === identifierNode && ((_reference$resolved2 = reference.resolved) === null || _reference$resolved2 === void 0 ? void 0 : _reference$resolved2.defs.some(def => {
123
+ var _def$parent, _String, _def$parent2;
124
+ 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)));
125
+ }));
126
+ });
127
+ };
128
+
129
+ /**
130
+ * Determine if this node is specifically from a `'styled-components'` import.
131
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
132
+ * we need to handle them differently in a few scenarios.
133
+ *
134
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
135
+ */
136
+ export const isStyledComponents = isImportedFrom('styled-components');
137
+ export const isCompiled = isImportedFrom('@compiled/', false);
138
+ export const isEmotion = isImportedFrom('@emotion/', false);
@@ -1,18 +1,21 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::6b1418e07df6af9ad4cae4baf66739c1>>
3
+ * @codegen <<SignedSource::c0973784c8a5047835032781219885ce>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
7
7
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
10
+ '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
10
11
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
11
12
  excludeReactComponents: true
12
13
  }],
13
14
  '@atlaskit/design-system/no-css-tagged-template-expression': 'warn',
14
15
  '@atlaskit/design-system/no-keyframes-tagged-template-expression': 'warn',
15
- '@atlaskit/design-system/no-styled-tagged-template-expression': 'warn',
16
+ '@atlaskit/design-system/no-styled-tagged-template-expression': ['warn', {
17
+ importSources: ['@compiled/react', '@atlaskit/css', '@emotion/core', '@emotion/react', '@emotion/styled']
18
+ }],
16
19
  '@compiled/no-suppress-xcss': 'warn',
17
20
  '@compiled/no-js-xcss': 'warn',
18
21
  '@atlaskit/design-system/no-empty-styled-expression': ['warn', {
@@ -1,18 +1,21 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::6b1418e07df6af9ad4cae4baf66739c1>>
3
+ * @codegen <<SignedSource::c0973784c8a5047835032781219885ce>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
7
7
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
10
+ '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
10
11
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
11
12
  excludeReactComponents: true
12
13
  }],
13
14
  '@atlaskit/design-system/no-css-tagged-template-expression': 'warn',
14
15
  '@atlaskit/design-system/no-keyframes-tagged-template-expression': 'warn',
15
- '@atlaskit/design-system/no-styled-tagged-template-expression': 'warn',
16
+ '@atlaskit/design-system/no-styled-tagged-template-expression': ['warn', {
17
+ importSources: ['@compiled/react', '@atlaskit/css', '@emotion/core', '@emotion/react', '@emotion/styled']
18
+ }],
16
19
  '@compiled/no-suppress-xcss': 'warn',
17
20
  '@compiled/no-js-xcss': 'warn',
18
21
  '@atlaskit/design-system/no-empty-styled-expression': ['warn', {
@@ -3,6 +3,29 @@ import { getRuleUrl } from './utils/create-rule';
3
3
  /** External rules must be scoped, have a display name, and external urls. */
4
4
 
5
5
  var importSources = ['@compiled/react', '@atlaskit/css', '@emotion/core', '@emotion/react', '@emotion/styled', 'styled-components'];
6
+
7
+ /**
8
+ * Same as importSources, but excludes `styled-components` because the type-checking
9
+ * for `styled-components` does not support the `no-styled-tagged-template-expression`
10
+ * lint output for this:
11
+ *
12
+ * export const Container = styled(Section)`
13
+ * max-height: ${gridSize * 46}px;
14
+ * overflow: auto;
15
+ * `;
16
+ *
17
+ * export const ErrorStateContainer = styled(Container)`
18
+ * margin: ${
19
+ * (props: any) =>
20
+ * props.canManageAutomations // Use negative margin to reduce whitespace when the Footer component is shown
21
+ * ? `${gridSize * -4}px ${gridSize * 2}px`
22
+ * : `0 ${gridSize * 2}px`
23
+ * };
24
+ * `;
25
+ */
26
+ var noStyledTaggedTemplateExpressionImportSources = ['@compiled/react', '@atlaskit/css',
27
+ // doesn't do anything because @atlaskit/css doesn't expose a styled API
28
+ '@emotion/core', '@emotion/react', '@emotion/styled'];
6
29
  export var externalRules = [{
7
30
  name: '@atlaskit/design-system/consistent-css-prop-usage',
8
31
  displayName: 'consistent-css-prop-usage',
@@ -55,7 +78,10 @@ export var externalRules = [{
55
78
  url: getRuleUrl('no-styled-tagged-template-expression'),
56
79
  externalUrl: 'https://atlassian.design/components/eslint-plugin-design-system/no-styled-tagged-template-expression/usage',
57
80
  recommended: true,
58
- severity: 'warn'
81
+ severity: 'warn',
82
+ pluginConfig: {
83
+ importSources: noStyledTaggedTemplateExpressionImportSources
84
+ }
59
85
  }
60
86
  }, {
61
87
  name: '@compiled/no-suppress-xcss',
@@ -1,9 +1,11 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::f907e3f7ab0b7145dcb8318732ae1fec>>
3
+ * @codegen <<SignedSource::4e0d0cd3ebc010cebc70fb1564055574>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import localCxXcss from './local-cx-xcss';
7
+ import noImportantStyles from './no-important-styles';
7
8
  export default {
8
- 'local-cx-xcss': localCxXcss
9
+ 'local-cx-xcss': localCxXcss,
10
+ 'no-important-styles': noImportantStyles
9
11
  };
@@ -0,0 +1,68 @@
1
+ import estraverse from 'estraverse';
2
+ import { createLintRule } from '../utils/create-rule';
3
+ import { isCss, getImportSources, isStyled, isKeyframes, isCssMap, isXcss } from '../utils/is-supported-import';
4
+ var schema = [{
5
+ type: 'object',
6
+ properties: {
7
+ importSources: {
8
+ type: 'array',
9
+ items: {
10
+ type: 'string'
11
+ },
12
+ uniqueItems: true
13
+ }
14
+ }
15
+ }];
16
+ export var rule = createLintRule({
17
+ meta: {
18
+ name: 'no-important-styles',
19
+ docs: {
20
+ description: 'Disallows important style declarations',
21
+ recommended: true,
22
+ severity: 'warn'
23
+ },
24
+ messages: {
25
+ 'no-important-styles': 'Important style declarations are disallowed. Refactor so the `!important` flag is not needed.'
26
+ },
27
+ type: 'problem',
28
+ schema: schema
29
+ },
30
+ create: function create(context) {
31
+ var importSources = getImportSources(context);
32
+ return {
33
+ CallExpression: function CallExpression(node) {
34
+ var _context$getScope = context.getScope(),
35
+ references = _context$getScope.references;
36
+ if (isCss(node.callee, references, importSources) || isStyled(node.callee, references, importSources) || isKeyframes(node.callee, references, importSources) || isCssMap(node.callee, references, importSources) || isXcss(node.callee, references, importSources)) {
37
+ estraverse.traverse(node, {
38
+ enter: function enter(node) {
39
+ var _node$value$value;
40
+ if (node.type !== 'Property') {
41
+ return;
42
+ }
43
+ if (node.value.type !== 'Literal') {
44
+ return;
45
+ }
46
+
47
+ /**
48
+ * There can be whitespace between the `!` and the `important` keyword.
49
+ * There can also be whitespace after the `important` keyword.
50
+ */
51
+ if ((_node$value$value = node.value.value) !== null && _node$value$value !== void 0 && _node$value$value.toString().match(/!\s*important\s*$/gm)) {
52
+ context.report({
53
+ node: node.value,
54
+ messageId: 'no-important-styles'
55
+ });
56
+ }
57
+ },
58
+ /**
59
+ * This is needed to handle unknown node types. Otherwise an error is thrown.
60
+ */
61
+ fallback: 'iteration'
62
+ });
63
+ }
64
+ }
65
+ };
66
+ }
67
+ });
68
+ export default rule;
@@ -1,10 +1,10 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
1
  // eslint-disable-next-line import/no-extraneous-dependencies
3
2
 
4
3
  export var CSS_IN_JS_IMPORTS = {
5
4
  compiled: '@compiled/react',
6
5
  emotionReact: '@emotion/react',
7
6
  emotionCore: '@emotion/core',
7
+ emotionStyled: '@emotion/styled',
8
8
  styledComponents: 'styled-components',
9
9
  atlaskitCss: '@atlaskit/css',
10
10
  atlaskitPrimitives: '@atlaskit/primitives'
@@ -12,16 +12,30 @@ export var CSS_IN_JS_IMPORTS = {
12
12
 
13
13
  // A CSS-in-JS library an import of a valid css, cx, cssMap, etc.
14
14
  // function might originate from, e.g. @compiled/react, @emotion/core.
15
- // All ESLint rules originating from `@compiled/eslint-plugin` should apply to these libraries.
16
- export var DEFAULT_IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.compiled, CSS_IN_JS_IMPORTS.atlaskitCss];
15
+
16
+ /**
17
+ * By default all known import sources are checked against.
18
+ */
19
+ export var DEFAULT_IMPORT_SOURCES = Object.values(CSS_IN_JS_IMPORTS);
20
+ var getIdentifierNode = function getIdentifierNode(node) {
21
+ var identifierNode = node.type === 'Identifier' ? node : undefined;
22
+ if (!identifierNode) {
23
+ // Handles styled.div`` case
24
+ if (node.type === 'MemberExpression' && node.object.type === 'Identifier') {
25
+ identifierNode = node.object;
26
+ }
27
+
28
+ // Handles styled(Component)`` case
29
+ if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
30
+ identifierNode = node.callee;
31
+ }
32
+ }
33
+ return identifierNode;
34
+ };
17
35
 
18
36
  /**
19
37
  * Given the ESLint rule context, extract and parse the value of the importSources rule option.
20
- * The importSources option is used to define additional libraries for which an ESLint rule
21
- * should apply to.
22
- *
23
- * Note that `@compiled/react` and `@atlaskit/css` are always included in importSources, regardless
24
- * of what importSources is configured to by the user.
38
+ * The importSources option is used to override which libraries an ESLint rule applies to.
25
39
  *
26
40
  * @param context The rule context.
27
41
  * @returns An array of strings representing what CSS-in-JS packages that should be checked, based
@@ -33,11 +47,12 @@ export var getImportSources = function getImportSources(context) {
33
47
  return DEFAULT_IMPORT_SOURCES;
34
48
  }
35
49
  if (options[0].importSources && Array.isArray(options[0].importSources)) {
36
- return [].concat(DEFAULT_IMPORT_SOURCES, _toConsumableArray(options[0].importSources));
50
+ return options[0].importSources;
37
51
  }
38
52
  return DEFAULT_IMPORT_SOURCES;
39
53
  };
40
54
  var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
55
+ var defaultFromImportSources = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
41
56
  var checkDefinitionHasImport = function checkDefinitionHasImport(def, importSources) {
42
57
  if (def.type !== 'ImportBinding') {
43
58
  return false;
@@ -45,12 +60,19 @@ var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
45
60
  if (!def.parent || !importSources.includes(def.parent.source.value)) {
46
61
  return false;
47
62
  }
48
- return (
49
- // import { functionName } from 'import-source';
50
- def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName ||
51
- // import functionName from 'import-source';
52
- def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName
53
- );
63
+
64
+ // Matches the imported name from a named import
65
+ // import { functionName, functioName as otherName } from 'import-source';
66
+ var isNamedImport = def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName;
67
+
68
+ // Must explicitly match the local name from a default import
69
+ // import functionName from 'import-source';
70
+ var isDefaultImportMatchingLocal = def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName;
71
+
72
+ // Can match any local name from a default import
73
+ // import anything from 'import-source'
74
+ var isKnownDefaultImport = def.node.type === 'ImportDefaultSpecifier' && defaultFromImportSources.includes(def.parent.source.value);
75
+ return isNamedImport || isDefaultImportMatchingLocal || isKnownDefaultImport;
54
76
  };
55
77
 
56
78
  /**
@@ -70,9 +92,10 @@ var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
70
92
  * @returns Whether the above conditions are true.
71
93
  */
72
94
  var isSupportedImport = function isSupportedImport(nodeToCheck, referencesInScope, importSources) {
73
- return nodeToCheck.type === 'Identifier' && referencesInScope.some(function (reference) {
95
+ var identifierNode = getIdentifierNode(nodeToCheck);
96
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(function (reference) {
74
97
  var _reference$resolved;
75
- return reference.identifier === nodeToCheck && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(function (def) {
98
+ return reference.identifier === identifierNode && ((_reference$resolved = reference.resolved) === null || _reference$resolved === void 0 ? void 0 : _reference$resolved.defs.some(function (def) {
76
99
  return checkDefinitionHasImport(def, importSources);
77
100
  }));
78
101
  });
@@ -87,4 +110,35 @@ export var isCss = isSupportedImportWrapper('css');
87
110
  export var isCxFunction = isSupportedImportWrapper('cx');
88
111
  export var isCssMap = isSupportedImportWrapper('cssMap');
89
112
  export var isKeyframes = isSupportedImportWrapper('keyframes');
90
- export var isStyled = isSupportedImportWrapper('styled');
113
+ // `styled` is also the explicit default of `styled-components` and `@emotion/styled`, so we also match on default imports generally
114
+ export var isStyled = isSupportedImportWrapper('styled', ['styled-components', '@emotion/styled']);
115
+ export var isXcss = isSupportedImportWrapper('xcss');
116
+ export var isImportedFrom = function isImportedFrom(moduleName) {
117
+ var exactMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
118
+ return function (nodeToCheck, referencesInScope, importSources) {
119
+ if (!importSources.includes(moduleName)) {
120
+ // Don't go through the trouble of checking the import sources does not include this
121
+ // We'll assume this is skipped elsewhere.
122
+ return false;
123
+ }
124
+ var identifierNode = getIdentifierNode(nodeToCheck);
125
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(function (reference) {
126
+ var _reference$resolved2;
127
+ return reference.identifier === identifierNode && ((_reference$resolved2 = reference.resolved) === null || _reference$resolved2 === void 0 ? void 0 : _reference$resolved2.defs.some(function (def) {
128
+ var _def$parent, _String, _def$parent2;
129
+ 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)));
130
+ }));
131
+ });
132
+ };
133
+ };
134
+
135
+ /**
136
+ * Determine if this node is specifically from a `'styled-components'` import.
137
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
138
+ * we need to handle them differently in a few scenarios.
139
+ *
140
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
141
+ */
142
+ export var isStyledComponents = isImportedFrom('styled-components');
143
+ export var isCompiled = isImportedFrom('@compiled/', false);
144
+ export var isEmotion = isImportedFrom('@emotion/', false);
@@ -4,12 +4,15 @@ export declare const configs: {
4
4
  plugins: string[];
5
5
  rules: {
6
6
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
7
+ '@atlaskit/ui-styling-standard/no-important-styles': string;
7
8
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
8
9
  excludeReactComponents: boolean;
9
10
  })[];
10
11
  '@atlaskit/design-system/no-css-tagged-template-expression': string;
11
12
  '@atlaskit/design-system/no-keyframes-tagged-template-expression': string;
12
- '@atlaskit/design-system/no-styled-tagged-template-expression': string;
13
+ '@atlaskit/design-system/no-styled-tagged-template-expression': (string | {
14
+ importSources: string[];
15
+ })[];
13
16
  '@compiled/no-suppress-xcss': string;
14
17
  '@compiled/no-js-xcss': string;
15
18
  '@atlaskit/design-system/no-empty-styled-expression': (string | {
@@ -30,12 +33,15 @@ export declare const configs: {
30
33
  plugins: string[];
31
34
  rules: {
32
35
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
36
+ '@atlaskit/ui-styling-standard/no-important-styles': string;
33
37
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
34
38
  excludeReactComponents: boolean;
35
39
  })[];
36
40
  '@atlaskit/design-system/no-css-tagged-template-expression': string;
37
41
  '@atlaskit/design-system/no-keyframes-tagged-template-expression': string;
38
- '@atlaskit/design-system/no-styled-tagged-template-expression': string;
42
+ '@atlaskit/design-system/no-styled-tagged-template-expression': (string | {
43
+ importSources: string[];
44
+ })[];
39
45
  '@compiled/no-suppress-xcss': string;
40
46
  '@compiled/no-js-xcss': string;
41
47
  '@atlaskit/design-system/no-empty-styled-expression': (string | {
@@ -1,18 +1,21 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::6b1418e07df6af9ad4cae4baf66739c1>>
3
+ * @codegen <<SignedSource::c0973784c8a5047835032781219885ce>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  declare const _default: {
7
7
  plugins: string[];
8
8
  rules: {
9
9
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
10
+ '@atlaskit/ui-styling-standard/no-important-styles': string;
10
11
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
11
12
  excludeReactComponents: boolean;
12
13
  })[];
13
14
  '@atlaskit/design-system/no-css-tagged-template-expression': string;
14
15
  '@atlaskit/design-system/no-keyframes-tagged-template-expression': string;
15
- '@atlaskit/design-system/no-styled-tagged-template-expression': string;
16
+ '@atlaskit/design-system/no-styled-tagged-template-expression': (string | {
17
+ importSources: string[];
18
+ })[];
16
19
  '@compiled/no-suppress-xcss': string;
17
20
  '@compiled/no-js-xcss': string;
18
21
  '@atlaskit/design-system/no-empty-styled-expression': (string | {