@atlaskit/eslint-plugin-ui-styling-standard 0.13.0 → 0.14.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 (45) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +1 -0
  3. package/constellation/index/overview.mdx +1 -0
  4. package/constellation/no-styled/usage.mdx +89 -0
  5. package/constellation/use-compiled/usage.mdx +2 -2
  6. package/dist/cjs/presets/all.codegen.js +2 -1
  7. package/dist/cjs/presets/recommended.codegen.js +2 -1
  8. package/dist/cjs/rules/index.codegen.js +3 -1
  9. package/dist/cjs/rules/no-important-styles/index.js +11 -17
  10. package/dist/cjs/rules/no-styled/index.js +67 -0
  11. package/dist/cjs/rules/utils/create-rule-with-typed-config.js +60 -0
  12. package/dist/es2019/presets/all.codegen.js +2 -1
  13. package/dist/es2019/presets/recommended.codegen.js +2 -1
  14. package/dist/es2019/rules/index.codegen.js +3 -1
  15. package/dist/es2019/rules/no-important-styles/index.js +13 -18
  16. package/dist/es2019/rules/no-styled/index.js +45 -0
  17. package/dist/es2019/rules/utils/create-rule-with-typed-config.js +53 -0
  18. package/dist/esm/presets/all.codegen.js +2 -1
  19. package/dist/esm/presets/recommended.codegen.js +2 -1
  20. package/dist/esm/rules/index.codegen.js +3 -1
  21. package/dist/esm/rules/no-important-styles/index.js +12 -18
  22. package/dist/esm/rules/no-styled/index.js +61 -0
  23. package/dist/esm/rules/utils/create-rule-with-typed-config.js +53 -0
  24. package/dist/types/index.codegen.d.ts +2 -0
  25. package/dist/types/presets/all.codegen.d.ts +2 -1
  26. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  27. package/dist/types/rules/index.codegen.d.ts +1 -0
  28. package/dist/types/rules/no-styled/index.d.ts +2 -0
  29. package/dist/types/rules/utils/create-rule-with-typed-config.d.ts +18 -0
  30. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  31. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  32. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  33. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  34. package/dist/types-ts4.5/rules/no-styled/index.d.ts +2 -0
  35. package/dist/types-ts4.5/rules/utils/create-rule-with-typed-config.d.ts +18 -0
  36. package/package.json +3 -2
  37. package/src/presets/all.codegen.tsx +2 -1
  38. package/src/presets/recommended.codegen.tsx +2 -1
  39. package/src/rules/index.codegen.tsx +3 -1
  40. package/src/rules/no-important-styles/__tests__/rule.test.tsx +12 -0
  41. package/src/rules/no-important-styles/index.tsx +11 -24
  42. package/src/rules/no-styled/README.md +82 -0
  43. package/src/rules/no-styled/__tests__/rule.test.tsx +73 -0
  44. package/src/rules/no-styled/index.tsx +52 -0
  45. package/src/rules/utils/create-rule-with-typed-config.tsx +87 -0
@@ -0,0 +1,52 @@
1
+ import { type MemberExpression, type CallExpression } from 'eslint-codemod-utils';
2
+ import { isStyled, getImportSources } from '@atlaskit/eslint-utils/is-supported-import';
3
+
4
+
5
+ import { createLintRule } from '../utils/create-rule';
6
+
7
+ export const rule = createLintRule({
8
+ meta: {
9
+ name: 'no-styled',
10
+ docs: {
11
+ description: 'Disallows usage of the `styled` imports',
12
+ recommended: true,
13
+ severity: 'warn',
14
+ },
15
+ messages: {
16
+ 'no-styled': "Avoid usages of styled, eg. styled.div, styled('div'), styled(Component), etc.",
17
+ },
18
+ type: 'problem',
19
+ },
20
+ create(context) {
21
+ return {
22
+ CallExpression: (node: CallExpression) => {
23
+ const references = context.getScope().references;
24
+ const importSources = getImportSources(context);
25
+
26
+ if (
27
+ isStyled(node, references, importSources)
28
+ ) {
29
+ context.report({ node, messageId: 'no-styled' });
30
+ }
31
+
32
+ },
33
+ MemberExpression: (node: MemberExpression) => {
34
+ const references = context.getScope().references;
35
+ const importSources = getImportSources(context);
36
+
37
+ if (
38
+ node.object.type === 'Identifier' &&
39
+ isStyled(node.object, references, importSources)
40
+ ) {
41
+ const parent = context.getAncestors().at(-1);
42
+ if (parent && (parent.type === 'CallExpression' || parent.type === 'MemberExpression')) {
43
+ return;
44
+ }
45
+ context.report({ node, messageId: 'no-styled' });
46
+ }
47
+ },
48
+ };
49
+ },
50
+ });
51
+
52
+ export default rule;
@@ -0,0 +1,87 @@
1
+ import Ajv from 'ajv';
2
+ import type { Rule } from 'eslint';
3
+ import type { FromSchema, JSONSchema } from 'json-schema-to-ts';
4
+
5
+ import type {
6
+ LintRule as BaseLintRule,
7
+ LintRuleMeta as BaseLintRuleMeta,
8
+ } from '@atlaskit/eslint-utils/create-rule';
9
+
10
+ import { getRuleUrl } from './create-rule';
11
+
12
+ export interface LintRule<Schema extends JSONSchema, Config>
13
+ extends Omit<BaseLintRule, 'meta' | 'create'> {
14
+ meta: LintRuleMeta<Schema>;
15
+
16
+ create(context: Rule.RuleContext, config: Config): Rule.RuleListener;
17
+
18
+ /**
19
+ * Unsure why the eslint typings include this when the docs
20
+ * say the schema should be in meta.
21
+ */
22
+ schema?: never;
23
+ }
24
+
25
+ export interface LintRuleMeta<Schema extends JSONSchema>
26
+ extends Omit<BaseLintRuleMeta, 'schema'> {
27
+ schema: Schema;
28
+ }
29
+
30
+ export const createLintRuleWithTypedConfig = <
31
+ Schema extends JSONSchema & { type: 'object' },
32
+ Config = FromSchema<Schema>,
33
+ >(
34
+ rule: LintRule<Schema, Config>,
35
+ ) => {
36
+ /**
37
+ * Gets the default config by running the same validator
38
+ * ESLint does against an empty config object with
39
+ * `useDefaults` option enabled.
40
+ *
41
+ * Any properties with a `default` property set in the schema
42
+ * will be populated with the specified default value.
43
+ */
44
+ function getDefaultConfig(): Config {
45
+ const ajv = new Ajv({ useDefaults: true });
46
+ const validate = ajv.compile(rule.meta.schema);
47
+
48
+ const config = {};
49
+ /**
50
+ * The `validate()` function mutates the config object.
51
+ */
52
+ validate(config);
53
+
54
+ return config as Config;
55
+ }
56
+
57
+ return {
58
+ ...rule,
59
+ meta: {
60
+ ...rule.meta,
61
+ docs: {
62
+ ...rule.meta.docs,
63
+ url: getRuleUrl(rule.meta.name),
64
+ },
65
+ schema: [rule.meta.schema],
66
+ },
67
+ create(context) {
68
+ /**
69
+ * The config the user has passed.
70
+ *
71
+ * If they passed a config object, even if it is missing properties,
72
+ * ESLint will have added any default values defined using the
73
+ * `default` property in the schema.
74
+ *
75
+ * https://github.com/eslint/eslint/pull/11288
76
+ */
77
+ const userConfig = context.options[0] as Config | undefined;
78
+ /**
79
+ * If the user passed no config object at all we need to obtain
80
+ * the default ourselves.
81
+ */
82
+ const config = userConfig ?? getDefaultConfig();
83
+
84
+ return rule.create(context, config);
85
+ },
86
+ } as Rule.RuleModule;
87
+ };