@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
@@ -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 | {
@@ -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,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 | {
@@ -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 | {
@@ -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.0",
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",
@@ -259,7 +259,7 @@ async function generateExternalRulePage(
259
259
 
260
260
  ${
261
261
  rule.module.meta?.docs?.externalUrl
262
- ? `This rule is maintained by another package and configured here, refer to ${rule.module.meta.docs.externalUrl} for more details`
262
+ ? `This rule is maintained by another package and configured here; refer to ${rule.module.meta.docs.externalUrl} for more details.`
263
263
  : ''
264
264
  }
265
265
  `;
@@ -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::6b1418e07df6af9ad4cae4baf66739c1>>
3
+ * @codegen <<SignedSource::c0973784c8a5047835032781219885ce>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -11,13 +11,25 @@ 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 },
17
18
  ],
18
19
  '@atlaskit/design-system/no-css-tagged-template-expression': 'warn',
19
20
  '@atlaskit/design-system/no-keyframes-tagged-template-expression': 'warn',
20
- '@atlaskit/design-system/no-styled-tagged-template-expression': 'warn',
21
+ '@atlaskit/design-system/no-styled-tagged-template-expression': [
22
+ 'warn',
23
+ {
24
+ importSources: [
25
+ '@compiled/react',
26
+ '@atlaskit/css',
27
+ '@emotion/core',
28
+ '@emotion/react',
29
+ '@emotion/styled',
30
+ ],
31
+ },
32
+ ],
21
33
  '@compiled/no-suppress-xcss': 'warn',
22
34
  '@compiled/no-js-xcss': 'warn',
23
35
  '@atlaskit/design-system/no-empty-styled-expression': [
@@ -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::6b1418e07df6af9ad4cae4baf66739c1>>
3
+ * @codegen <<SignedSource::c0973784c8a5047835032781219885ce>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -11,13 +11,25 @@ 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 },
17
18
  ],
18
19
  '@atlaskit/design-system/no-css-tagged-template-expression': 'warn',
19
20
  '@atlaskit/design-system/no-keyframes-tagged-template-expression': 'warn',
20
- '@atlaskit/design-system/no-styled-tagged-template-expression': 'warn',
21
+ '@atlaskit/design-system/no-styled-tagged-template-expression': [
22
+ 'warn',
23
+ {
24
+ importSources: [
25
+ '@compiled/react',
26
+ '@atlaskit/css',
27
+ '@emotion/core',
28
+ '@emotion/react',
29
+ '@emotion/styled',
30
+ ],
31
+ },
32
+ ],
21
33
  '@compiled/no-suppress-xcss': 'warn',
22
34
  '@compiled/no-js-xcss': 'warn',
23
35
  '@atlaskit/design-system/no-empty-styled-expression': [
@@ -17,6 +17,33 @@ const importSources: readonly string[] = [
17
17
  'styled-components',
18
18
  ];
19
19
 
20
+ /**
21
+ * Same as importSources, but excludes `styled-components` because the type-checking
22
+ * for `styled-components` does not support the `no-styled-tagged-template-expression`
23
+ * lint output for this:
24
+ *
25
+ * export const Container = styled(Section)`
26
+ * max-height: ${gridSize * 46}px;
27
+ * overflow: auto;
28
+ * `;
29
+ *
30
+ * export const ErrorStateContainer = styled(Container)`
31
+ * margin: ${
32
+ * (props: any) =>
33
+ * props.canManageAutomations // Use negative margin to reduce whitespace when the Footer component is shown
34
+ * ? `${gridSize * -4}px ${gridSize * 2}px`
35
+ * : `0 ${gridSize * 2}px`
36
+ * };
37
+ * `;
38
+ */
39
+ const noStyledTaggedTemplateExpressionImportSources: readonly string[] = [
40
+ '@compiled/react',
41
+ '@atlaskit/css', // doesn't do anything because @atlaskit/css doesn't expose a styled API
42
+ '@emotion/core',
43
+ '@emotion/react',
44
+ '@emotion/styled',
45
+ ];
46
+
20
47
  export const externalRules: ExternalRuleMeta[] = [
21
48
  {
22
49
  name: '@atlaskit/design-system/consistent-css-prop-usage',
@@ -81,6 +108,9 @@ export const externalRules: ExternalRuleMeta[] = [
81
108
  'https://atlassian.design/components/eslint-plugin-design-system/no-styled-tagged-template-expression/usage',
82
109
  recommended: true,
83
110
  severity: 'warn',
111
+ pluginConfig: {
112
+ importSources: noStyledTaggedTemplateExpressionImportSources,
113
+ },
84
114
  },
85
115
  },
86
116
  {
@@ -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
+ });
@@ -0,0 +1,88 @@
1
+ import estraverse from 'estraverse';
2
+
3
+ import { createLintRule } from '../utils/create-rule';
4
+ import {
5
+ isCss,
6
+ getImportSources,
7
+ isStyled,
8
+ isKeyframes,
9
+ isCssMap,
10
+ isXcss,
11
+ } from '../utils/is-supported-import';
12
+ import type { JSONSchema4 } from '@typescript-eslint/utils/dist/json-schema';
13
+
14
+ const schema: JSONSchema4 = [
15
+ {
16
+ type: 'object',
17
+ properties: {
18
+ importSources: {
19
+ type: 'array',
20
+ items: { type: 'string' },
21
+ uniqueItems: true,
22
+ },
23
+ },
24
+ },
25
+ ];
26
+
27
+ export const rule = createLintRule({
28
+ meta: {
29
+ name: 'no-important-styles',
30
+ docs: {
31
+ description: 'Disallows important style declarations',
32
+ recommended: true,
33
+ severity: 'warn',
34
+ },
35
+ messages: {
36
+ 'no-important-styles':
37
+ 'Important style declarations are disallowed. Refactor so the `!important` flag is not needed.',
38
+ },
39
+ type: 'problem',
40
+ schema,
41
+ },
42
+ create(context) {
43
+ const importSources = getImportSources(context);
44
+
45
+ return {
46
+ CallExpression(node) {
47
+ const { references } = context.getScope();
48
+
49
+ if (
50
+ isCss(node.callee, references, importSources) ||
51
+ isStyled(node.callee, references, importSources) ||
52
+ isKeyframes(node.callee, references, importSources) ||
53
+ isCssMap(node.callee, references, importSources) ||
54
+ isXcss(node.callee, references, importSources)
55
+ ) {
56
+ estraverse.traverse(node, {
57
+ enter(node) {
58
+ if (node.type !== 'Property') {
59
+ return;
60
+ }
61
+
62
+ if (node.value.type !== 'Literal') {
63
+ return;
64
+ }
65
+
66
+ /**
67
+ * There can be whitespace between the `!` and the `important` keyword.
68
+ * There can also be whitespace after the `important` keyword.
69
+ */
70
+ if (node.value.value?.toString().match(/!\s*important\s*$/gm)) {
71
+ context.report({
72
+ node: node.value,
73
+ messageId: 'no-important-styles',
74
+ });
75
+ }
76
+ },
77
+ /**
78
+ * This is needed to handle unknown node types. Otherwise an error is thrown.
79
+ */
80
+ fallback: 'iteration',
81
+ });
82
+ }
83
+ },
84
+ };
85
+ },
86
+ });
87
+
88
+ export default rule;