@atlaskit/eslint-plugin-design-system 13.27.1 → 13.29.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 (38) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +2 -0
  3. package/dist/cjs/presets/all-flat.codegen.js +3 -1
  4. package/dist/cjs/presets/all.codegen.js +3 -1
  5. package/dist/cjs/presets/recommended-flat.codegen.js +3 -1
  6. package/dist/cjs/presets/recommended.codegen.js +3 -1
  7. package/dist/cjs/rules/index.codegen.js +5 -1
  8. package/dist/cjs/rules/use-character-counter-field/index.js +139 -0
  9. package/dist/cjs/rules/use-field-message-wrapper/index.js +125 -0
  10. package/dist/es2019/presets/all-flat.codegen.js +3 -1
  11. package/dist/es2019/presets/all.codegen.js +3 -1
  12. package/dist/es2019/presets/recommended-flat.codegen.js +3 -1
  13. package/dist/es2019/presets/recommended.codegen.js +3 -1
  14. package/dist/es2019/rules/index.codegen.js +5 -1
  15. package/dist/es2019/rules/use-character-counter-field/index.js +131 -0
  16. package/dist/es2019/rules/use-field-message-wrapper/index.js +115 -0
  17. package/dist/esm/presets/all-flat.codegen.js +3 -1
  18. package/dist/esm/presets/all.codegen.js +3 -1
  19. package/dist/esm/presets/recommended-flat.codegen.js +3 -1
  20. package/dist/esm/presets/recommended.codegen.js +3 -1
  21. package/dist/esm/rules/index.codegen.js +5 -1
  22. package/dist/esm/rules/use-character-counter-field/index.js +133 -0
  23. package/dist/esm/rules/use-field-message-wrapper/index.js +119 -0
  24. package/dist/types/presets/all-flat.codegen.d.ts +1 -1
  25. package/dist/types/presets/all.codegen.d.ts +1 -1
  26. package/dist/types/presets/recommended-flat.codegen.d.ts +1 -1
  27. package/dist/types/presets/recommended.codegen.d.ts +1 -1
  28. package/dist/types/rules/index.codegen.d.ts +1 -1
  29. package/dist/types/rules/use-character-counter-field/index.d.ts +5 -0
  30. package/dist/types/rules/use-field-message-wrapper/index.d.ts +3 -0
  31. package/dist/types-ts4.5/presets/all-flat.codegen.d.ts +1 -1
  32. package/dist/types-ts4.5/presets/all.codegen.d.ts +1 -1
  33. package/dist/types-ts4.5/presets/recommended-flat.codegen.d.ts +1 -1
  34. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +1 -1
  35. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -1
  36. package/dist/types-ts4.5/rules/use-character-counter-field/index.d.ts +5 -0
  37. package/dist/types-ts4.5/rules/use-field-message-wrapper/index.d.ts +3 -0
  38. package/package.json +1 -1
@@ -0,0 +1,131 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import { JSXElementHelper } from '../../ast-nodes/jsx-element';
3
+ import { createLintRule } from '../utils/create-rule';
4
+ const TEXTFIELD_PACKAGE = '@atlaskit/textfield';
5
+ const TEXTAREA_PACKAGE = '@atlaskit/textarea';
6
+ const FORM_PACKAGE = '@atlaskit/form';
7
+ export const message = 'When using `maxLength` or `minLength` props with Textfield/Textarea inside a Form, use `CharacterCounterField` from `@atlaskit/form` instead of Field and remove the props from the Textfield/Textarea. This ensures accessibility through real time feedback and aligns with the design system. Read more about [character counter fields](https://atlassian.design/components/form/examples#character-counter-field)';
8
+ export const ruleName = __dirname.split('/').slice(-1)[0];
9
+
10
+ /**
11
+ * Check if a node is inside a Form component or Field component
12
+ */
13
+ function isInsideFormOrField(node, formComponentNames) {
14
+ let current = node;
15
+
16
+ // Traverse up the AST to find parent JSX elements
17
+ while (current) {
18
+ const parent = current.parent;
19
+ if (!parent) {
20
+ break;
21
+ }
22
+
23
+ // Check if parent is a JSXElement with a name we're tracking
24
+ if (isNodeOfType(parent, 'JSXElement') && isNodeOfType(parent.openingElement.name, 'JSXIdentifier')) {
25
+ const parentName = parent.openingElement.name.name;
26
+
27
+ // Check if this is a Form component or Field component
28
+ if (formComponentNames.has(parentName)) {
29
+ return true;
30
+ }
31
+ }
32
+
33
+ // Check if parent is inside a JSXExpressionContainer (render prop pattern)
34
+ // This handles: <Field>{({ fieldProps }) => <Textfield {...fieldProps} />}</Field>
35
+ if (isNodeOfType(parent, 'JSXExpressionContainer')) {
36
+ // Continue traversing up to find the Field/Form
37
+ current = parent;
38
+ continue;
39
+ }
40
+
41
+ // Check if parent is an arrow function (render prop)
42
+ if (isNodeOfType(parent, 'ArrowFunctionExpression')) {
43
+ current = parent;
44
+ continue;
45
+ }
46
+ current = parent;
47
+ }
48
+ return false;
49
+ }
50
+ const rule = createLintRule({
51
+ meta: {
52
+ name: ruleName,
53
+ type: 'suggestion',
54
+ docs: {
55
+ description: 'Suggests using CharacterCounterField when Textfield or Textarea components have maxLength or minLength props within a Form.',
56
+ recommended: true,
57
+ severity: 'warn'
58
+ },
59
+ messages: {
60
+ useCharacterCounterField: message
61
+ }
62
+ },
63
+ create(context) {
64
+ // Track imported component names
65
+ const importedComponents = new Map();
66
+ const formComponentNames = new Set();
67
+ return {
68
+ ImportDeclaration(node) {
69
+ const source = node.source.value;
70
+ if (typeof source !== 'string') {
71
+ return;
72
+ }
73
+
74
+ // Track Form package imports (Form, Field, etc.)
75
+ if (source === FORM_PACKAGE) {
76
+ node.specifiers.forEach(spec => {
77
+ if (isNodeOfType(spec, 'ImportDefaultSpecifier')) {
78
+ // Default import: import Form from '@atlaskit/form'
79
+ formComponentNames.add(spec.local.name);
80
+ } else if (isNodeOfType(spec, 'ImportSpecifier')) {
81
+ // Named import: import { Form, Field } from '@atlaskit/form'
82
+ formComponentNames.add(spec.local.name);
83
+ }
84
+ });
85
+ return;
86
+ }
87
+
88
+ // Track Textfield and Textarea imports
89
+ if (source === TEXTFIELD_PACKAGE || source === TEXTAREA_PACKAGE) {
90
+ const defaultImport = node.specifiers.find(spec => isNodeOfType(spec, 'ImportDefaultSpecifier'));
91
+ if (defaultImport && isNodeOfType(defaultImport, 'ImportDefaultSpecifier')) {
92
+ const localName = defaultImport.local.name;
93
+ importedComponents.set(localName, {
94
+ localName,
95
+ package: source
96
+ });
97
+ }
98
+ }
99
+ },
100
+ JSXElement(node) {
101
+ if (!isNodeOfType(node, 'JSXElement')) {
102
+ return;
103
+ }
104
+ if (!isNodeOfType(node.openingElement.name, 'JSXIdentifier')) {
105
+ return;
106
+ }
107
+ const elementName = node.openingElement.name.name;
108
+
109
+ // Check if this element is one of our tracked components
110
+ const componentInfo = importedComponents.get(elementName);
111
+ if (!componentInfo) {
112
+ return;
113
+ }
114
+
115
+ // Check if the component has maxLength or minLength props
116
+ const hasMaxLength = JSXElementHelper.getAttributeByName(node, 'maxLength');
117
+ const hasMinLength = JSXElementHelper.getAttributeByName(node, 'minLength');
118
+ if (hasMaxLength || hasMinLength) {
119
+ // Only warn if inside a Form or Field component
120
+ if (isInsideFormOrField(node, formComponentNames)) {
121
+ context.report({
122
+ node,
123
+ messageId: 'useCharacterCounterField'
124
+ });
125
+ }
126
+ }
127
+ }
128
+ };
129
+ }
130
+ });
131
+ export default rule;
@@ -0,0 +1,115 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import { createLintRule } from '../utils/create-rule';
3
+ const FORM_PACKAGE = '@atlaskit/form';
4
+ const MESSAGE_COMPONENTS = ['ErrorMessage', 'HelperMessage', 'ValidMessage'];
5
+ const rule = createLintRule({
6
+ meta: {
7
+ name: 'use-field-message-wrapper',
8
+ type: 'suggestion',
9
+ hasSuggestions: true,
10
+ docs: {
11
+ description: 'Encourage use of message wrapper component when using form message components.',
12
+ recommended: true,
13
+ severity: 'warn'
14
+ },
15
+ messages: {
16
+ useMessageWrapper: `All ADS form messaging components within a field should be wrapped by the \`MessageWrapper\` component from the form package. Consider also using the [simplified field implementation](https://atlassian.design/components/form/examples#simple-implementation-1) to handle styling and accessible messaging directly.`
17
+ }
18
+ },
19
+ create(context) {
20
+ let fieldImport;
21
+ let messageWrapperImport;
22
+ let messageImports = [];
23
+ return {
24
+ ImportDeclaration(node) {
25
+ const source = node.source.value;
26
+
27
+ // Ignore anomalies
28
+ if (typeof source !== 'string') {
29
+ return;
30
+ }
31
+ if (!node.specifiers.length) {
32
+ return;
33
+ }
34
+
35
+ // If it's not from our package, ignore.
36
+ if (source !== FORM_PACKAGE) {
37
+ return;
38
+ }
39
+ const namedImportSpecifiers = node.specifiers.filter(spec => isNodeOfType(spec, 'ImportSpecifier'));
40
+ namedImportSpecifiers.forEach(spec => {
41
+ if (spec.type === 'ImportSpecifier' && 'name' in spec.imported) {
42
+ const name = spec.imported.name;
43
+ const local = spec.local;
44
+ if (MESSAGE_COMPONENTS.includes(name)) {
45
+ messageImports.push(local);
46
+ } else if (name === 'Field') {
47
+ fieldImport = local;
48
+ } else if (name === 'MessageWrapper') {
49
+ messageWrapperImport = local;
50
+ }
51
+ }
52
+ });
53
+ },
54
+ JSXElement(node) {
55
+ if (!isNodeOfType(node, 'JSXElement')) {
56
+ return;
57
+ }
58
+ if (!isNodeOfType(node.openingElement.name, 'JSXIdentifier')) {
59
+ return;
60
+ }
61
+ const name = node.openingElement.name.name;
62
+
63
+ // if it's not a message component, skip
64
+ if (messageImports.length === 0 || !messageImports.find(imp => imp.name === name)) {
65
+ return;
66
+ }
67
+
68
+ // if no field import exists, skip. It needs to be within our field
69
+ if (!fieldImport) {
70
+ return;
71
+ }
72
+
73
+ // If no message wrapper import exists, then it's definitely an error
74
+ if (!messageWrapperImport) {
75
+ return context.report({
76
+ node: node,
77
+ messageId: 'useMessageWrapper'
78
+ });
79
+ }
80
+
81
+ // check for if field and message wrapper are parents
82
+ let _node = node;
83
+ let hasParentField = false;
84
+ let hasParentMessageWrapper = false;
85
+ while (isNodeOfType(_node, 'JSXElement') && isNodeOfType(_node.openingElement.name, 'JSXIdentifier') && !hasParentField) {
86
+ const name = _node.openingElement.name.name;
87
+ hasParentField = hasParentField || name === fieldImport.name;
88
+ hasParentMessageWrapper = hasParentMessageWrapper || name === messageWrapperImport.name;
89
+ _node = _node.parent;
90
+ // Skip up until a JSXElement is reached
91
+ if (isNodeOfType(_node, 'JSXFragment') || isNodeOfType(_node, 'ArrowFunctionExpression') || isNodeOfType(_node, 'JSXExpressionContainer')) {
92
+ while (_node && !isNodeOfType(_node, 'JSXElement') && !isNodeOfType(_node, 'Program')) {
93
+ _node = _node.parent;
94
+ }
95
+ }
96
+ }
97
+
98
+ // if not field, skip because this doesn't matter
99
+ if (!hasParentField) {
100
+ return;
101
+ }
102
+
103
+ // if it has a message wrapper, skip
104
+ if (hasParentMessageWrapper) {
105
+ return;
106
+ }
107
+ context.report({
108
+ node: node,
109
+ messageId: 'useMessageWrapper'
110
+ });
111
+ }
112
+ };
113
+ }
114
+ });
115
+ export default rule;
@@ -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::d7a0407a6c6b10bfbba790523d06f97d>>
3
+ * @codegen <<SignedSource::131350ebc877f42247042c6e7327fe18>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
 
@@ -56,10 +56,12 @@ var rules = {
56
56
  '@atlaskit/design-system/no-utility-icons': 'warn',
57
57
  '@atlaskit/design-system/prefer-primitives': 'warn',
58
58
  '@atlaskit/design-system/use-button-group-label': 'warn',
59
+ '@atlaskit/design-system/use-character-counter-field': 'warn',
59
60
  '@atlaskit/design-system/use-correct-field': 'warn',
60
61
  '@atlaskit/design-system/use-cx-function-in-xcss': 'error',
61
62
  '@atlaskit/design-system/use-datetime-picker-calendar-button': 'warn',
62
63
  '@atlaskit/design-system/use-drawer-label': 'warn',
64
+ '@atlaskit/design-system/use-field-message-wrapper': 'warn',
63
65
  '@atlaskit/design-system/use-heading': 'warn',
64
66
  '@atlaskit/design-system/use-heading-level-in-spotlight-card': 'warn',
65
67
  '@atlaskit/design-system/use-href-in-link-item': 'warn',
@@ -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::e632a96e9bae920c23e25114f40e3c0d>>
3
+ * @codegen <<SignedSource::b3ddf9ee0a27aaf8a318a6579d48ce99>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
 
@@ -55,10 +55,12 @@ var rules = {
55
55
  '@atlaskit/design-system/no-utility-icons': 'warn',
56
56
  '@atlaskit/design-system/prefer-primitives': 'warn',
57
57
  '@atlaskit/design-system/use-button-group-label': 'warn',
58
+ '@atlaskit/design-system/use-character-counter-field': 'warn',
58
59
  '@atlaskit/design-system/use-correct-field': 'warn',
59
60
  '@atlaskit/design-system/use-cx-function-in-xcss': 'error',
60
61
  '@atlaskit/design-system/use-datetime-picker-calendar-button': 'warn',
61
62
  '@atlaskit/design-system/use-drawer-label': 'warn',
63
+ '@atlaskit/design-system/use-field-message-wrapper': 'warn',
62
64
  '@atlaskit/design-system/use-heading': 'warn',
63
65
  '@atlaskit/design-system/use-heading-level-in-spotlight-card': 'warn',
64
66
  '@atlaskit/design-system/use-href-in-link-item': 'warn',
@@ -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::e2157edd1fabfe25390d7c22a0b63f86>>
3
+ * @codegen <<SignedSource::ba71ef89188bdc20d0ec948cc00f6436>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
 
@@ -39,10 +39,12 @@ var rules = {
39
39
  '@atlaskit/design-system/no-unused-css-map': 'warn',
40
40
  '@atlaskit/design-system/no-utility-icons': 'warn',
41
41
  '@atlaskit/design-system/use-button-group-label': 'warn',
42
+ '@atlaskit/design-system/use-character-counter-field': 'warn',
42
43
  '@atlaskit/design-system/use-correct-field': 'warn',
43
44
  '@atlaskit/design-system/use-cx-function-in-xcss': 'error',
44
45
  '@atlaskit/design-system/use-datetime-picker-calendar-button': 'warn',
45
46
  '@atlaskit/design-system/use-drawer-label': 'warn',
47
+ '@atlaskit/design-system/use-field-message-wrapper': 'warn',
46
48
  '@atlaskit/design-system/use-heading': 'warn',
47
49
  '@atlaskit/design-system/use-heading-level-in-spotlight-card': 'warn',
48
50
  '@atlaskit/design-system/use-href-in-link-item': 'warn',
@@ -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::b3fadf2e122fc30002c54a922fc55a1f>>
3
+ * @codegen <<SignedSource::5c119e0ab08e1f39d471b2b827bbb4de>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
 
@@ -38,10 +38,12 @@ var rules = {
38
38
  '@atlaskit/design-system/no-unused-css-map': 'warn',
39
39
  '@atlaskit/design-system/no-utility-icons': 'warn',
40
40
  '@atlaskit/design-system/use-button-group-label': 'warn',
41
+ '@atlaskit/design-system/use-character-counter-field': 'warn',
41
42
  '@atlaskit/design-system/use-correct-field': 'warn',
42
43
  '@atlaskit/design-system/use-cx-function-in-xcss': 'error',
43
44
  '@atlaskit/design-system/use-datetime-picker-calendar-button': 'warn',
44
45
  '@atlaskit/design-system/use-drawer-label': 'warn',
46
+ '@atlaskit/design-system/use-field-message-wrapper': 'warn',
45
47
  '@atlaskit/design-system/use-heading': 'warn',
46
48
  '@atlaskit/design-system/use-heading-level-in-spotlight-card': 'warn',
47
49
  '@atlaskit/design-system/use-href-in-link-item': 'warn',
@@ -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::62347cf64e4ac99b927fce9d1a2bd894>>
3
+ * @codegen <<SignedSource::02be73e0813b6f22038f0f3f2ad6865d>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
 
@@ -53,10 +53,12 @@ import noUnusedCssMap from './no-unused-css-map';
53
53
  import noUtilityIcons from './no-utility-icons';
54
54
  import preferPrimitives from './prefer-primitives';
55
55
  import useButtonGroupLabel from './use-button-group-label';
56
+ import useCharacterCounterField from './use-character-counter-field';
56
57
  import useCorrectField from './use-correct-field';
57
58
  import useCxFunctionInXcss from './use-cx-function-in-xcss';
58
59
  import useDatetimePickerCalendarButton from './use-datetime-picker-calendar-button';
59
60
  import useDrawerLabel from './use-drawer-label';
61
+ import useFieldMessageWrapper from './use-field-message-wrapper';
60
62
  import useHeading from './use-heading';
61
63
  import useHeadingLevelInSpotlightCard from './use-heading-level-in-spotlight-card';
62
64
  import useHrefInLinkItem from './use-href-in-link-item';
@@ -125,10 +127,12 @@ export var rules = {
125
127
  'no-utility-icons': noUtilityIcons,
126
128
  'prefer-primitives': preferPrimitives,
127
129
  'use-button-group-label': useButtonGroupLabel,
130
+ 'use-character-counter-field': useCharacterCounterField,
128
131
  'use-correct-field': useCorrectField,
129
132
  'use-cx-function-in-xcss': useCxFunctionInXcss,
130
133
  'use-datetime-picker-calendar-button': useDatetimePickerCalendarButton,
131
134
  'use-drawer-label': useDrawerLabel,
135
+ 'use-field-message-wrapper': useFieldMessageWrapper,
132
136
  'use-heading': useHeading,
133
137
  'use-heading-level-in-spotlight-card': useHeadingLevelInSpotlightCard,
134
138
  'use-href-in-link-item': useHrefInLinkItem,
@@ -0,0 +1,133 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import { JSXElementHelper } from '../../ast-nodes/jsx-element';
3
+ import { createLintRule } from '../utils/create-rule';
4
+ var TEXTFIELD_PACKAGE = '@atlaskit/textfield';
5
+ var TEXTAREA_PACKAGE = '@atlaskit/textarea';
6
+ var FORM_PACKAGE = '@atlaskit/form';
7
+ export var message = 'When using `maxLength` or `minLength` props with Textfield/Textarea inside a Form, use `CharacterCounterField` from `@atlaskit/form` instead of Field and remove the props from the Textfield/Textarea. This ensures accessibility through real time feedback and aligns with the design system. Read more about [character counter fields](https://atlassian.design/components/form/examples#character-counter-field)';
8
+ export var ruleName = __dirname.split('/').slice(-1)[0];
9
+
10
+ /**
11
+ * Check if a node is inside a Form component or Field component
12
+ */
13
+ function isInsideFormOrField(node, formComponentNames) {
14
+ var current = node;
15
+
16
+ // Traverse up the AST to find parent JSX elements
17
+ while (current) {
18
+ var parent = current.parent;
19
+ if (!parent) {
20
+ break;
21
+ }
22
+
23
+ // Check if parent is a JSXElement with a name we're tracking
24
+ if (isNodeOfType(parent, 'JSXElement') && isNodeOfType(parent.openingElement.name, 'JSXIdentifier')) {
25
+ var parentName = parent.openingElement.name.name;
26
+
27
+ // Check if this is a Form component or Field component
28
+ if (formComponentNames.has(parentName)) {
29
+ return true;
30
+ }
31
+ }
32
+
33
+ // Check if parent is inside a JSXExpressionContainer (render prop pattern)
34
+ // This handles: <Field>{({ fieldProps }) => <Textfield {...fieldProps} />}</Field>
35
+ if (isNodeOfType(parent, 'JSXExpressionContainer')) {
36
+ // Continue traversing up to find the Field/Form
37
+ current = parent;
38
+ continue;
39
+ }
40
+
41
+ // Check if parent is an arrow function (render prop)
42
+ if (isNodeOfType(parent, 'ArrowFunctionExpression')) {
43
+ current = parent;
44
+ continue;
45
+ }
46
+ current = parent;
47
+ }
48
+ return false;
49
+ }
50
+ var rule = createLintRule({
51
+ meta: {
52
+ name: ruleName,
53
+ type: 'suggestion',
54
+ docs: {
55
+ description: 'Suggests using CharacterCounterField when Textfield or Textarea components have maxLength or minLength props within a Form.',
56
+ recommended: true,
57
+ severity: 'warn'
58
+ },
59
+ messages: {
60
+ useCharacterCounterField: message
61
+ }
62
+ },
63
+ create: function create(context) {
64
+ // Track imported component names
65
+ var importedComponents = new Map();
66
+ var formComponentNames = new Set();
67
+ return {
68
+ ImportDeclaration: function ImportDeclaration(node) {
69
+ var source = node.source.value;
70
+ if (typeof source !== 'string') {
71
+ return;
72
+ }
73
+
74
+ // Track Form package imports (Form, Field, etc.)
75
+ if (source === FORM_PACKAGE) {
76
+ node.specifiers.forEach(function (spec) {
77
+ if (isNodeOfType(spec, 'ImportDefaultSpecifier')) {
78
+ // Default import: import Form from '@atlaskit/form'
79
+ formComponentNames.add(spec.local.name);
80
+ } else if (isNodeOfType(spec, 'ImportSpecifier')) {
81
+ // Named import: import { Form, Field } from '@atlaskit/form'
82
+ formComponentNames.add(spec.local.name);
83
+ }
84
+ });
85
+ return;
86
+ }
87
+
88
+ // Track Textfield and Textarea imports
89
+ if (source === TEXTFIELD_PACKAGE || source === TEXTAREA_PACKAGE) {
90
+ var defaultImport = node.specifiers.find(function (spec) {
91
+ return isNodeOfType(spec, 'ImportDefaultSpecifier');
92
+ });
93
+ if (defaultImport && isNodeOfType(defaultImport, 'ImportDefaultSpecifier')) {
94
+ var localName = defaultImport.local.name;
95
+ importedComponents.set(localName, {
96
+ localName: localName,
97
+ package: source
98
+ });
99
+ }
100
+ }
101
+ },
102
+ JSXElement: function JSXElement(node) {
103
+ if (!isNodeOfType(node, 'JSXElement')) {
104
+ return;
105
+ }
106
+ if (!isNodeOfType(node.openingElement.name, 'JSXIdentifier')) {
107
+ return;
108
+ }
109
+ var elementName = node.openingElement.name.name;
110
+
111
+ // Check if this element is one of our tracked components
112
+ var componentInfo = importedComponents.get(elementName);
113
+ if (!componentInfo) {
114
+ return;
115
+ }
116
+
117
+ // Check if the component has maxLength or minLength props
118
+ var hasMaxLength = JSXElementHelper.getAttributeByName(node, 'maxLength');
119
+ var hasMinLength = JSXElementHelper.getAttributeByName(node, 'minLength');
120
+ if (hasMaxLength || hasMinLength) {
121
+ // Only warn if inside a Form or Field component
122
+ if (isInsideFormOrField(node, formComponentNames)) {
123
+ context.report({
124
+ node: node,
125
+ messageId: 'useCharacterCounterField'
126
+ });
127
+ }
128
+ }
129
+ }
130
+ };
131
+ }
132
+ });
133
+ export default rule;
@@ -0,0 +1,119 @@
1
+ import { isNodeOfType } from 'eslint-codemod-utils';
2
+ import { createLintRule } from '../utils/create-rule';
3
+ var FORM_PACKAGE = '@atlaskit/form';
4
+ var MESSAGE_COMPONENTS = ['ErrorMessage', 'HelperMessage', 'ValidMessage'];
5
+ var rule = createLintRule({
6
+ meta: {
7
+ name: 'use-field-message-wrapper',
8
+ type: 'suggestion',
9
+ hasSuggestions: true,
10
+ docs: {
11
+ description: 'Encourage use of message wrapper component when using form message components.',
12
+ recommended: true,
13
+ severity: 'warn'
14
+ },
15
+ messages: {
16
+ useMessageWrapper: "All ADS form messaging components within a field should be wrapped by the `MessageWrapper` component from the form package. Consider also using the [simplified field implementation](https://atlassian.design/components/form/examples#simple-implementation-1) to handle styling and accessible messaging directly."
17
+ }
18
+ },
19
+ create: function create(context) {
20
+ var fieldImport;
21
+ var messageWrapperImport;
22
+ var messageImports = [];
23
+ return {
24
+ ImportDeclaration: function ImportDeclaration(node) {
25
+ var source = node.source.value;
26
+
27
+ // Ignore anomalies
28
+ if (typeof source !== 'string') {
29
+ return;
30
+ }
31
+ if (!node.specifiers.length) {
32
+ return;
33
+ }
34
+
35
+ // If it's not from our package, ignore.
36
+ if (source !== FORM_PACKAGE) {
37
+ return;
38
+ }
39
+ var namedImportSpecifiers = node.specifiers.filter(function (spec) {
40
+ return isNodeOfType(spec, 'ImportSpecifier');
41
+ });
42
+ namedImportSpecifiers.forEach(function (spec) {
43
+ if (spec.type === 'ImportSpecifier' && 'name' in spec.imported) {
44
+ var name = spec.imported.name;
45
+ var local = spec.local;
46
+ if (MESSAGE_COMPONENTS.includes(name)) {
47
+ messageImports.push(local);
48
+ } else if (name === 'Field') {
49
+ fieldImport = local;
50
+ } else if (name === 'MessageWrapper') {
51
+ messageWrapperImport = local;
52
+ }
53
+ }
54
+ });
55
+ },
56
+ JSXElement: function JSXElement(node) {
57
+ if (!isNodeOfType(node, 'JSXElement')) {
58
+ return;
59
+ }
60
+ if (!isNodeOfType(node.openingElement.name, 'JSXIdentifier')) {
61
+ return;
62
+ }
63
+ var name = node.openingElement.name.name;
64
+
65
+ // if it's not a message component, skip
66
+ if (messageImports.length === 0 || !messageImports.find(function (imp) {
67
+ return imp.name === name;
68
+ })) {
69
+ return;
70
+ }
71
+
72
+ // if no field import exists, skip. It needs to be within our field
73
+ if (!fieldImport) {
74
+ return;
75
+ }
76
+
77
+ // If no message wrapper import exists, then it's definitely an error
78
+ if (!messageWrapperImport) {
79
+ return context.report({
80
+ node: node,
81
+ messageId: 'useMessageWrapper'
82
+ });
83
+ }
84
+
85
+ // check for if field and message wrapper are parents
86
+ var _node = node;
87
+ var hasParentField = false;
88
+ var hasParentMessageWrapper = false;
89
+ while (isNodeOfType(_node, 'JSXElement') && isNodeOfType(_node.openingElement.name, 'JSXIdentifier') && !hasParentField) {
90
+ var _name = _node.openingElement.name.name;
91
+ hasParentField = hasParentField || _name === fieldImport.name;
92
+ hasParentMessageWrapper = hasParentMessageWrapper || _name === messageWrapperImport.name;
93
+ _node = _node.parent;
94
+ // Skip up until a JSXElement is reached
95
+ if (isNodeOfType(_node, 'JSXFragment') || isNodeOfType(_node, 'ArrowFunctionExpression') || isNodeOfType(_node, 'JSXExpressionContainer')) {
96
+ while (_node && !isNodeOfType(_node, 'JSXElement') && !isNodeOfType(_node, 'Program')) {
97
+ _node = _node.parent;
98
+ }
99
+ }
100
+ }
101
+
102
+ // if not field, skip because this doesn't matter
103
+ if (!hasParentField) {
104
+ return;
105
+ }
106
+
107
+ // if it has a message wrapper, skip
108
+ if (hasParentMessageWrapper) {
109
+ return;
110
+ }
111
+ context.report({
112
+ node: node,
113
+ messageId: 'useMessageWrapper'
114
+ });
115
+ }
116
+ };
117
+ }
118
+ });
119
+ export default rule;
@@ -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::d7a0407a6c6b10bfbba790523d06f97d>>
3
+ * @codegen <<SignedSource::131350ebc877f42247042c6e7327fe18>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  import type { Linter } from 'eslint';
@@ -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::e632a96e9bae920c23e25114f40e3c0d>>
3
+ * @codegen <<SignedSource::b3ddf9ee0a27aaf8a318a6579d48ce99>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  import type { ESLint } from 'eslint';
@@ -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::e2157edd1fabfe25390d7c22a0b63f86>>
3
+ * @codegen <<SignedSource::ba71ef89188bdc20d0ec948cc00f6436>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  import type { Linter } from 'eslint';
@@ -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::b3fadf2e122fc30002c54a922fc55a1f>>
3
+ * @codegen <<SignedSource::5c119e0ab08e1f39d471b2b827bbb4de>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  import type { ESLint } from 'eslint';
@@ -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::62347cf64e4ac99b927fce9d1a2bd894>>
3
+ * @codegen <<SignedSource::02be73e0813b6f22038f0f3f2ad6865d>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
5
5
  */
6
6
  import type { Rule } from 'eslint';
@@ -0,0 +1,5 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const message = "When using `maxLength` or `minLength` props with Textfield/Textarea inside a Form, use `CharacterCounterField` from `@atlaskit/form` instead of Field and remove the props from the Textfield/Textarea. This ensures accessibility through real time feedback and aligns with the design system. Read more about [character counter fields](https://atlassian.design/components/form/examples#character-counter-field)";
3
+ export declare const ruleName: string;
4
+ declare const rule: Rule.RuleModule;
5
+ export default rule;