@atlaskit/eslint-plugin-ui-styling-standard 0.4.1 → 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 (39) hide show
  1. package/CHANGELOG.md +6 -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 +5 -3
  33. package/src/presets/all.codegen.tsx +2 -1
  34. package/src/presets/recommended.codegen.tsx +2 -1
  35. package/src/rules/index.codegen.tsx +3 -1
  36. package/src/rules/no-important-styles/README.md +27 -0
  37. package/src/rules/no-important-styles/__tests__/rule.test.tsx +140 -0
  38. package/src/rules/no-important-styles/index.tsx +88 -0
  39. package/src/rules/utils/is-supported-import.tsx +99 -20
@@ -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,6 +4,7 @@ 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
  })[];
@@ -32,6 +33,7 @@ export declare const configs: {
32
33
  plugins: string[];
33
34
  rules: {
34
35
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
36
+ '@atlaskit/ui-styling-standard/no-important-styles': string;
35
37
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
36
38
  excludeReactComponents: boolean;
37
39
  })[];
@@ -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
  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
  })[];
@@ -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
  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
  })[];
@@ -1,4 +1,5 @@
1
1
  declare const _default: {
2
2
  'local-cx-xcss': import("eslint").Rule.RuleModule;
3
+ 'no-important-styles': import("eslint").Rule.RuleModule;
3
4
  };
4
5
  export default _default;
@@ -0,0 +1,2 @@
1
+ export declare const rule: import("eslint").Rule.RuleModule;
2
+ export default rule;
@@ -6,20 +6,20 @@ export declare const CSS_IN_JS_IMPORTS: {
6
6
  readonly compiled: "@compiled/react";
7
7
  readonly emotionReact: "@emotion/react";
8
8
  readonly emotionCore: "@emotion/core";
9
+ readonly emotionStyled: "@emotion/styled";
9
10
  readonly styledComponents: "styled-components";
10
11
  readonly atlaskitCss: "@atlaskit/css";
11
12
  readonly atlaskitPrimitives: "@atlaskit/primitives";
12
13
  };
13
14
  export type ImportSource = string;
14
15
  export type SupportedNameChecker = (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
16
+ /**
17
+ * By default all known import sources are checked against.
18
+ */
15
19
  export declare const DEFAULT_IMPORT_SOURCES: ImportSource[];
16
20
  /**
17
21
  * Given the ESLint rule context, extract and parse the value of the importSources rule option.
18
- * The importSources option is used to define additional libraries for which an ESLint rule
19
- * should apply to.
20
- *
21
- * Note that `@compiled/react` and `@atlaskit/css` are always included in importSources, regardless
22
- * of what importSources is configured to by the user.
22
+ * The importSources option is used to override which libraries an ESLint rule applies to.
23
23
  *
24
24
  * @param context The rule context.
25
25
  * @returns An array of strings representing what CSS-in-JS packages that should be checked, based
@@ -31,4 +31,16 @@ 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 isXcss: SupportedNameChecker;
35
+ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
36
+ /**
37
+ * Determine if this node is specifically from a `'styled-components'` import.
38
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
39
+ * we need to handle them differently in a few scenarios.
40
+ *
41
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
42
+ */
43
+ export declare const isStyledComponents: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
44
+ export declare const isCompiled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
45
+ export declare const isEmotion: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
34
46
  export {};
@@ -4,6 +4,7 @@ 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
  })[];
@@ -32,6 +33,7 @@ export declare const configs: {
32
33
  plugins: string[];
33
34
  rules: {
34
35
  '@atlaskit/ui-styling-standard/local-cx-xcss': string;
36
+ '@atlaskit/ui-styling-standard/no-important-styles': string;
35
37
  '@atlaskit/design-system/consistent-css-prop-usage': (string | {
36
38
  excludeReactComponents: boolean;
37
39
  })[];
@@ -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
  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
  })[];
@@ -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
  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
  })[];
@@ -1,4 +1,5 @@
1
1
  declare const _default: {
2
2
  'local-cx-xcss': import("eslint").Rule.RuleModule;
3
+ 'no-important-styles': import("eslint").Rule.RuleModule;
3
4
  };
4
5
  export default _default;
@@ -0,0 +1,2 @@
1
+ export declare const rule: import("eslint").Rule.RuleModule;
2
+ export default rule;
@@ -6,20 +6,20 @@ export declare const CSS_IN_JS_IMPORTS: {
6
6
  readonly compiled: "@compiled/react";
7
7
  readonly emotionReact: "@emotion/react";
8
8
  readonly emotionCore: "@emotion/core";
9
+ readonly emotionStyled: "@emotion/styled";
9
10
  readonly styledComponents: "styled-components";
10
11
  readonly atlaskitCss: "@atlaskit/css";
11
12
  readonly atlaskitPrimitives: "@atlaskit/primitives";
12
13
  };
13
14
  export type ImportSource = string;
14
15
  export type SupportedNameChecker = (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
16
+ /**
17
+ * By default all known import sources are checked against.
18
+ */
15
19
  export declare const DEFAULT_IMPORT_SOURCES: ImportSource[];
16
20
  /**
17
21
  * Given the ESLint rule context, extract and parse the value of the importSources rule option.
18
- * The importSources option is used to define additional libraries for which an ESLint rule
19
- * should apply to.
20
- *
21
- * Note that `@compiled/react` and `@atlaskit/css` are always included in importSources, regardless
22
- * of what importSources is configured to by the user.
22
+ * The importSources option is used to override which libraries an ESLint rule applies to.
23
23
  *
24
24
  * @param context The rule context.
25
25
  * @returns An array of strings representing what CSS-in-JS packages that should be checked, based
@@ -31,4 +31,16 @@ 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 isXcss: SupportedNameChecker;
35
+ export declare const isImportedFrom: (moduleName: string, exactMatch?: boolean) => (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
36
+ /**
37
+ * Determine if this node is specifically from a `'styled-components'` import.
38
+ * This is because `styled-components@3.4` APIs are not consistent with Emotion and Compiled,
39
+ * we need to handle them differently in a few scenarios.
40
+ *
41
+ * This can be cleaned up when `'styled-components'` is no longer a valid ImportSource.
42
+ */
43
+ export declare const isStyledComponents: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
44
+ export declare const isCompiled: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
45
+ export declare const isEmotion: (nodeToCheck: Callee, referencesInScope: Reference[], importSources: ImportSource[]) => boolean;
34
46
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-ui-styling-standard",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "author": "Atlassian Pty Ltd",
5
5
  "description": "The eslint plugin to enforce and educate on Atlassian's UI Styling Standard",
6
6
  "license": "UNLICENSED",
@@ -37,15 +37,17 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@babel/runtime": "^7.0.0",
40
- "@typescript-eslint/utils": "^5.48.1"
40
+ "@typescript-eslint/utils": "^5.48.1",
41
+ "estraverse": "^5.3.0"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@atlaskit/ds-lib": "^2.2.0",
44
- "@atlaskit/eslint-plugin-design-system": "^8.31.0",
45
+ "@atlaskit/eslint-plugin-design-system": "^8.36.0",
45
46
  "@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
46
47
  "@atlassian/codegen": "*",
47
48
  "@compiled/eslint-plugin": "^0.13.3",
48
49
  "@types/eslint": "^8.4.5",
50
+ "@types/estraverse": "^5.1.7",
49
51
  "eslint": "^7.7.0",
50
52
  "lodash": "^4.17.21",
51
53
  "outdent": "^0.5.0",
@@ -1,6 +1,6 @@
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 {
@@ -11,6 +11,7 @@ export default {
11
11
  ],
12
12
  rules: {
13
13
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
14
+ '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
14
15
  '@atlaskit/design-system/consistent-css-prop-usage': [
15
16
  'warn',
16
17
  { excludeReactComponents: true },
@@ -1,6 +1,6 @@
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 {
@@ -11,6 +11,7 @@ export default {
11
11
  ],
12
12
  rules: {
13
13
  '@atlaskit/ui-styling-standard/local-cx-xcss': 'warn',
14
+ '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
14
15
  '@atlaskit/design-system/consistent-css-prop-usage': [
15
16
  'warn',
16
17
  { excludeReactComponents: true },
@@ -1,10 +1,12 @@
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
 
8
9
  export default {
9
10
  'local-cx-xcss': localCxXcss,
11
+ 'no-important-styles': noImportantStyles,
10
12
  };
@@ -0,0 +1,27 @@
1
+ This rule prevents the usage of the `!important` flag in style declarations.
2
+
3
+ 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.
4
+
5
+ The use of `!important` is a code smell and greatly impacts readability and determinism.
6
+
7
+ ## Examples
8
+
9
+ ### Incorrect
10
+
11
+ ```js
12
+ import { css } from '@compiled/react';
13
+
14
+ const styles = css({
15
+ color: 'red!important',
16
+ });
17
+ ```
18
+
19
+ ### Correct
20
+
21
+ ```js
22
+ import { css } from '@compiled/react';
23
+
24
+ const styles = css({
25
+ color: 'red',
26
+ });
27
+ ```
@@ -0,0 +1,140 @@
1
+ import { tester } from '../../__tests__/utils/_tester';
2
+ import rule from '../index';
3
+
4
+ tester.run('no-important-styles', rule, {
5
+ valid: [
6
+ `
7
+ import { css } from '@compiled/react';
8
+
9
+ const styles = css({
10
+ color: 'red'
11
+ });
12
+ `,
13
+ {
14
+ // Custom import sources (subtractive)
15
+ code: `
16
+ import { css } from '@compiled/react';
17
+
18
+ const styles = css({
19
+ color: 'red !important'
20
+ });
21
+ `,
22
+ options: [
23
+ {
24
+ importSources: [],
25
+ },
26
+ ],
27
+ },
28
+ ],
29
+ invalid: [
30
+ {
31
+ // Basic test case
32
+ code: `
33
+ import { css } from '@compiled/react';
34
+
35
+ const styles = css({
36
+ color: 'red !important'
37
+ });
38
+ `,
39
+ errors: [{ messageId: 'no-important-styles' }],
40
+ },
41
+ {
42
+ // Whitespace handling
43
+ code: `
44
+ import { css } from '@compiled/react';
45
+
46
+ const styles = css({
47
+ color: 'red !important '
48
+ });
49
+ `,
50
+ errors: [{ messageId: 'no-important-styles' }],
51
+ },
52
+ {
53
+ // Nesting
54
+ code: `
55
+ import { css } from '@compiled/react';
56
+
57
+ const styles = css({
58
+ div: {
59
+ padding: '8px',
60
+ span: {
61
+ padding: '8px',
62
+ color: 'blue!important',
63
+ margin: '8px'
64
+ }
65
+ }
66
+ });
67
+ `,
68
+ errors: [{ messageId: 'no-important-styles' }],
69
+ },
70
+ {
71
+ // styled API
72
+ code: `
73
+ import { styled } from '@compiled/react';
74
+
75
+ const Component = styled.div({
76
+ div: {
77
+ padding: '8px',
78
+ span: {
79
+ padding: '8px',
80
+ color: 'blue!important',
81
+ margin: '8px'
82
+ }
83
+ }
84
+ });
85
+ `,
86
+ errors: [{ messageId: 'no-important-styles' }],
87
+ },
88
+ {
89
+ // keyframes API
90
+ code: `
91
+ import { keyframes } from '@compiled/react';
92
+
93
+ const fadeOut = keyframes({
94
+ from: {
95
+ opacity: '1 !important',
96
+ },
97
+ to: {
98
+ opacity: 0,
99
+ },
100
+ });
101
+ `,
102
+ errors: [{ messageId: 'no-important-styles' }],
103
+ },
104
+ {
105
+ // cssMap API
106
+ code: `
107
+ import { cssMap } from '@compiled/react';
108
+
109
+ const borderStyleMap = cssMap({
110
+ none: { borderStyle: 'none !important' },
111
+ solid: { borderStyle: 'solid' },
112
+ });
113
+ `,
114
+ errors: [{ messageId: 'no-important-styles' }],
115
+ },
116
+ {
117
+ // xcss API
118
+ code: `
119
+ import { xcss } from '@atlaskit/primitives';
120
+
121
+ const styles = xcss({
122
+ display: 'block !important'
123
+ });
124
+ `,
125
+ errors: [{ messageId: 'no-important-styles' }],
126
+ },
127
+ {
128
+ // Custom import sources (additive)
129
+ code: `
130
+ import { css } from 'custom-library';
131
+
132
+ const styles = css({
133
+ color: 'red !important'
134
+ });
135
+ `,
136
+ errors: [{ messageId: 'no-important-styles' }],
137
+ options: [{ importSources: ['custom-library'] }],
138
+ },
139
+ ],
140
+ });