@atlaskit/eslint-plugin-ui-styling-standard 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -0
  3. package/constellation/index/overview.mdx +1 -0
  4. package/constellation/no-important-styles/usage.mdx +29 -0
  5. package/dist/cjs/presets/all.codegen.js +2 -1
  6. package/dist/cjs/presets/recommended.codegen.js +2 -1
  7. package/dist/cjs/rules/index.codegen.js +4 -2
  8. package/dist/cjs/rules/no-important-styles/index.js +75 -0
  9. package/dist/cjs/rules/utils/is-supported-import.js +72 -20
  10. package/dist/es2019/presets/all.codegen.js +2 -1
  11. package/dist/es2019/presets/recommended.codegen.js +2 -1
  12. package/dist/es2019/rules/index.codegen.js +4 -2
  13. package/dist/es2019/rules/no-important-styles/index.js +69 -0
  14. package/dist/es2019/rules/utils/is-supported-import.js +69 -18
  15. package/dist/esm/presets/all.codegen.js +2 -1
  16. package/dist/esm/presets/recommended.codegen.js +2 -1
  17. package/dist/esm/rules/index.codegen.js +4 -2
  18. package/dist/esm/rules/no-important-styles/index.js +68 -0
  19. package/dist/esm/rules/utils/is-supported-import.js +72 -18
  20. package/dist/types/index.codegen.d.ts +2 -0
  21. package/dist/types/presets/all.codegen.d.ts +2 -1
  22. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  23. package/dist/types/rules/index.codegen.d.ts +1 -0
  24. package/dist/types/rules/no-important-styles/index.d.ts +2 -0
  25. package/dist/types/rules/utils/is-supported-import.d.ts +17 -5
  26. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  27. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  28. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  29. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  30. package/dist/types-ts4.5/rules/no-important-styles/index.d.ts +2 -0
  31. package/dist/types-ts4.5/rules/utils/is-supported-import.d.ts +17 -5
  32. package/package.json +6 -4
  33. package/src/presets/all.codegen.tsx +2 -1
  34. package/src/presets/recommended.codegen.tsx +2 -1
  35. package/src/rules/__tests__/utils/_tester.tsx +1 -1
  36. package/src/rules/index.codegen.tsx +3 -1
  37. package/src/rules/no-important-styles/README.md +27 -0
  38. package/src/rules/no-important-styles/__tests__/rule.test.tsx +140 -0
  39. package/src/rules/no-important-styles/index.tsx +88 -0
  40. package/src/rules/utils/is-supported-import.tsx +99 -20
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 0.5.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#81166](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/81166) [`a249a1bd29a6`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/a249a1bd29a6) - Upgrade ESLint to version 8
8
+
9
+ ## 0.5.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#80523](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/80523) [`1fbc903e5b7b`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/1fbc903e5b7b) - Adds `no-important-styles` rule which prevents the use of `!important` flags in style declarations.
14
+
3
15
  ## 0.4.1
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -14,6 +14,7 @@ Not intended for use outside the Atlassian frontend repository.
14
14
  | Rule | Description | Recommended | Fixable | Suggestions |
15
15
  | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------- | ----------- |
16
16
  | <a href="./src/rules/local-cx-xcss/README.md">local-cx-xcss</a> | Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element. | Yes | | |
17
+ | <a href="./src/rules/no-important-styles/README.md">no-important-styles</a> | Disallows important style declarations | Yes | | |
17
18
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/consistent-css-prop-usage/usage">@atlaskit/design-system/consistent-css-prop-usage</a> | Ensures consistency with `css` and `xcss` prop usages | Yes | Yes | |
18
19
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/no-css-tagged-template-expression/usage">@atlaskit/design-system/no-css-tagged-template-expression</a> | Disallows any `css` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
19
20
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/no-keyframes-tagged-template-expression/usage">@atlaskit/design-system/no-keyframes-tagged-template-expression</a> | Disallows any `keyframe` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
@@ -12,6 +12,7 @@ but more are planned to support the standard over time.
12
12
  | Rule | Description | Recommended | Fixable | Suggestions |
13
13
  | --------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- | ------- | ----------- |
14
14
  | <a href="local-cx-xcss/usage">local-cx-xcss</a> | Ensures the cx() function, which is part of the XCSS API, is only used within the xcss prop. This aids tracking what styles are applied to a jsx element. | Yes | | |
15
+ | <a href="no-important-styles/usage">no-important-styles</a> | Disallows important style declarations | Yes | | |
15
16
  | <a href="consistent-css-prop-usage/usage">@atlaskit/design-system/consistent-css-prop-usage</a> | Ensures consistency with `css` and `xcss` prop usages | Yes | Yes | |
16
17
  | <a href="no-css-tagged-template-expression/usage">@atlaskit/design-system/no-css-tagged-template-expression</a> | Disallows any `css` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
17
18
  | <a href="no-keyframes-tagged-template-expression/usage">@atlaskit/design-system/no-keyframes-tagged-template-expression</a> | Disallows any `keyframe` tagged template expressions that originate from Emotion, Styled Components or Compiled | Yes | Yes | |
@@ -0,0 +1,29 @@
1
+ # no-important-styles
2
+
3
+ This rule prevents the usage of the `!important` flag in style declarations.
4
+
5
+ Properly composed styles will never need an `!important` flag. It is used to override specificity when working across different scopes, and the UI Styling Standard enforces styles with minimal scope.
6
+
7
+ The use of `!important` is a code smell and greatly impacts readability and determinism.
8
+
9
+ ## Examples
10
+
11
+ ### Incorrect
12
+
13
+ ```js
14
+ import { css } from '@compiled/react';
15
+
16
+ const styles = css({
17
+ color: 'red!important',
18
+ });
19
+ ```
20
+
21
+ ### Correct
22
+
23
+ ```js
24
+ import { css } from '@compiled/react';
25
+
26
+ const styles = css({
27
+ color: 'red',
28
+ });
29
+ ```
@@ -6,13 +6,14 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  /**
8
8
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
9
- * @codegen <<SignedSource::9869b1757ca915f74b471fe04f72e5ea>>
9
+ * @codegen <<SignedSource::c0973784c8a5047835032781219885ce>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
13
13
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
14
14
  rules: {
15
15
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
16
+ '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
16
17
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
17
18
  excludeReactComponents: true
18
19
  }],
@@ -6,13 +6,14 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  /**
8
8
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
9
- * @codegen <<SignedSource::9869b1757ca915f74b471fe04f72e5ea>>
9
+ * @codegen <<SignedSource::c0973784c8a5047835032781219885ce>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
13
13
  plugins: ['@atlaskit/ui-styling-standard', '@atlaskit/design-system', '@compiled'],
14
14
  rules: {
15
15
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
16
+ '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
16
17
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
17
18
  excludeReactComponents: true
18
19
  }],
@@ -6,11 +6,13 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.default = void 0;
8
8
  var _localCxXcss = _interopRequireDefault(require("./local-cx-xcss"));
9
+ var _noImportantStyles = _interopRequireDefault(require("./no-important-styles"));
9
10
  /**
10
11
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
11
- * @codegen <<SignedSource::f907e3f7ab0b7145dcb8318732ae1fec>>
12
+ * @codegen <<SignedSource::4e0d0cd3ebc010cebc70fb1564055574>>
12
13
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
13
14
  */
14
15
  var _default = exports.default = {
15
- 'local-cx-xcss': _localCxXcss.default
16
+ 'local-cx-xcss': _localCxXcss.default,
17
+ 'no-important-styles': _noImportantStyles.default
16
18
  };
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.rule = exports.default = void 0;
8
+ var _estraverse = _interopRequireDefault(require("estraverse"));
9
+ var _createRule = require("../utils/create-rule");
10
+ var _isSupportedImport = require("../utils/is-supported-import");
11
+ var schema = [{
12
+ type: 'object',
13
+ properties: {
14
+ importSources: {
15
+ type: 'array',
16
+ items: {
17
+ type: 'string'
18
+ },
19
+ uniqueItems: true
20
+ }
21
+ }
22
+ }];
23
+ var rule = exports.rule = (0, _createRule.createLintRule)({
24
+ meta: {
25
+ name: 'no-important-styles',
26
+ docs: {
27
+ description: 'Disallows important style declarations',
28
+ recommended: true,
29
+ severity: 'warn'
30
+ },
31
+ messages: {
32
+ 'no-important-styles': 'Important style declarations are disallowed. Refactor so the `!important` flag is not needed.'
33
+ },
34
+ type: 'problem',
35
+ schema: schema
36
+ },
37
+ create: function create(context) {
38
+ var importSources = (0, _isSupportedImport.getImportSources)(context);
39
+ return {
40
+ CallExpression: function CallExpression(node) {
41
+ var _context$getScope = context.getScope(),
42
+ references = _context$getScope.references;
43
+ if ((0, _isSupportedImport.isCss)(node.callee, references, importSources) || (0, _isSupportedImport.isStyled)(node.callee, references, importSources) || (0, _isSupportedImport.isKeyframes)(node.callee, references, importSources) || (0, _isSupportedImport.isCssMap)(node.callee, references, importSources) || (0, _isSupportedImport.isXcss)(node.callee, references, importSources)) {
44
+ _estraverse.default.traverse(node, {
45
+ enter: function enter(node) {
46
+ var _node$value$value;
47
+ if (node.type !== 'Property') {
48
+ return;
49
+ }
50
+ if (node.value.type !== 'Literal') {
51
+ return;
52
+ }
53
+
54
+ /**
55
+ * There can be whitespace between the `!` and the `important` keyword.
56
+ * There can also be whitespace after the `important` keyword.
57
+ */
58
+ if ((_node$value$value = node.value.value) !== null && _node$value$value !== void 0 && _node$value$value.toString().match(/!\s*important\s*$/gm)) {
59
+ context.report({
60
+ node: node.value,
61
+ messageId: 'no-important-styles'
62
+ });
63
+ }
64
+ },
65
+ /**
66
+ * This is needed to handle unknown node types. Otherwise an error is thrown.
67
+ */
68
+ fallback: 'iteration'
69
+ });
70
+ }
71
+ }
72
+ };
73
+ }
74
+ });
75
+ var _default = exports.default = rule;
@@ -1,17 +1,16 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
3
  Object.defineProperty(exports, "__esModule", {
5
4
  value: true
6
5
  });
7
- exports.isStyled = exports.isKeyframes = exports.isCxFunction = exports.isCssMap = exports.isCss = exports.getImportSources = exports.DEFAULT_IMPORT_SOURCES = exports.CSS_IN_JS_IMPORTS = void 0;
8
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
6
+ exports.isXcss = exports.isStyledComponents = exports.isStyled = exports.isKeyframes = exports.isImportedFrom = exports.isEmotion = exports.isCxFunction = exports.isCssMap = exports.isCss = exports.isCompiled = exports.getImportSources = exports.DEFAULT_IMPORT_SOURCES = exports.CSS_IN_JS_IMPORTS = void 0;
9
7
  // eslint-disable-next-line import/no-extraneous-dependencies
10
8
 
11
9
  var CSS_IN_JS_IMPORTS = exports.CSS_IN_JS_IMPORTS = {
12
10
  compiled: '@compiled/react',
13
11
  emotionReact: '@emotion/react',
14
12
  emotionCore: '@emotion/core',
13
+ emotionStyled: '@emotion/styled',
15
14
  styledComponents: 'styled-components',
16
15
  atlaskitCss: '@atlaskit/css',
17
16
  atlaskitPrimitives: '@atlaskit/primitives'
@@ -20,16 +19,29 @@ var CSS_IN_JS_IMPORTS = exports.CSS_IN_JS_IMPORTS = {
20
19
  // A CSS-in-JS library an import of a valid css, cx, cssMap, etc.
21
20
  // function might originate from, e.g. @compiled/react, @emotion/core.
22
21
 
23
- // All ESLint rules originating from `@compiled/eslint-plugin` should apply to these libraries.
24
- var DEFAULT_IMPORT_SOURCES = exports.DEFAULT_IMPORT_SOURCES = [CSS_IN_JS_IMPORTS.compiled, CSS_IN_JS_IMPORTS.atlaskitCss];
22
+ /**
23
+ * By default all known import sources are checked against.
24
+ */
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
+ };
25
41
 
26
42
  /**
27
43
  * Given the ESLint rule context, extract and parse the value of the importSources rule option.
28
- * The importSources option is used to define additional libraries for which an ESLint rule
29
- * should apply to.
30
- *
31
- * Note that `@compiled/react` and `@atlaskit/css` are always included in importSources, regardless
32
- * of what importSources is configured to by the user.
44
+ * The importSources option is used to override which libraries an ESLint rule applies to.
33
45
  *
34
46
  * @param context The rule context.
35
47
  * @returns An array of strings representing what CSS-in-JS packages that should be checked, based
@@ -41,11 +53,12 @@ var getImportSources = exports.getImportSources = function getImportSources(cont
41
53
  return DEFAULT_IMPORT_SOURCES;
42
54
  }
43
55
  if (options[0].importSources && Array.isArray(options[0].importSources)) {
44
- return [].concat(DEFAULT_IMPORT_SOURCES, (0, _toConsumableArray2.default)(options[0].importSources));
56
+ return options[0].importSources;
45
57
  }
46
58
  return DEFAULT_IMPORT_SOURCES;
47
59
  };
48
60
  var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
61
+ var defaultFromImportSources = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
49
62
  var checkDefinitionHasImport = function checkDefinitionHasImport(def, importSources) {
50
63
  if (def.type !== 'ImportBinding') {
51
64
  return false;
@@ -53,12 +66,19 @@ var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
53
66
  if (!def.parent || !importSources.includes(def.parent.source.value)) {
54
67
  return false;
55
68
  }
56
- return (
57
- // import { functionName } from 'import-source';
58
- def.node.type === 'ImportSpecifier' && def.node.imported.name === functionName ||
59
- // import functionName from 'import-source';
60
- def.node.type === 'ImportDefaultSpecifier' && def.node.local.name === functionName
61
- );
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;
62
82
  };
63
83
 
64
84
  /**
@@ -78,9 +98,10 @@ var isSupportedImportWrapper = function isSupportedImportWrapper(functionName) {
78
98
  * @returns Whether the above conditions are true.
79
99
  */
80
100
  var isSupportedImport = function isSupportedImport(nodeToCheck, referencesInScope, importSources) {
81
- 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) {
82
103
  var _reference$resolved;
83
- 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) {
84
105
  return checkDefinitionHasImport(def, importSources);
85
106
  }));
86
107
  });
@@ -95,4 +116,35 @@ var isCss = exports.isCss = isSupportedImportWrapper('css');
95
116
  var isCxFunction = exports.isCxFunction = isSupportedImportWrapper('cx');
96
117
  var isCssMap = exports.isCssMap = isSupportedImportWrapper('cssMap');
97
118
  var isKeyframes = exports.isKeyframes = isSupportedImportWrapper('keyframes');
98
- 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 isXcss = exports.isXcss = isSupportedImportWrapper('xcss');
122
+ var isImportedFrom = exports.isImportedFrom = function isImportedFrom(moduleName) {
123
+ var exactMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
124
+ return function (nodeToCheck, referencesInScope, importSources) {
125
+ if (!importSources.includes(moduleName)) {
126
+ // Don't go through the trouble of checking the import sources does not include this
127
+ // We'll assume this is skipped elsewhere.
128
+ return false;
129
+ }
130
+ var identifierNode = getIdentifierNode(nodeToCheck);
131
+ return (identifierNode === null || identifierNode === void 0 ? void 0 : identifierNode.type) === 'Identifier' && referencesInScope.some(function (reference) {
132
+ var _reference$resolved2;
133
+ return reference.identifier === identifierNode && ((_reference$resolved2 = reference.resolved) === null || _reference$resolved2 === void 0 ? void 0 : _reference$resolved2.defs.some(function (def) {
134
+ var _def$parent, _String, _def$parent2;
135
+ 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)));
136
+ }));
137
+ });
138
+ };
139
+ };
140
+
141
+ /**
142
+ * Determine if this node is specifically from a `'styled-components'` import.
143
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
144
+ * we need to handle them differently in a few scenarios.
145
+ *
146
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
147
+ */
148
+ var isStyledComponents = exports.isStyledComponents = isImportedFrom('styled-components');
149
+ var isCompiled = exports.isCompiled = isImportedFrom('@compiled/', false);
150
+ var isEmotion = exports.isEmotion = isImportedFrom('@emotion/', false);
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::9869b1757ca915f74b471fe04f72e5ea>>
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
  }],
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::9869b1757ca915f74b471fe04f72e5ea>>
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
  }],
@@ -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,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::9869b1757ca915f74b471fe04f72e5ea>>
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
  }],
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
3
- * @codegen <<SignedSource::9869b1757ca915f74b471fe04f72e5ea>>
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
  }],
@@ -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
  };