@atlaskit/eslint-plugin-ui-styling-standard 0.3.0 → 0.4.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 (80) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +18 -5
  3. package/build/tsconfig.json +2 -4
  4. package/constellation/consistent-css-prop-usage/usage.mdx +5 -0
  5. package/constellation/index/overview.mdx +21 -0
  6. package/constellation/local-cx-xcss/usage.mdx +37 -0
  7. package/constellation/no-css-tagged-template-expression/usage.mdx +5 -0
  8. package/constellation/no-empty-styled-expression/usage.mdx +5 -0
  9. package/constellation/no-exported-css/usage.mdx +5 -0
  10. package/constellation/no-exported-keyframes/usage.mdx +5 -0
  11. package/constellation/no-invalid-css-map/usage.mdx +5 -0
  12. package/constellation/no-js-xcss/usage.mdx +23 -0
  13. package/constellation/no-keyframes-tagged-template-expression/usage.mdx +5 -0
  14. package/constellation/no-styled-tagged-template-expression/usage.mdx +5 -0
  15. package/constellation/no-suppress-xcss/usage.mdx +25 -0
  16. package/dist/cjs/index.codegen.js +26 -0
  17. package/dist/cjs/index.js +12 -33
  18. package/dist/cjs/presets/all.codegen.js +37 -0
  19. package/dist/cjs/presets/recommended.codegen.js +37 -0
  20. package/dist/cjs/rules/external-rules.js +143 -0
  21. package/dist/cjs/rules/index.codegen.js +16 -0
  22. package/dist/cjs/rules/local-cx-xcss/index.js +51 -0
  23. package/dist/cjs/rules/utils/create-rule.js +43 -0
  24. package/dist/cjs/rules/utils/is-supported-import.js +98 -0
  25. package/dist/es2019/index.codegen.js +12 -0
  26. package/dist/es2019/index.js +1 -33
  27. package/dist/es2019/presets/all.codegen.js +31 -0
  28. package/dist/es2019/presets/recommended.codegen.js +31 -0
  29. package/dist/es2019/rules/external-rules.js +138 -0
  30. package/dist/es2019/rules/index.codegen.js +9 -0
  31. package/dist/es2019/rules/local-cx-xcss/index.js +45 -0
  32. package/dist/es2019/rules/utils/create-rule.js +33 -0
  33. package/dist/es2019/rules/utils/is-supported-import.js +87 -0
  34. package/dist/esm/index.codegen.js +12 -0
  35. package/dist/esm/index.js +1 -33
  36. package/dist/esm/presets/all.codegen.js +31 -0
  37. package/dist/esm/presets/recommended.codegen.js +31 -0
  38. package/dist/esm/rules/external-rules.js +138 -0
  39. package/dist/esm/rules/index.codegen.js +9 -0
  40. package/dist/esm/rules/local-cx-xcss/index.js +45 -0
  41. package/dist/esm/rules/utils/create-rule.js +35 -0
  42. package/dist/esm/rules/utils/is-supported-import.js +90 -0
  43. package/dist/types/index.codegen.d.ts +55 -0
  44. package/dist/types/index.d.ts +1 -0
  45. package/dist/types/presets/all.codegen.d.ts +32 -0
  46. package/dist/types/presets/recommended.codegen.d.ts +32 -0
  47. package/dist/types/rules/external-rules.d.ts +11 -0
  48. package/dist/types/rules/index.codegen.d.ts +4 -0
  49. package/dist/types/rules/local-cx-xcss/index.d.ts +3 -0
  50. package/dist/types/rules/utils/create-rule.d.ts +64 -0
  51. package/dist/types/rules/utils/is-supported-import.d.ts +34 -0
  52. package/dist/types-ts4.5/index.codegen.d.ts +55 -0
  53. package/dist/types-ts4.5/index.d.ts +1 -0
  54. package/dist/types-ts4.5/presets/all.codegen.d.ts +32 -0
  55. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +32 -0
  56. package/dist/types-ts4.5/rules/external-rules.d.ts +11 -0
  57. package/dist/types-ts4.5/rules/index.codegen.d.ts +4 -0
  58. package/dist/types-ts4.5/rules/local-cx-xcss/index.d.ts +3 -0
  59. package/dist/types-ts4.5/rules/utils/create-rule.d.ts +64 -0
  60. package/dist/types-ts4.5/rules/utils/is-supported-import.d.ts +34 -0
  61. package/package.json +13 -4
  62. package/scripts/codegen.tsx +439 -0
  63. package/src/index.codegen.tsx +14 -0
  64. package/src/index.tsx +1 -51
  65. package/src/presets/all.codegen.tsx +67 -0
  66. package/src/presets/recommended.codegen.tsx +67 -0
  67. package/src/rules/TEMPLATE.md +29 -0
  68. package/src/rules/__tests__/utils/_tester.tsx +40 -0
  69. package/src/rules/external-rules.tsx +175 -0
  70. package/src/rules/index.codegen.tsx +10 -0
  71. package/src/rules/local-cx-xcss/README.md +35 -0
  72. package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +102 -0
  73. package/src/rules/local-cx-xcss/index.tsx +69 -0
  74. package/src/rules/utils/create-rule.tsx +80 -0
  75. package/src/rules/utils/is-supported-import.tsx +130 -0
  76. package/tsconfig.app.json +5 -1
  77. package/tsconfig.dev.json +5 -1
  78. package/tsconfig.json +7 -6
  79. package/dist/types/src/index.d.ts +0 -29
  80. package/dist/types-ts4.5/src/index.d.ts +0 -29
@@ -0,0 +1,80 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ // eslint-disable-next-line import/no-extraneous-dependencies
3
+ import type { Rule } from 'eslint';
4
+
5
+ /**
6
+ * We are moving to our own small abstraction to create a lint rule that we have the power
7
+ * to change and mold to our own needs.
8
+ *
9
+ * @see createLintRule
10
+ *
11
+ * @private
12
+ * @deprecated
13
+ */
14
+ export const createRule = ESLintUtils.RuleCreator((name) => getRuleUrl(name));
15
+
16
+ export interface LintRule extends Omit<Rule.RuleModule, 'meta'> {
17
+ /**
18
+ * Including this for backwards compat moving from the typescript-eslint util.
19
+ */
20
+ name?: never;
21
+ meta: LintRuleMeta;
22
+ }
23
+
24
+ export interface LintRuleMeta extends Omit<Rule.RuleMetaData, 'docs'> {
25
+ /**
26
+ * Name of the rule.
27
+ * Must match the folder it is in exactly else build will throw.
28
+ * Add an exception in codegen.tsx for nested rules.
29
+ */
30
+ name: string;
31
+ docs: {
32
+ /**
33
+ * Short description of what this rule does.
34
+ */
35
+ description: string;
36
+ /**
37
+ * Specifies the URL at which the full documentation can be accessed.
38
+ */
39
+ url?: string | undefined;
40
+ /**
41
+ * If this rule should be in the recommended preset or not.
42
+ * Set to `false` to exclude it from the recommended preset.
43
+ */
44
+ recommended: boolean;
45
+ /**
46
+ * The severity level to be applied to this rule.
47
+ * When setting to `"error"` it will mean releasing a breaking change.
48
+ */
49
+ severity: 'error' | 'warn';
50
+ /**
51
+ * This is configuration passed into the plugin to configure the rule.
52
+ *
53
+ * Eg. `{ excludeReactComponents: true }` would be passed into the plugin config
54
+ */
55
+ pluginConfig?: Record<string, any>;
56
+ };
57
+ }
58
+
59
+ /**
60
+ * Tiny wrapped over the ESLint rule module type that ensures
61
+ * there is a docs link to our ESLint plugin documentation page,
62
+ * as well as improving type support.
63
+ */
64
+ export const createLintRule = (rule: LintRule) => {
65
+ (rule.meta.docs as Record<string, unknown>).url = getRuleUrl(rule.meta.name);
66
+
67
+ return rule as Rule.RuleModule;
68
+ };
69
+
70
+ export function getRuleUrl(ruleName: string) {
71
+ const name = getPathSafeName(ruleName);
72
+ return `https://atlassian.design/components/eslint-plugin-ui-styling-standard/${name}/usage`;
73
+ }
74
+
75
+ /**
76
+ * If it's a nested rule, ensure the url is clean and safe for urls, file paths, etc.
77
+ */
78
+ export function getPathSafeName(ruleName: string) {
79
+ return ruleName.replace('/', '-');
80
+ }
@@ -0,0 +1,130 @@
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
2
+ import type { Rule, Scope } from 'eslint';
3
+ import type { CallExpression } from 'estree';
4
+
5
+ type Definition = Scope.Definition;
6
+ type Callee = CallExpression['callee'];
7
+ type Reference = Scope.Reference;
8
+
9
+ export const CSS_IN_JS_IMPORTS = {
10
+ compiled: '@compiled/react',
11
+ emotionReact: '@emotion/react',
12
+ emotionCore: '@emotion/core',
13
+ styledComponents: 'styled-components',
14
+ atlaskitCss: '@atlaskit/css',
15
+ atlaskitPrimitives: '@atlaskit/primitives',
16
+ } as const;
17
+
18
+ // A CSS-in-JS library an import of a valid css, cx, cssMap, etc.
19
+ // function might originate from, e.g. @compiled/react, @emotion/core.
20
+ export type ImportSource = string;
21
+
22
+ export type SupportedNameChecker = (
23
+ nodeToCheck: Callee,
24
+ referencesInScope: Reference[],
25
+ importSources: ImportSource[],
26
+ ) => boolean;
27
+
28
+ // All ESLint rules originating from `@compiled/eslint-plugin` should apply to these libraries.
29
+ export const DEFAULT_IMPORT_SOURCES: ImportSource[] = [
30
+ CSS_IN_JS_IMPORTS.compiled,
31
+ CSS_IN_JS_IMPORTS.atlaskitCss,
32
+ ];
33
+
34
+ /**
35
+ * Given the ESLint rule context, extract and parse the value of the importSources rule option.
36
+ * The importSources option is used to define additional libraries for which an ESLint rule
37
+ * should apply to.
38
+ *
39
+ * Note that `@compiled/react` and `@atlaskit/css` are always included in importSources, regardless
40
+ * of what importSources is configured to by the user.
41
+ *
42
+ * @param context The rule context.
43
+ * @returns An array of strings representing what CSS-in-JS packages that should be checked, based
44
+ * on the rule options configuration.
45
+ */
46
+ export const getImportSources = (context: Rule.RuleContext): ImportSource[] => {
47
+ const options = context.options;
48
+ if (!options.length) {
49
+ return DEFAULT_IMPORT_SOURCES;
50
+ }
51
+
52
+ if (options[0].importSources && Array.isArray(options[0].importSources)) {
53
+ return [...DEFAULT_IMPORT_SOURCES, ...options[0].importSources];
54
+ }
55
+
56
+ return DEFAULT_IMPORT_SOURCES;
57
+ };
58
+
59
+ const isSupportedImportWrapper = (
60
+ functionName: string,
61
+ ): SupportedNameChecker => {
62
+ const checkDefinitionHasImport = (
63
+ def: Definition,
64
+ importSources: ImportSource[],
65
+ ) => {
66
+ if (def.type !== 'ImportBinding') {
67
+ return false;
68
+ }
69
+
70
+ if (
71
+ !def.parent ||
72
+ !importSources.includes(def.parent.source.value as ImportSource)
73
+ ) {
74
+ return false;
75
+ }
76
+
77
+ return (
78
+ // import { functionName } from 'import-source';
79
+ (def.node.type === 'ImportSpecifier' &&
80
+ def.node.imported.name === functionName) ||
81
+ // import functionName from 'import-source';
82
+ (def.node.type === 'ImportDefaultSpecifier' &&
83
+ def.node.local.name === functionName)
84
+ );
85
+ };
86
+
87
+ /**
88
+ * Checks whether:
89
+ *
90
+ * 1. A function name `nodeToCheck` matches the name of the function we
91
+ * want to check for (e.g. `cx`, `css`, `cssMap`, or `keyframes`), and
92
+ * 2. Whether `nodeToCheck` originates from one of the libraries listed
93
+ * in `importSources`.
94
+ *
95
+ * @param nodeToCheck The function callee we are checking (e.g. The `css` in `css()`).
96
+ * @param referencesInScope List of references that are in scope. We'll use this
97
+ * to check where the function callee is imported from.
98
+ * @param importSources List of libraries that we want to ensure `nodeToCheck`
99
+ * comes from.
100
+ *
101
+ * @returns Whether the above conditions are true.
102
+ */
103
+ const isSupportedImport = (
104
+ nodeToCheck: Callee,
105
+ referencesInScope: Reference[],
106
+ importSources: ImportSource[],
107
+ ): boolean => {
108
+ return (
109
+ nodeToCheck.type === 'Identifier' &&
110
+ referencesInScope.some(
111
+ (reference) =>
112
+ reference.identifier === nodeToCheck &&
113
+ reference.resolved?.defs.some((def) =>
114
+ checkDefinitionHasImport(def, importSources),
115
+ ),
116
+ )
117
+ );
118
+ };
119
+
120
+ return isSupportedImport;
121
+ };
122
+
123
+ // Unused functions have been commented out until we implement corresponding
124
+ // eslint rules which use them
125
+ //
126
+ export const isCss = isSupportedImportWrapper('css');
127
+ export const isCxFunction = isSupportedImportWrapper('cx');
128
+ export const isCssMap = isSupportedImportWrapper('cssMap');
129
+ export const isKeyframes = isSupportedImportWrapper('keyframes');
130
+ export const isStyled = isSupportedImportWrapper('styled');
package/tsconfig.app.json CHANGED
@@ -32,6 +32,10 @@
32
32
  "composite": true,
33
33
  "outDir": "../../../tsDist/@atlaskit__eslint-plugin-ui-styling-standard/app"
34
34
  },
35
- "references": [],
35
+ "references": [
36
+ {
37
+ "path": "../codegen/tsconfig.app.json"
38
+ }
39
+ ],
36
40
  "files": []
37
41
  }
package/tsconfig.dev.json CHANGED
@@ -23,7 +23,8 @@
23
23
  "./__mocks__/**/*",
24
24
  "**/mock.*",
25
25
  "**/codemods/**/*.ts",
26
- "**/codemods/**/*.tsx"
26
+ "**/codemods/**/*.tsx",
27
+ "./**/__fixtures__/**/*.json"
27
28
  ],
28
29
  "exclude": ["./dist/**/*", "./build/**/*", "./node_modules/**/*"],
29
30
  "compilerOptions": {
@@ -35,6 +36,9 @@
35
36
  "references": [
36
37
  {
37
38
  "path": "tsconfig.app.json"
39
+ },
40
+ {
41
+ "path": "../ds-lib/tsconfig.app.json"
38
42
  }
39
43
  ],
40
44
  "files": []
package/tsconfig.json CHANGED
@@ -3,22 +3,23 @@
3
3
  "include": [
4
4
  "__tests__/**/*.ts",
5
5
  "__tests__/**/*.tsx",
6
+ "docs/**/*.ts",
7
+ "docs/**/*.tsx",
8
+ "examples/**/*.ts",
9
+ "examples/**/*.tsx",
6
10
  "src/**/*.ts",
7
11
  "src/**/*.tsx",
8
12
  "scripts"
9
13
  ],
10
- "exclude": [
11
- "src/**/__tests__/*.*",
12
- ],
14
+ "exclude": [],
13
15
  "compilerOptions": {
14
16
  "baseUrl": "./",
15
- "lib": ["ES2021.String"],
16
- "paths": {}
17
+ "lib": ["ES2021.String"]
17
18
  },
18
19
  "ts-node": {
19
20
  // It is faster to skip typechecking.
20
21
  "transpileOnly": true,
21
22
  // We must load files for third party defs - https://typestrong.org/ts-node/docs/types
22
- "files": true,
23
+ "files": true
23
24
  }
24
25
  }
@@ -1,29 +0,0 @@
1
- export declare const rules: {};
2
- export declare const configs: {
3
- recommended: {
4
- plugins: string[];
5
- rules: {
6
- '@atlaskit/design-system/consistent-css-prop-usage': (string | {
7
- excludeReactComponents: boolean;
8
- })[];
9
- '@atlaskit/design-system/local-cx-xcss': string;
10
- '@atlaskit/design-system/no-css-tagged-template-expression': string;
11
- '@atlaskit/design-system/no-keyframes-tagged-template-expression': string;
12
- '@atlaskit/design-system/no-styled-tagged-template-expression': string;
13
- '@compiled/no-suppress-xcss': string;
14
- '@compiled/no-js-xcss': string;
15
- '@atlaskit/design-system/no-empty-styled-expression': (string | {
16
- importSources: readonly string[];
17
- })[];
18
- '@atlaskit/design-system/no-exported-css': (string | {
19
- importSources: readonly string[];
20
- })[];
21
- '@atlaskit/design-system/no-exported-keyframes': (string | {
22
- importSources: readonly string[];
23
- })[];
24
- '@atlaskit/design-system/no-invalid-css-map': (string | {
25
- allowedFunctionCalls: string[][];
26
- })[];
27
- };
28
- };
29
- };
@@ -1,29 +0,0 @@
1
- export declare const rules: {};
2
- export declare const configs: {
3
- recommended: {
4
- plugins: string[];
5
- rules: {
6
- '@atlaskit/design-system/consistent-css-prop-usage': (string | {
7
- excludeReactComponents: boolean;
8
- })[];
9
- '@atlaskit/design-system/local-cx-xcss': string;
10
- '@atlaskit/design-system/no-css-tagged-template-expression': string;
11
- '@atlaskit/design-system/no-keyframes-tagged-template-expression': string;
12
- '@atlaskit/design-system/no-styled-tagged-template-expression': string;
13
- '@compiled/no-suppress-xcss': string;
14
- '@compiled/no-js-xcss': string;
15
- '@atlaskit/design-system/no-empty-styled-expression': (string | {
16
- importSources: readonly string[];
17
- })[];
18
- '@atlaskit/design-system/no-exported-css': (string | {
19
- importSources: readonly string[];
20
- })[];
21
- '@atlaskit/design-system/no-exported-keyframes': (string | {
22
- importSources: readonly string[];
23
- })[];
24
- '@atlaskit/design-system/no-invalid-css-map': (string | {
25
- allowedFunctionCalls: string[][];
26
- })[];
27
- };
28
- };
29
- };