@atlaskit/eslint-plugin-design-system 8.29.0 → 8.30.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 (73) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +1 -0
  3. package/constellation/index/usage.mdx +1 -0
  4. package/constellation/use-primitives-text/usage.mdx +21 -0
  5. package/dist/cjs/ast-nodes/jsx-element.js +33 -2
  6. package/dist/cjs/presets/all.codegen.js +2 -1
  7. package/dist/cjs/rules/index.codegen.js +3 -1
  8. package/dist/cjs/rules/no-unsupported-drag-and-drop-libraries/paths.js +3 -0
  9. package/dist/cjs/rules/use-primitives/transformers/emotion-css/index.js +1 -2
  10. package/dist/cjs/rules/use-primitives-text/config/index.js +15 -0
  11. package/dist/cjs/rules/use-primitives-text/index.js +57 -0
  12. package/dist/cjs/rules/use-primitives-text/transformers/common.js +43 -0
  13. package/dist/cjs/rules/use-primitives-text/transformers/emphasis-elements.js +82 -0
  14. package/dist/cjs/rules/use-primitives-text/transformers/index.js +33 -0
  15. package/dist/cjs/rules/use-primitives-text/transformers/paragraph-elements.js +196 -0
  16. package/dist/cjs/rules/use-primitives-text/transformers/span-elements.js +87 -0
  17. package/dist/cjs/rules/use-primitives-text/transformers/strong-elements.js +82 -0
  18. package/dist/es2019/ast-nodes/jsx-element.js +33 -2
  19. package/dist/es2019/presets/all.codegen.js +2 -1
  20. package/dist/es2019/rules/index.codegen.js +3 -1
  21. package/dist/es2019/rules/no-unsupported-drag-and-drop-libraries/paths.js +3 -0
  22. package/dist/es2019/rules/use-primitives/transformers/emotion-css/index.js +1 -2
  23. package/dist/es2019/rules/use-primitives-text/config/index.js +9 -0
  24. package/dist/es2019/rules/use-primitives-text/index.js +51 -0
  25. package/dist/es2019/rules/use-primitives-text/transformers/common.js +33 -0
  26. package/dist/es2019/rules/use-primitives-text/transformers/emphasis-elements.js +72 -0
  27. package/dist/es2019/rules/use-primitives-text/transformers/index.js +4 -0
  28. package/dist/es2019/rules/use-primitives-text/transformers/paragraph-elements.js +187 -0
  29. package/dist/es2019/rules/use-primitives-text/transformers/span-elements.js +77 -0
  30. package/dist/es2019/rules/use-primitives-text/transformers/strong-elements.js +72 -0
  31. package/dist/esm/ast-nodes/jsx-element.js +33 -2
  32. package/dist/esm/presets/all.codegen.js +2 -1
  33. package/dist/esm/rules/index.codegen.js +3 -1
  34. package/dist/esm/rules/no-unsupported-drag-and-drop-libraries/paths.js +3 -0
  35. package/dist/esm/rules/use-primitives/transformers/emotion-css/index.js +1 -2
  36. package/dist/esm/rules/use-primitives-text/config/index.js +9 -0
  37. package/dist/esm/rules/use-primitives-text/index.js +51 -0
  38. package/dist/esm/rules/use-primitives-text/transformers/common.js +33 -0
  39. package/dist/esm/rules/use-primitives-text/transformers/emphasis-elements.js +72 -0
  40. package/dist/esm/rules/use-primitives-text/transformers/index.js +4 -0
  41. package/dist/esm/rules/use-primitives-text/transformers/paragraph-elements.js +186 -0
  42. package/dist/esm/rules/use-primitives-text/transformers/span-elements.js +77 -0
  43. package/dist/esm/rules/use-primitives-text/transformers/strong-elements.js +72 -0
  44. package/dist/types/ast-nodes/jsx-element.d.ts +5 -2
  45. package/dist/types/index.codegen.d.ts +1 -0
  46. package/dist/types/presets/all.codegen.d.ts +2 -1
  47. package/dist/types/rules/index.codegen.d.ts +1 -0
  48. package/dist/types/rules/use-primitives-text/config/index.d.ts +6 -0
  49. package/dist/types/rules/use-primitives-text/index.d.ts +3 -0
  50. package/dist/types/rules/use-primitives-text/transformers/common.d.ts +5 -0
  51. package/dist/types/rules/use-primitives-text/transformers/emphasis-elements.d.ts +16 -0
  52. package/dist/types/rules/use-primitives-text/transformers/index.d.ts +4 -0
  53. package/dist/types/rules/use-primitives-text/transformers/paragraph-elements.d.ts +32 -0
  54. package/dist/types/rules/use-primitives-text/transformers/span-elements.d.ts +16 -0
  55. package/dist/types/rules/use-primitives-text/transformers/strong-elements.d.ts +16 -0
  56. package/dist/types-ts4.5/ast-nodes/jsx-element.d.ts +5 -2
  57. package/dist/types-ts4.5/index.codegen.d.ts +1 -0
  58. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  59. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  60. package/dist/types-ts4.5/rules/use-primitives-text/config/index.d.ts +6 -0
  61. package/dist/types-ts4.5/rules/use-primitives-text/index.d.ts +3 -0
  62. package/dist/types-ts4.5/rules/use-primitives-text/transformers/common.d.ts +5 -0
  63. package/dist/types-ts4.5/rules/use-primitives-text/transformers/emphasis-elements.d.ts +16 -0
  64. package/dist/types-ts4.5/rules/use-primitives-text/transformers/index.d.ts +4 -0
  65. package/dist/types-ts4.5/rules/use-primitives-text/transformers/paragraph-elements.d.ts +32 -0
  66. package/dist/types-ts4.5/rules/use-primitives-text/transformers/span-elements.d.ts +16 -0
  67. package/dist/types-ts4.5/rules/use-primitives-text/transformers/strong-elements.d.ts +16 -0
  68. package/package.json +1 -1
  69. package/dist/cjs/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.js +0 -27
  70. package/dist/es2019/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.js +0 -19
  71. package/dist/esm/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.js +0 -19
  72. package/dist/types/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.d.ts +0 -7
  73. package/dist/types-ts4.5/rules/use-primitives/transformers/emotion-css/contains-only-supported-attrs.d.ts +0 -7
@@ -0,0 +1,77 @@
1
+ /* eslint-disable @repo/internal/react/require-jsdoc */
2
+
3
+ import { isNodeOfType } from 'eslint-codemod-utils';
4
+ import * as ast from '../../../ast-nodes';
5
+ import { upsertImportDeclaration } from '../../use-primitives/transformers/emotion-css/upsert-import-declaration';
6
+ import { allowedAttrs, hasTextChildrenOnly, updateTestIdAttributeFix } from './common';
7
+ export const SpanElements = {
8
+ lint(node, {
9
+ context,
10
+ config
11
+ }) {
12
+ if (!isNodeOfType(node, 'JSXElement')) {
13
+ return;
14
+ }
15
+
16
+ // Check whether all criteria needed to make a transformation are met
17
+ if (!SpanElements._check(node, {
18
+ context,
19
+ config
20
+ })) {
21
+ return;
22
+ }
23
+ context.report({
24
+ node: node.openingElement,
25
+ messageId: 'preferPrimitivesText',
26
+ suggest: [{
27
+ desc: `Convert to Text`,
28
+ fix: SpanElements._fix(node, {
29
+ context
30
+ })
31
+ }]
32
+ });
33
+ },
34
+ _check(node, {
35
+ context,
36
+ config
37
+ }) {
38
+ if (!config.patterns.includes('span-elements')) {
39
+ return false;
40
+ }
41
+ const elementName = ast.JSXElement.getName(node);
42
+ if (elementName !== 'span') {
43
+ return false;
44
+ }
45
+
46
+ // Element has no unallowed props
47
+ if (!ast.JSXElement.hasAllowedAttrsOnly(node, allowedAttrs)) {
48
+ return false;
49
+ }
50
+
51
+ // Only allow elements with strings as children
52
+ if (!hasTextChildrenOnly(node)) {
53
+ return false;
54
+ }
55
+ const importDeclaration = ast.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
56
+
57
+ // If there is more than one `@atlaskit/primitives` import, then it becomes difficult to determine which import to transform
58
+ if (importDeclaration.length > 1) {
59
+ return false;
60
+ }
61
+ return true;
62
+ },
63
+ _fix(node, {
64
+ context
65
+ }) {
66
+ return fixer => {
67
+ const importFix = upsertImportDeclaration({
68
+ module: '@atlaskit/primitives',
69
+ specifiers: ['Text']
70
+ }, context, fixer);
71
+ const elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
72
+ const variantAttributeFix = ast.JSXElement.addAttribute(node, 'variant', 'ui', fixer);
73
+ const testAttributeFix = updateTestIdAttributeFix(node, fixer);
74
+ return [importFix, ...elementNameFixes, variantAttributeFix, testAttributeFix].filter(fix => Boolean(fix)); // Some of the transformers can return arrays with undefined, so filter them out
75
+ };
76
+ }
77
+ };
@@ -0,0 +1,72 @@
1
+ /* eslint-disable @repo/internal/react/require-jsdoc */
2
+
3
+ import { isNodeOfType } from 'eslint-codemod-utils';
4
+ import * as ast from '../../../ast-nodes';
5
+ import { upsertImportDeclaration } from '../../use-primitives/transformers/emotion-css/upsert-import-declaration';
6
+ import { allowedAttrs, updateTestIdAttributeFix } from './common';
7
+ export const StrongElements = {
8
+ lint(node, {
9
+ context,
10
+ config
11
+ }) {
12
+ if (!isNodeOfType(node, 'JSXElement')) {
13
+ return;
14
+ }
15
+
16
+ // Check whether all criteria needed to make a transformation are met
17
+ if (!StrongElements._check(node, {
18
+ context,
19
+ config
20
+ })) {
21
+ return;
22
+ }
23
+ context.report({
24
+ node: node.openingElement,
25
+ messageId: 'preferPrimitivesText',
26
+ suggest: [{
27
+ desc: `Convert to Text`,
28
+ fix: StrongElements._fix(node, {
29
+ context
30
+ })
31
+ }]
32
+ });
33
+ },
34
+ _check(node, {
35
+ context,
36
+ config
37
+ }) {
38
+ if (!config.patterns.includes('strong-elements')) {
39
+ return false;
40
+ }
41
+ const elementName = ast.JSXElement.getName(node);
42
+ if (elementName !== 'strong') {
43
+ return false;
44
+ }
45
+
46
+ // Element has no unallowed props
47
+ if (!ast.JSXElement.hasAllowedAttrsOnly(node, allowedAttrs)) {
48
+ return false;
49
+ }
50
+
51
+ // If there is more than one `@atlaskit/primitives` import, then it becomes difficult to determine which import to transform
52
+ const importDeclaration = ast.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
53
+ if (importDeclaration.length > 1) {
54
+ return false;
55
+ }
56
+ return true;
57
+ },
58
+ _fix(node, {
59
+ context
60
+ }) {
61
+ return fixer => {
62
+ const importFix = upsertImportDeclaration({
63
+ module: '@atlaskit/primitives',
64
+ specifiers: ['Text']
65
+ }, context, fixer);
66
+ const elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
67
+ const asAttributeFix = ast.JSXElement.addAttribute(node, 'as', 'strong', fixer);
68
+ const testAttributeFix = updateTestIdAttributeFix(node, fixer);
69
+ return [importFix, ...elementNameFixes, asAttributeFix, testAttributeFix].filter(fix => Boolean(fix)); // Some of the transformers can return arrays with undefined, so filter them out
70
+ };
71
+ }
72
+ };
@@ -1,4 +1,5 @@
1
1
  import { isNodeOfType, jsxIdentifier } from 'eslint-codemod-utils';
2
+ import { JSXAttribute as HelperJSXAttribute } from './jsx-attribute';
2
3
  export var JSXElementHelper = {
3
4
  /**
4
5
  * Names of JSXElements can be any of:
@@ -6,8 +7,8 @@ export var JSXElementHelper = {
6
7
  * `<MyComponents.Component></MyComponents.Component>` - `MyComponents` is a namespace (JSXNamespacedName)
7
8
  * `<MyComponents.Component></MyComponents.Component>` - `MyComponents` is an object (JSXMemberExpression)
8
9
  *
9
- * Getting the name of a JSXMemberExpression is difficult, because object can contain objects, which is recursively defined in the AST.
10
- * e.g. getting the name of `<MyComponents.PresentationLayer.LeftSideBar.Header />` would require `getName` to recursively resolve all parts of the name.
10
+ * getting the name of a JSXMemberExpression is difficult, because object can contain objects, which is recursively defined in the AST.
11
+ * e.g. Getting the name of `<MyComponents.PresentationLayer.LeftSideBar.Header />` would require `getName` to recursively resolve all parts of the name.
11
12
  * `getName` does not currently have this functionality. Add it if you need it.
12
13
  */
13
14
  getName: function getName(node) {
@@ -45,6 +46,36 @@ export var JSXElementHelper = {
45
46
  return node.openingElement.attributes.some(function (attr) {
46
47
  return isNodeOfType(attr, 'JSXSpreadAttribute');
47
48
  });
49
+ },
50
+ addAttribute: function addAttribute(node, name, value, fixer) {
51
+ var attributeString = " ".concat(name, "='").concat(value, "'");
52
+ var isSelfClosing = JSXElementHelper.isSelfClosing(node);
53
+ var start = node.openingElement.range ? node.openingElement.range[0] : 0;
54
+ var end = node.openingElement.range ? node.openingElement.range[1] - (isSelfClosing ? 3 : 1) : 0;
55
+ var range = [start, end];
56
+ var fix = fixer.insertTextAfterRange(range, attributeString);
57
+ return fix;
58
+ },
59
+ getChildren: function getChildren(node) {
60
+ // Filter out text children with whitespace characters only as JSX removes whitespace used for intendation
61
+ var filteredChildren = node.children.filter(function (child) {
62
+ if (isNodeOfType(child, 'JSXText')) {
63
+ var whiteSpaceChars = new RegExp('\\s', 'g');
64
+ return !whiteSpaceChars.test(child.value);
65
+ }
66
+ return true;
67
+ });
68
+ return filteredChildren;
69
+ },
70
+ hasAllowedAttrsOnly: function hasAllowedAttrsOnly(node, allowedProps) {
71
+ var attrs = JSXElementHelper.getAttributes(node);
72
+ return attrs.every(function (attr) {
73
+ if (!isNodeOfType(attr, 'JSXAttribute')) {
74
+ return false;
75
+ }
76
+ var name = HelperJSXAttribute.getName(attr);
77
+ return allowedProps.includes(name);
78
+ });
48
79
  }
49
80
  };
50
81
  export { JSXElementHelper as JSXElement };
@@ -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::5647ce9c10ba880cffece66b5924fd6e>>
3
+ * @codegen <<SignedSource::98174f9eecc3be9d3a54506bcf2994ef>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  export default {
@@ -34,6 +34,7 @@ export default {
34
34
  '@atlaskit/design-system/use-heading-level-in-spotlight-card': 'warn',
35
35
  '@atlaskit/design-system/use-href-in-link-item': 'warn',
36
36
  '@atlaskit/design-system/use-primitives': 'warn',
37
+ '@atlaskit/design-system/use-primitives-text': 'warn',
37
38
  '@atlaskit/design-system/use-visually-hidden': 'error'
38
39
  }
39
40
  };
@@ -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::0a2813f0b87e94d23e1f6fc388539c70>>
3
+ * @codegen <<SignedSource::2687f4b835e076cef09f1fbbfdfc1b06>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  import consistentCssPropUsage from './consistent-css-prop-usage';
@@ -31,6 +31,7 @@ import useDrawerLabel from './use-drawer-label';
31
31
  import useHeadingLevelInSpotlightCard from './use-heading-level-in-spotlight-card';
32
32
  import useHrefInLinkItem from './use-href-in-link-item';
33
33
  import usePrimitives from './use-primitives';
34
+ import usePrimitivesText from './use-primitives-text';
34
35
  import useVisuallyHidden from './use-visually-hidden';
35
36
  export default {
36
37
  'consistent-css-prop-usage': consistentCssPropUsage,
@@ -61,5 +62,6 @@ export default {
61
62
  'use-heading-level-in-spotlight-card': useHeadingLevelInSpotlightCard,
62
63
  'use-href-in-link-item': useHrefInLinkItem,
63
64
  'use-primitives': usePrimitives,
65
+ 'use-primitives-text': usePrimitivesText,
64
66
  'use-visually-hidden': useVisuallyHidden
65
67
  };
@@ -22,4 +22,7 @@ export var restrictedPaths = [{
22
22
  }, {
23
23
  path: '@dnd-kit',
24
24
  message: ''
25
+ }, {
26
+ path: '@formkit/drag-and-drop',
27
+ message: ''
25
28
  }];
@@ -5,7 +5,6 @@ import { getIdentifierInParentScope, isNodeOfType } from 'eslint-codemod-utils';
5
5
  import * as ast from '../../../../ast-nodes';
6
6
  import { getVariableDefinitionValue, getVariableUsagesCount, isValidCssPropertiesToTransform } from '../../utils';
7
7
  import { cssToXcssTransformer } from '../css-to-xcss';
8
- import { containsOnlySupportedAttrs } from './contains-only-supported-attrs';
9
8
  import * as supported from './supported';
10
9
  import { upsertImportDeclaration } from './upsert-import-declaration';
11
10
  export var EmotionCSS = {
@@ -55,7 +54,7 @@ export var EmotionCSS = {
55
54
  }
56
55
 
57
56
  // Ignore elements that contain dangerous attributes like `id`.
58
- if (!containsOnlySupportedAttrs(node)) {
57
+ if (!ast.JSXElement.hasAllowedAttrsOnly(node, supported.attributes)) {
59
58
  return false;
60
59
  }
61
60
 
@@ -0,0 +1,9 @@
1
+ var defaults = {
2
+ patterns: ['paragraph-elements', 'span-elements', 'strong-elements', 'emphasis-elements']
3
+ };
4
+ export var getConfig = function getConfig(overrides) {
5
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
6
+ // start with an empty object, then merge in the defaults, then merge in overrides.
7
+ // The empty object is returned, as well as modified in place
8
+ return Object.assign({}, defaults, overrides);
9
+ };
@@ -0,0 +1,51 @@
1
+ import { createLintRule } from '../utils/create-rule';
2
+ import { getConfig } from './config';
3
+ import { EmphasisElements, ParagraphElements, SpanElements, StrongElements } from './transformers';
4
+ var textDocsUrl = 'https://atlassian.design/components/primitives/text';
5
+ var rule = createLintRule({
6
+ meta: {
7
+ name: 'use-primitives-text',
8
+ type: 'suggestion',
9
+ fixable: 'code',
10
+ hasSuggestions: true,
11
+ docs: {
12
+ description: 'Encourage the usage of text components.',
13
+ recommended: false,
14
+ severity: 'warn'
15
+ },
16
+ messages: {
17
+ preferPrimitivesText: "This element can be replaced with a \"Text\" primitive. See ".concat(textDocsUrl, " for additional guidance."),
18
+ preferPrimitivesStackedText: "These paragraphs can be replaced with a \"Text\" and \"Stack\" primitives. See ".concat(textDocsUrl, " for additional guidance.")
19
+ }
20
+ },
21
+ create: function create(context) {
22
+ var config = getConfig(context.options[0]);
23
+ return {
24
+ 'JSXElement[openingElement.name.name=span]': function JSXElementOpeningElementNameNameSpan(node) {
25
+ return SpanElements.lint(node, {
26
+ context: context,
27
+ config: config
28
+ });
29
+ },
30
+ 'JSXElement[openingElement.name.name=p]': function JSXElementOpeningElementNameNameP(node) {
31
+ return ParagraphElements.lint(node, {
32
+ context: context,
33
+ config: config
34
+ });
35
+ },
36
+ 'JSXElement[openingElement.name.name=strong]': function JSXElementOpeningElementNameNameStrong(node) {
37
+ return StrongElements.lint(node, {
38
+ context: context,
39
+ config: config
40
+ });
41
+ },
42
+ 'JSXElement[openingElement.name.name=em]': function JSXElementOpeningElementNameNameEm(node) {
43
+ return EmphasisElements.lint(node, {
44
+ context: context,
45
+ config: config
46
+ });
47
+ }
48
+ };
49
+ }
50
+ });
51
+ export default rule;
@@ -0,0 +1,33 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import * as ast from '../../../ast-nodes';
3
+
4
+ // Rename data-testid prop to testId if present
5
+ export function updateTestIdAttributeFix(node, fixer) {
6
+ var testIdAttr = ast.JSXElement.getAttributeByName(node, 'data-testid');
7
+ if (testIdAttr) {
8
+ return ast.JSXAttribute.updateName(testIdAttr, 'testId', fixer);
9
+ }
10
+ }
11
+ export var allowedAttrs = ['id', 'data-testid', 'key'];
12
+
13
+ // Only allow elements with strings as children
14
+ // The use of `FormattedMessage` component and `formatMessage` are allowed as these are used for i18n
15
+ export function hasTextChildrenOnly(node) {
16
+ var _node$children;
17
+ return (_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.every(function (child) {
18
+ if (isNodeOfType(child, 'JSXText')) {
19
+ return true;
20
+ }
21
+
22
+ // JSX child element <span><FormattedMessage /></span>
23
+ if (isNodeOfType(child, 'JSXElement') && ast.JSXElement.getName(child) === 'FormattedMessage') {
24
+ return true;
25
+ }
26
+
27
+ // JSX expression <span>{formatMessage(...)}</span>
28
+ if (isNodeOfType(child, 'JSXExpressionContainer') && isNodeOfType(child.expression, 'CallExpression')) {
29
+ return ast.FunctionCall.getName(child.expression) === 'formatMessage';
30
+ }
31
+ return false;
32
+ });
33
+ }
@@ -0,0 +1,72 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ /* eslint-disable @repo/internal/react/require-jsdoc */
3
+
4
+ import { isNodeOfType } from 'eslint-codemod-utils';
5
+ import * as ast from '../../../ast-nodes';
6
+ import { upsertImportDeclaration } from '../../use-primitives/transformers/emotion-css/upsert-import-declaration';
7
+ import { allowedAttrs, updateTestIdAttributeFix } from './common';
8
+ export var EmphasisElements = {
9
+ lint: function lint(node, _ref) {
10
+ var context = _ref.context,
11
+ config = _ref.config;
12
+ if (!isNodeOfType(node, 'JSXElement')) {
13
+ return;
14
+ }
15
+
16
+ // Check whether all criteria needed to make a transformation are met
17
+ if (!EmphasisElements._check(node, {
18
+ context: context,
19
+ config: config
20
+ })) {
21
+ return;
22
+ }
23
+ context.report({
24
+ node: node.openingElement,
25
+ messageId: 'preferPrimitivesText',
26
+ suggest: [{
27
+ desc: "Convert to Text",
28
+ fix: EmphasisElements._fix(node, {
29
+ context: context
30
+ })
31
+ }]
32
+ });
33
+ },
34
+ _check: function _check(node, _ref2) {
35
+ var context = _ref2.context,
36
+ config = _ref2.config;
37
+ if (!config.patterns.includes('emphasis-elements')) {
38
+ return false;
39
+ }
40
+ var elementName = ast.JSXElement.getName(node);
41
+ if (elementName !== 'em') {
42
+ return false;
43
+ }
44
+
45
+ // Element has no unallowed props
46
+ if (!ast.JSXElement.hasAllowedAttrsOnly(node, allowedAttrs)) {
47
+ return false;
48
+ }
49
+
50
+ // If there is more than one `@atlaskit/primitives` import, then it becomes difficult to determine which import to transform
51
+ var importDeclaration = ast.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
52
+ if (importDeclaration.length > 1) {
53
+ return false;
54
+ }
55
+ return true;
56
+ },
57
+ _fix: function _fix(node, _ref3) {
58
+ var context = _ref3.context;
59
+ return function (fixer) {
60
+ var importFix = upsertImportDeclaration({
61
+ module: '@atlaskit/primitives',
62
+ specifiers: ['Text']
63
+ }, context, fixer);
64
+ var elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
65
+ var asAttributeFix = ast.JSXElement.addAttribute(node, 'as', 'em', fixer);
66
+ var testAttributeFix = updateTestIdAttributeFix(node, fixer);
67
+ return [importFix].concat(_toConsumableArray(elementNameFixes), [asAttributeFix, testAttributeFix]).filter(function (fix) {
68
+ return Boolean(fix);
69
+ }); // Some of the transformers can return arrays with undefined, so filter them out
70
+ };
71
+ }
72
+ };
@@ -0,0 +1,4 @@
1
+ export { EmphasisElements } from './emphasis-elements';
2
+ export { SpanElements } from './span-elements';
3
+ export { StrongElements } from './strong-elements';
4
+ export { ParagraphElements } from './paragraph-elements';
@@ -0,0 +1,186 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ /* eslint-disable @repo/internal/react/require-jsdoc */
3
+
4
+ import { isNodeOfType } from 'eslint-codemod-utils';
5
+ import * as ast from '../../../ast-nodes';
6
+ import { upsertImportDeclaration } from '../../use-primitives/transformers/emotion-css/upsert-import-declaration';
7
+ import { allowedAttrs, updateTestIdAttributeFix } from './common';
8
+ export var ParagraphElements = {
9
+ lint: function lint(node, _ref) {
10
+ var context = _ref.context,
11
+ config = _ref.config;
12
+ if (!isNodeOfType(node, 'JSXElement')) {
13
+ return;
14
+ }
15
+
16
+ // Check whether all criteria needed to make a transformation are met
17
+ var _ParagraphElements$_c = ParagraphElements._check(node, {
18
+ context: context,
19
+ config: config
20
+ }),
21
+ success = _ParagraphElements$_c.success,
22
+ refs = _ParagraphElements$_c.refs;
23
+ if (!success) {
24
+ return;
25
+ }
26
+ if (refs.siblings.length > 1) {
27
+ var _refs$siblings$0$loc, _refs$siblings$loc;
28
+ /**
29
+ * Highlighting from first opening element to last closing element
30
+ * to indicate fix will change all p elements and wrap them in a Stack,
31
+ * falls back to first opening element.
32
+ */
33
+ var startLoc = (_refs$siblings$0$loc = refs.siblings[0].loc) === null || _refs$siblings$0$loc === void 0 ? void 0 : _refs$siblings$0$loc.start;
34
+ var endLoc = (_refs$siblings$loc = refs.siblings[refs.siblings.length - 1].loc) === null || _refs$siblings$loc === void 0 ? void 0 : _refs$siblings$loc.end;
35
+ context.report({
36
+ loc: startLoc && endLoc && {
37
+ start: startLoc,
38
+ end: endLoc
39
+ },
40
+ node: node.openingElement,
41
+ messageId: 'preferPrimitivesStackedText',
42
+ suggest: [{
43
+ desc: 'Convert to Text and Stack',
44
+ fix: ParagraphElements._fixMultiple(node, {
45
+ context: context,
46
+ refs: refs
47
+ })
48
+ }]
49
+ });
50
+ } else {
51
+ context.report({
52
+ node: node,
53
+ messageId: 'preferPrimitivesText',
54
+ suggest: [{
55
+ desc: 'Convert to Text',
56
+ fix: ParagraphElements._fixSingle(node, {
57
+ context: context
58
+ })
59
+ }]
60
+ });
61
+ }
62
+ },
63
+ _check: function _check(node, _ref2) {
64
+ var context = _ref2.context,
65
+ config = _ref2.config;
66
+ if (!config.patterns.includes('paragraph-elements')) {
67
+ return {
68
+ success: false,
69
+ refs: {
70
+ siblings: []
71
+ }
72
+ };
73
+ }
74
+ var elementName = ast.JSXElement.getName(node);
75
+ if (elementName !== 'p') {
76
+ return {
77
+ success: false,
78
+ refs: {
79
+ siblings: []
80
+ }
81
+ };
82
+ }
83
+
84
+ // All siblings have to be paragraph elements with no unallowed props
85
+ if (!isNodeOfType(node.parent, 'JSXElement')) {
86
+ return {
87
+ success: false,
88
+ refs: {
89
+ siblings: []
90
+ }
91
+ };
92
+ }
93
+ var siblings = ast.JSXElement.getChildren(node.parent);
94
+ if (siblings.length > 1) {
95
+ var _siblings$0$range, _node$range, _siblings$0$range2, _node$range2;
96
+ // Only report for the first p element by comparing node location
97
+ if (((_siblings$0$range = siblings[0].range) === null || _siblings$0$range === void 0 ? void 0 : _siblings$0$range[0]) !== ((_node$range = node.range) === null || _node$range === void 0 ? void 0 : _node$range[0]) || ((_siblings$0$range2 = siblings[0].range) === null || _siblings$0$range2 === void 0 ? void 0 : _siblings$0$range2[1]) !== ((_node$range2 = node.range) === null || _node$range2 === void 0 ? void 0 : _node$range2[1])) {
98
+ return {
99
+ success: false,
100
+ refs: {
101
+ siblings: siblings
102
+ }
103
+ };
104
+ }
105
+ // Only report when every sibling is a p element
106
+ var siblingsMatch = siblings.every(function (child) {
107
+ if (!isNodeOfType(child, 'JSXElement')) {
108
+ return false;
109
+ }
110
+ if (ast.JSXElement.getName(child) !== 'p') {
111
+ return false;
112
+ }
113
+ return ast.JSXElement.hasAllowedAttrsOnly(child, allowedAttrs);
114
+ });
115
+ if (!siblingsMatch) {
116
+ return {
117
+ success: false,
118
+ refs: {
119
+ siblings: siblings
120
+ }
121
+ };
122
+ }
123
+ }
124
+ var importDeclaration = ast.Root.findImportsByModule(context.getSourceCode().ast.body, '@atlaskit/primitives');
125
+
126
+ // If there is more than one `@atlaskit/primitives` import, then it becomes difficult to determine which import to transform
127
+ if (importDeclaration.length > 1) {
128
+ return {
129
+ success: false,
130
+ refs: {
131
+ siblings: siblings
132
+ }
133
+ };
134
+ }
135
+ return {
136
+ success: true,
137
+ refs: {
138
+ siblings: siblings
139
+ }
140
+ };
141
+ },
142
+ _fixSingle: function _fixSingle(node, _ref3) {
143
+ var context = _ref3.context;
144
+ return function (fixer) {
145
+ var importFix = upsertImportDeclaration({
146
+ module: '@atlaskit/primitives',
147
+ specifiers: ['Text']
148
+ }, context, fixer);
149
+ var elementNameFixes = ast.JSXElement.updateName(node, 'Text', fixer);
150
+ var testAttributeFix = updateTestIdAttributeFix(node, fixer);
151
+ return [importFix].concat(_toConsumableArray(elementNameFixes), [testAttributeFix]).filter(function (fix) {
152
+ return Boolean(fix);
153
+ }); // Some of the transformers can return arrays with undefined, so filter them out
154
+ };
155
+ },
156
+ _fixMultiple: function _fixMultiple(node, _ref4) {
157
+ var context = _ref4.context,
158
+ refs = _ref4.refs;
159
+ return function (fixer) {
160
+ if (!isNodeOfType(node.parent, 'JSXElement') || !node.parent.closingElement) {
161
+ return [];
162
+ }
163
+ var importFix = upsertImportDeclaration({
164
+ module: '@atlaskit/primitives',
165
+ specifiers: ['Text', 'Stack']
166
+ }, context, fixer);
167
+
168
+ // Update all siblings elements and their attributes
169
+ var siblingFixes = refs.siblings.map(function (sibling) {
170
+ if (isNodeOfType(sibling, 'JSXElement')) {
171
+ var elementNameFixes = ast.JSXElement.updateName(sibling, 'Text', fixer);
172
+ var testAttributeFix = updateTestIdAttributeFix(sibling, fixer);
173
+ return [].concat(_toConsumableArray(elementNameFixes), [testAttributeFix]);
174
+ }
175
+ return undefined;
176
+ }).flat();
177
+
178
+ // Wrap in <Stack /> when more than 1 sibling
179
+ var wrapperOpenElementFix = fixer.insertTextAfter(node.parent.openingElement, "<Stack space='space.150'>");
180
+ var wrapperCloseElementFix = fixer.insertTextBefore(node.parent.closingElement, '</Stack>');
181
+ return [importFix].concat(_toConsumableArray(siblingFixes), [wrapperOpenElementFix, wrapperCloseElementFix]).filter(function (fix) {
182
+ return Boolean(fix);
183
+ }); // Some of the transformers can return arrays with undefined, so filter them out
184
+ };
185
+ }
186
+ };