@atlaskit/eslint-plugin-design-system 9.7.0 → 10.0.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.
- package/CHANGELOG.md +18 -0
- package/README.md +6 -5
- package/constellation/consistent-css-prop-usage/usage.mdx +44 -30
- package/constellation/ensure-design-token-usage/usage.mdx +12 -7
- package/constellation/ensure-design-token-usage-preview/usage.mdx +2 -1
- package/constellation/icon-label/usage.mdx +5 -3
- package/constellation/index/usage.mdx +4 -2
- package/constellation/no-banned-imports/usage.mdx +3 -1
- package/constellation/no-css-tagged-template-expression/usage.mdx +22 -17
- package/constellation/no-deprecated-apis/usage.mdx +33 -27
- package/constellation/no-deprecated-design-token-usage/usage.mdx +7 -4
- package/constellation/no-deprecated-imports/usage.mdx +31 -27
- package/constellation/no-direct-use-of-web-platform-drag-and-drop/usage.mdx +19 -10
- package/constellation/no-empty-styled-expression/usage.mdx +19 -14
- package/constellation/no-exported-css/usage.mdx +15 -10
- package/constellation/no-exported-keyframes/usage.mdx +15 -10
- package/constellation/no-html-anchor/usage.mdx +40 -0
- package/constellation/no-html-button/usage.mdx +52 -0
- package/constellation/no-invalid-css-map/usage.mdx +69 -58
- package/constellation/no-keyframes-tagged-template-expression/usage.mdx +24 -18
- package/constellation/no-margin/usage.mdx +3 -2
- package/constellation/no-nested-styles/usage.mdx +16 -16
- package/constellation/no-physical-properties/usage.mdx +13 -13
- package/constellation/no-styled-tagged-template-expression/usage.mdx +39 -34
- package/constellation/no-unsafe-design-token-usage/usage.mdx +8 -7
- package/constellation/no-unsafe-style-overrides/usage.mdx +10 -7
- package/constellation/no-unsupported-drag-and-drop-libraries/usage.mdx +8 -2
- package/constellation/prefer-primitives/usage.mdx +3 -2
- package/constellation/use-button-group-label/usage.mdx +4 -2
- package/constellation/use-drawer-label/usage.mdx +4 -2
- package/constellation/use-heading/usage.mdx +7 -4
- package/constellation/use-heading-level-in-spotlight-card/usage.mdx +3 -2
- package/constellation/use-href-in-link-item/usage.mdx +2 -1
- package/constellation/use-popup-label/usage.mdx +5 -2
- package/constellation/use-primitives/usage.mdx +40 -39
- package/constellation/use-primitives-text/usage.mdx +7 -3
- package/constellation/use-tokens-space/usage.mdx +7 -3
- package/constellation/use-tokens-typography/usage.mdx +15 -6
- package/constellation/use-visually-hidden/usage.mdx +2 -1
- package/dist/cjs/common/token-maps.partial.js +49 -0
- package/dist/cjs/index.codegen.js +1 -1
- package/dist/cjs/presets/all.codegen.js +3 -2
- package/dist/cjs/presets/recommended.codegen.js +1 -1
- package/dist/cjs/rules/index.codegen.js +5 -3
- package/dist/cjs/rules/no-html-anchor/index.js +39 -0
- package/dist/cjs/rules/no-html-anchor/node-types/index.js +19 -0
- package/dist/cjs/rules/no-html-anchor/node-types/jsx-element/index.js +28 -0
- package/dist/cjs/rules/no-html-anchor/node-types/styled-component/get-styled-component-call.js +47 -0
- package/dist/cjs/rules/no-html-anchor/node-types/styled-component/index.js +37 -0
- package/dist/cjs/rules/no-html-anchor/node-types/supported.js +66 -0
- package/dist/cjs/rules/no-html-anchor/utils/get-jsx-element-by-name.js +53 -0
- package/dist/cjs/rules/no-html-button/index.js +39 -0
- package/dist/cjs/rules/no-html-button/node-types/index.js +19 -0
- package/dist/cjs/rules/no-html-button/node-types/jsx-element/index.js +28 -0
- package/dist/cjs/rules/no-html-button/node-types/styled-component/get-styled-component-call.js +47 -0
- package/dist/cjs/rules/no-html-button/node-types/styled-component/index.js +37 -0
- package/dist/cjs/rules/no-html-button/node-types/supported.js +79 -0
- package/dist/cjs/rules/no-html-button/utils/get-jsx-element-by-name.js +53 -0
- package/dist/es2019/common/token-maps.partial.js +42 -0
- package/dist/es2019/index.codegen.js +1 -1
- package/dist/es2019/presets/all.codegen.js +3 -2
- package/dist/es2019/presets/recommended.codegen.js +1 -1
- package/dist/es2019/rules/index.codegen.js +5 -3
- package/dist/es2019/rules/no-html-anchor/index.js +33 -0
- package/dist/es2019/rules/no-html-anchor/node-types/index.js +2 -0
- package/dist/es2019/rules/no-html-anchor/node-types/jsx-element/index.js +20 -0
- package/dist/es2019/rules/no-html-anchor/node-types/styled-component/get-styled-component-call.js +42 -0
- package/dist/es2019/rules/no-html-anchor/node-types/styled-component/index.js +32 -0
- package/dist/es2019/rules/no-html-anchor/node-types/supported.js +56 -0
- package/dist/es2019/rules/no-html-anchor/utils/get-jsx-element-by-name.js +39 -0
- package/dist/es2019/rules/no-html-button/index.js +33 -0
- package/dist/es2019/rules/no-html-button/node-types/index.js +2 -0
- package/dist/es2019/rules/no-html-button/node-types/jsx-element/index.js +20 -0
- package/dist/es2019/rules/no-html-button/node-types/styled-component/get-styled-component-call.js +42 -0
- package/dist/es2019/rules/no-html-button/node-types/styled-component/index.js +32 -0
- package/dist/es2019/rules/no-html-button/node-types/supported.js +69 -0
- package/dist/es2019/rules/no-html-button/utils/get-jsx-element-by-name.js +39 -0
- package/dist/esm/common/token-maps.partial.js +42 -0
- package/dist/esm/index.codegen.js +1 -1
- package/dist/esm/presets/all.codegen.js +3 -2
- package/dist/esm/presets/recommended.codegen.js +1 -1
- package/dist/esm/rules/index.codegen.js +5 -3
- package/dist/esm/rules/no-html-anchor/index.js +33 -0
- package/dist/esm/rules/no-html-anchor/node-types/index.js +2 -0
- package/dist/esm/rules/no-html-anchor/node-types/jsx-element/index.js +19 -0
- package/dist/esm/rules/no-html-anchor/node-types/styled-component/get-styled-component-call.js +42 -0
- package/dist/esm/rules/no-html-anchor/node-types/styled-component/index.js +31 -0
- package/dist/esm/rules/no-html-anchor/node-types/supported.js +57 -0
- package/dist/esm/rules/no-html-anchor/utils/get-jsx-element-by-name.js +47 -0
- package/dist/esm/rules/no-html-button/index.js +33 -0
- package/dist/esm/rules/no-html-button/node-types/index.js +2 -0
- package/dist/esm/rules/no-html-button/node-types/jsx-element/index.js +19 -0
- package/dist/esm/rules/no-html-button/node-types/styled-component/get-styled-component-call.js +42 -0
- package/dist/esm/rules/no-html-button/node-types/styled-component/index.js +31 -0
- package/dist/esm/rules/no-html-button/node-types/supported.js +70 -0
- package/dist/esm/rules/no-html-button/utils/get-jsx-element-by-name.js +47 -0
- package/dist/types/common/token-maps.partial.d.ts +65 -0
- package/dist/types/index.codegen.d.ts +2 -1
- package/dist/types/presets/all.codegen.d.ts +3 -2
- package/dist/types/presets/recommended.codegen.d.ts +1 -1
- package/dist/types/rules/index.codegen.d.ts +2 -1
- package/dist/types/rules/no-html-anchor/index.d.ts +3 -0
- package/dist/types/rules/no-html-anchor/node-types/index.d.ts +2 -0
- package/dist/types/rules/no-html-anchor/node-types/jsx-element/index.d.ts +8 -0
- package/dist/types/rules/no-html-anchor/node-types/styled-component/get-styled-component-call.d.ts +6 -0
- package/dist/types/rules/no-html-anchor/node-types/styled-component/index.d.ts +8 -0
- package/dist/types/rules/no-html-anchor/node-types/supported.d.ts +7 -0
- package/dist/types/rules/no-html-anchor/utils/get-jsx-element-by-name.d.ts +6 -0
- package/dist/types/rules/no-html-button/index.d.ts +3 -0
- package/dist/types/rules/no-html-button/node-types/index.d.ts +2 -0
- package/dist/types/rules/no-html-button/node-types/jsx-element/index.d.ts +8 -0
- package/dist/types/rules/no-html-button/node-types/styled-component/get-styled-component-call.d.ts +6 -0
- package/dist/types/rules/no-html-button/node-types/styled-component/index.d.ts +8 -0
- package/dist/types/rules/no-html-button/node-types/supported.d.ts +7 -0
- package/dist/types/rules/no-html-button/utils/get-jsx-element-by-name.d.ts +6 -0
- package/dist/types-ts4.5/common/token-maps.partial.d.ts +65 -0
- package/dist/types-ts4.5/index.codegen.d.ts +2 -1
- package/dist/types-ts4.5/presets/all.codegen.d.ts +3 -2
- package/dist/types-ts4.5/presets/recommended.codegen.d.ts +1 -1
- package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -1
- package/dist/types-ts4.5/rules/no-html-anchor/node-types/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-html-anchor/node-types/jsx-element/index.d.ts +8 -0
- package/dist/types-ts4.5/rules/no-html-anchor/node-types/styled-component/get-styled-component-call.d.ts +6 -0
- package/dist/types-ts4.5/rules/no-html-anchor/node-types/styled-component/index.d.ts +8 -0
- package/dist/types-ts4.5/rules/no-html-anchor/node-types/supported.d.ts +7 -0
- package/dist/types-ts4.5/rules/no-html-anchor/utils/get-jsx-element-by-name.d.ts +6 -0
- package/dist/types-ts4.5/rules/no-html-button/node-types/index.d.ts +2 -0
- package/dist/types-ts4.5/rules/no-html-button/node-types/jsx-element/index.d.ts +8 -0
- package/dist/types-ts4.5/rules/no-html-button/node-types/styled-component/get-styled-component-call.d.ts +6 -0
- package/dist/types-ts4.5/rules/no-html-button/node-types/styled-component/index.d.ts +8 -0
- package/dist/types-ts4.5/rules/no-html-button/node-types/supported.d.ts +7 -0
- package/dist/types-ts4.5/rules/no-html-button/utils/get-jsx-element-by-name.d.ts +6 -0
- package/package.json +3 -2
- package/constellation/no-html-button-element/usage.mdx +0 -26
- package/dist/cjs/rules/no-html-button-element/index.js +0 -107
- package/dist/cjs/rules/no-html-button-element/utils.js +0 -18
- package/dist/es2019/rules/no-html-button-element/index.js +0 -101
- package/dist/es2019/rules/no-html-button-element/utils.js +0 -12
- package/dist/esm/rules/no-html-button-element/index.js +0 -101
- package/dist/esm/rules/no-html-button-element/utils.js +0 -12
- package/dist/types/rules/no-html-button-element/utils.d.ts +0 -2
- package/dist/types-ts4.5/rules/no-html-button-element/utils.d.ts +0 -2
- /package/dist/types-ts4.5/rules/{no-html-button-element → no-html-anchor}/index.d.ts +0 -0
- /package/dist/{types/rules/no-html-button-element → types-ts4.5/rules/no-html-button}/index.d.ts +0 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.getJsxElementByName = void 0;
|
|
8
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
9
|
+
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
10
|
+
/**
|
|
11
|
+
* Given a component name finds its JSX usage
|
|
12
|
+
*/
|
|
13
|
+
var getJsxElementByName = exports.getJsxElementByName = function getJsxElementByName(componentName, scope) {
|
|
14
|
+
var _variableDeclaration$;
|
|
15
|
+
var variableDeclaration = scope.variables.find(function (v) {
|
|
16
|
+
return v.name === componentName;
|
|
17
|
+
});
|
|
18
|
+
if (!variableDeclaration) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// length here should be exactly 2 to indicate only two references:
|
|
23
|
+
// one being the variable declaration itself
|
|
24
|
+
// second being the JSX call site
|
|
25
|
+
// we might consider handling multiple local JSX call sites in the future
|
|
26
|
+
// but "this is good enough for now"™️
|
|
27
|
+
if (variableDeclaration.references.length !== 2) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
var jsxUsage = (_variableDeclaration$ = variableDeclaration.references[1]) === null || _variableDeclaration$ === void 0 ? void 0 : _variableDeclaration$.identifier;
|
|
31
|
+
var _variableDeclaration$2 = variableDeclaration.references.map(function (ref) {
|
|
32
|
+
return ref === null || ref === void 0 ? void 0 : ref.identifier;
|
|
33
|
+
}),
|
|
34
|
+
_variableDeclaration$3 = (0, _slicedToArray2.default)(_variableDeclaration$2, 2),
|
|
35
|
+
firstIdentifier = _variableDeclaration$3[0],
|
|
36
|
+
secondIdentifier = _variableDeclaration$3[1];
|
|
37
|
+
// Check if the first reference is a JSXOpeningElement and the second is not or vice versa
|
|
38
|
+
if ((0, _eslintCodemodUtils.isNodeOfType)(firstIdentifier, 'JSXIdentifier') && !(0, _eslintCodemodUtils.isNodeOfType)(secondIdentifier, 'JSXIdentifier')) {
|
|
39
|
+
jsxUsage = firstIdentifier;
|
|
40
|
+
} else if ((0, _eslintCodemodUtils.isNodeOfType)(secondIdentifier, 'JSXIdentifier') && !(0, _eslintCodemodUtils.isNodeOfType)(firstIdentifier, 'JSXIdentifier')) {
|
|
41
|
+
jsxUsage = secondIdentifier;
|
|
42
|
+
} else {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(jsxUsage, 'JSXIdentifier')) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
var jsxOpeningElement = jsxUsage.parent;
|
|
49
|
+
if (!(0, _eslintCodemodUtils.isNodeOfType)(jsxOpeningElement, 'JSXOpeningElement')) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
return jsxOpeningElement;
|
|
53
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THIS SECTION WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
|
|
3
|
+
* @codegen <<SignedSource::30c84f908843af1f250860b6f3deeea0>>
|
|
4
|
+
* @codegenId spacing
|
|
5
|
+
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen-token-maps
|
|
6
|
+
* @codegenDependency ../../../tokens/src/artifacts/tokens-raw/atlassian-spacing.tsx <<SignedSource::55622b91aca9b3afac4bce440f222b71>>
|
|
7
|
+
*/
|
|
8
|
+
export const positiveSpaceMap = {
|
|
9
|
+
'0px': 'space.0',
|
|
10
|
+
'2px': 'space.025',
|
|
11
|
+
'4px': 'space.050',
|
|
12
|
+
'6px': 'space.075',
|
|
13
|
+
'8px': 'space.100',
|
|
14
|
+
'12px': 'space.150',
|
|
15
|
+
'16px': 'space.200',
|
|
16
|
+
'20px': 'space.250',
|
|
17
|
+
'24px': 'space.300',
|
|
18
|
+
'32px': 'space.400',
|
|
19
|
+
'40px': 'space.500',
|
|
20
|
+
'48px': 'space.600',
|
|
21
|
+
'64px': 'space.800',
|
|
22
|
+
'80px': 'space.1000'
|
|
23
|
+
};
|
|
24
|
+
export const negativeSpaceMap = {
|
|
25
|
+
'-2px': 'space.negative.025',
|
|
26
|
+
'-4px': 'space.negative.050',
|
|
27
|
+
'-6px': 'space.negative.075',
|
|
28
|
+
'-8px': 'space.negative.100',
|
|
29
|
+
'-12px': 'space.negative.150',
|
|
30
|
+
'-16px': 'space.negative.200',
|
|
31
|
+
'-20px': 'space.negative.250',
|
|
32
|
+
'-24px': 'space.negative.300',
|
|
33
|
+
'-32px': 'space.negative.400'
|
|
34
|
+
};
|
|
35
|
+
export const allSpaceMap = {
|
|
36
|
+
...positiveSpaceMap,
|
|
37
|
+
...negativeSpaceMap
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @codegenEnd
|
|
42
|
+
*/
|
|
@@ -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::
|
|
3
|
+
* @codegen <<SignedSource::a1bb0f17560603fa4895326b1d50744e>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
|
|
5
5
|
*/
|
|
6
6
|
import all from './presets/all.codegen';
|
|
@@ -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::
|
|
3
|
+
* @codegen <<SignedSource::0be7639a95bf4c9bbfe00154d8193477>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -19,7 +19,8 @@ export default {
|
|
|
19
19
|
'@atlaskit/design-system/no-empty-styled-expression': 'warn',
|
|
20
20
|
'@atlaskit/design-system/no-exported-css': 'warn',
|
|
21
21
|
'@atlaskit/design-system/no-exported-keyframes': 'warn',
|
|
22
|
-
'@atlaskit/design-system/no-html-
|
|
22
|
+
'@atlaskit/design-system/no-html-anchor': 'warn',
|
|
23
|
+
'@atlaskit/design-system/no-html-button': 'warn',
|
|
23
24
|
'@atlaskit/design-system/no-invalid-css-map': 'error',
|
|
24
25
|
'@atlaskit/design-system/no-keyframes-tagged-template-expression': 'error',
|
|
25
26
|
'@atlaskit/design-system/no-margin': '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::
|
|
3
|
+
* @codegen <<SignedSource::73a3666b244b58cc221eebcda071161d>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
|
|
5
5
|
*/
|
|
6
6
|
export default {
|
|
@@ -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::
|
|
3
|
+
* @codegen <<SignedSource::9b934590b0ff7880e962fa4dd73bdb0b>>
|
|
4
4
|
* @codegenCommand yarn workspace @atlaskit/eslint-plugin-design-system codegen
|
|
5
5
|
*/
|
|
6
6
|
import consistentCssPropUsage from './consistent-css-prop-usage';
|
|
@@ -16,7 +16,8 @@ import noDirectUseOfWebPlatformDragAndDrop from './no-direct-use-of-web-platform
|
|
|
16
16
|
import noEmptyStyledExpression from './no-empty-styled-expression';
|
|
17
17
|
import noExportedCss from './no-exported-css';
|
|
18
18
|
import noExportedKeyframes from './no-exported-keyframes';
|
|
19
|
-
import
|
|
19
|
+
import noHtmlAnchor from './no-html-anchor';
|
|
20
|
+
import noHtmlButton from './no-html-button';
|
|
20
21
|
import noInvalidCssMap from './no-invalid-css-map';
|
|
21
22
|
import noKeyframesTaggedTemplateExpression from './no-keyframes-tagged-template-expression';
|
|
22
23
|
import noMargin from './no-margin';
|
|
@@ -52,7 +53,8 @@ export default {
|
|
|
52
53
|
'no-empty-styled-expression': noEmptyStyledExpression,
|
|
53
54
|
'no-exported-css': noExportedCss,
|
|
54
55
|
'no-exported-keyframes': noExportedKeyframes,
|
|
55
|
-
'no-html-
|
|
56
|
+
'no-html-anchor': noHtmlAnchor,
|
|
57
|
+
'no-html-button': noHtmlButton,
|
|
56
58
|
'no-invalid-css-map': noInvalidCssMap,
|
|
57
59
|
'no-keyframes-tagged-template-expression': noKeyframesTaggedTemplateExpression,
|
|
58
60
|
'no-margin': noMargin,
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createLintRule } from '../utils/create-rule';
|
|
2
|
+
import { JSXElement, StyledComponent } from './node-types';
|
|
3
|
+
const rule = createLintRule({
|
|
4
|
+
meta: {
|
|
5
|
+
name: 'no-html-anchor',
|
|
6
|
+
type: 'suggestion',
|
|
7
|
+
docs: {
|
|
8
|
+
description: 'Discourage direct usage of HTML anchor elements in favor of Atlassian Design System link components.',
|
|
9
|
+
recommended: false,
|
|
10
|
+
severity: 'warn'
|
|
11
|
+
},
|
|
12
|
+
messages: {
|
|
13
|
+
noHtmlAnchor: `This <{{ name }}> should be replaced with a link component from the Atlassian Design System, such as the "Link" or "LinkButton" component when suitable. For custom links use the "Anchor" primitive. ADS links include event tracking, automatic router configuration, ensure accessible implementations, and provide access to ADS styling features like design tokens.`
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
return {
|
|
18
|
+
// transforms styled.<anchor>(...) usages
|
|
19
|
+
CallExpression(node) {
|
|
20
|
+
StyledComponent.lint(node, {
|
|
21
|
+
context
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
// transforms <anchor css={...}> usages
|
|
25
|
+
JSXElement(node) {
|
|
26
|
+
JSXElement.lint(node, {
|
|
27
|
+
context
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
export default rule;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* eslint-disable @repo/internal/react/require-jsdoc */
|
|
2
|
+
|
|
3
|
+
import * as ast from '../../../../ast-nodes';
|
|
4
|
+
import { isSupportedForLint } from '../supported';
|
|
5
|
+
export const JSXElement = {
|
|
6
|
+
lint(node, {
|
|
7
|
+
context
|
|
8
|
+
}) {
|
|
9
|
+
if (!isSupportedForLint(node)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
context.report({
|
|
13
|
+
node: node.openingElement,
|
|
14
|
+
messageId: 'noHtmlAnchor',
|
|
15
|
+
data: {
|
|
16
|
+
name: ast.JSXElement.getName(node)
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
};
|
package/dist/es2019/rules/no-html-anchor/node-types/styled-component/get-styled-component-call.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { closestOfType, isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a styled component
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const getStyledComponentCall = node => {
|
|
8
|
+
// halts unless we are dealing with a styled component
|
|
9
|
+
if (!isStyledCallExpression(node)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
// halts if the component is being exported directly
|
|
13
|
+
if (closestOfType(node, 'ExportNamedDeclaration')) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const styledComponentVariableRef = node.parent;
|
|
17
|
+
// halts if the styled component is not assigned to a variable immediately
|
|
18
|
+
if (!isNodeOfType(styledComponentVariableRef, 'VariableDeclarator')) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
return styledComponentVariableRef;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Some verbose precondition checks but all it does is check
|
|
26
|
+
* a call expression is of form `styled.a` or `styled2.a`
|
|
27
|
+
*/
|
|
28
|
+
const isStyledCallExpression = call => {
|
|
29
|
+
if (!isNodeOfType(call, 'CallExpression')) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (!isNodeOfType(call.callee, 'MemberExpression')) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (!isNodeOfType(call.callee.object, 'Identifier') || !isNodeOfType(call.callee.property, 'Identifier')) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if (/^styled2?$/.test(call.callee.object.name)) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* eslint-disable @repo/internal/react/require-jsdoc */
|
|
2
|
+
|
|
3
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
4
|
+
import { getJsxElementByName } from '../../utils/get-jsx-element-by-name';
|
|
5
|
+
import { isSupportedForLint } from '../supported';
|
|
6
|
+
import { getStyledComponentCall } from './get-styled-component-call';
|
|
7
|
+
export const StyledComponent = {
|
|
8
|
+
lint(node, {
|
|
9
|
+
context
|
|
10
|
+
}) {
|
|
11
|
+
var _getJsxElementByName;
|
|
12
|
+
if (!isNodeOfType(node, 'CallExpression') || !isNodeOfType(node.callee, 'MemberExpression') || !isNodeOfType(node.callee.object, 'Identifier') || !isNodeOfType(node.callee.property, 'Identifier')) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const styles = getStyledComponentCall(node);
|
|
16
|
+
const elementName = node.callee.property.name;
|
|
17
|
+
if (!styles || !isNodeOfType(styles.id, 'Identifier')) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const jsxElement = (_getJsxElementByName = getJsxElementByName(styles.id.name, context.getScope())) === null || _getJsxElementByName === void 0 ? void 0 : _getJsxElementByName.parent;
|
|
21
|
+
if (jsxElement && !isSupportedForLint(jsxElement, elementName)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
context.report({
|
|
25
|
+
node: styles,
|
|
26
|
+
messageId: 'noHtmlAnchor',
|
|
27
|
+
data: {
|
|
28
|
+
name: node.callee.property.name
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
import * as ast from '../../../ast-nodes';
|
|
3
|
+
const supportedElements = [{
|
|
4
|
+
name: 'a'
|
|
5
|
+
}];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Determines if the given JSX element is a supported element to lint with this rule.
|
|
9
|
+
*/
|
|
10
|
+
export function isSupportedForLint(jsxNode, elementName) {
|
|
11
|
+
if (!isNodeOfType(jsxNode, 'JSXElement')) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Allow passing in the element name because the jsxNode doesn't
|
|
16
|
+
// represent the element name with styled components
|
|
17
|
+
const elName = elementName || ast.JSXElement.getName(jsxNode);
|
|
18
|
+
if (!elName) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Only check native HTML elements, not components
|
|
23
|
+
if (elName[0] !== elName[0].toLowerCase()) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
let supportedElement = supportedElements.find(({
|
|
27
|
+
name
|
|
28
|
+
}) => name === elName);
|
|
29
|
+
if (!supportedElement) {
|
|
30
|
+
supportedElement = supportedElements.find(({
|
|
31
|
+
name
|
|
32
|
+
}) => name === '*');
|
|
33
|
+
}
|
|
34
|
+
if (!supportedElement) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Check if the element has any attributes that are not supported
|
|
39
|
+
const attributes = ast.JSXElement.getAttributes(jsxNode);
|
|
40
|
+
if (supportedElement.attributes && !supportedElement.attributes.every(({
|
|
41
|
+
name,
|
|
42
|
+
values
|
|
43
|
+
}) => {
|
|
44
|
+
return attributes.some(attribute => {
|
|
45
|
+
if (attribute.type === 'JSXSpreadAttribute') {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const isMatchingName = attribute.name.name === name;
|
|
49
|
+
const isMatchingValues = values && attribute.value && attribute.value.type === 'Literal' && typeof attribute.value.value === 'string' && (values === null || values === void 0 ? void 0 : values.includes(attribute.value.value));
|
|
50
|
+
return isMatchingName && isMatchingValues;
|
|
51
|
+
});
|
|
52
|
+
})) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Given a component name finds its JSX usage
|
|
5
|
+
*/
|
|
6
|
+
export const getJsxElementByName = (componentName, scope) => {
|
|
7
|
+
var _variableDeclaration$;
|
|
8
|
+
const variableDeclaration = scope.variables.find(v => v.name === componentName);
|
|
9
|
+
if (!variableDeclaration) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// length here should be exactly 2 to indicate only two references:
|
|
14
|
+
// one being the variable declaration itself
|
|
15
|
+
// second being the JSX call site
|
|
16
|
+
// we might consider handling multiple local JSX call sites in the future
|
|
17
|
+
// but "this is good enough for now"™️
|
|
18
|
+
if (variableDeclaration.references.length !== 2) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
let jsxUsage = (_variableDeclaration$ = variableDeclaration.references[1]) === null || _variableDeclaration$ === void 0 ? void 0 : _variableDeclaration$.identifier;
|
|
22
|
+
const [firstIdentifier, secondIdentifier] = variableDeclaration.references.map(ref => ref === null || ref === void 0 ? void 0 : ref.identifier);
|
|
23
|
+
// Check if the first reference is a JSXOpeningElement and the second is not or vice versa
|
|
24
|
+
if (isNodeOfType(firstIdentifier, 'JSXIdentifier') && !isNodeOfType(secondIdentifier, 'JSXIdentifier')) {
|
|
25
|
+
jsxUsage = firstIdentifier;
|
|
26
|
+
} else if (isNodeOfType(secondIdentifier, 'JSXIdentifier') && !isNodeOfType(firstIdentifier, 'JSXIdentifier')) {
|
|
27
|
+
jsxUsage = secondIdentifier;
|
|
28
|
+
} else {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (!isNodeOfType(jsxUsage, 'JSXIdentifier')) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const jsxOpeningElement = jsxUsage.parent;
|
|
35
|
+
if (!isNodeOfType(jsxOpeningElement, 'JSXOpeningElement')) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
return jsxOpeningElement;
|
|
39
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createLintRule } from '../utils/create-rule';
|
|
2
|
+
import { JSXElement, StyledComponent } from './node-types';
|
|
3
|
+
const rule = createLintRule({
|
|
4
|
+
meta: {
|
|
5
|
+
name: 'no-html-button',
|
|
6
|
+
type: 'suggestion',
|
|
7
|
+
docs: {
|
|
8
|
+
description: 'Discourage direct usage of HTML button elements in favor of Atlassian Design System button components.',
|
|
9
|
+
recommended: false,
|
|
10
|
+
severity: 'warn'
|
|
11
|
+
},
|
|
12
|
+
messages: {
|
|
13
|
+
noHtmlButton: `This {{ name }} should be replaced with a button component from the Atlassian Design System, such as the "Button" component when suitable. For custom buttons use the "Pressable" primitive. ADS buttons include event tracking, ensure accessible implementations, and provide access to ADS styling features like design tokens.`
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
return {
|
|
18
|
+
// transforms styled.<button>(...) usages
|
|
19
|
+
CallExpression(node) {
|
|
20
|
+
StyledComponent.lint(node, {
|
|
21
|
+
context
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
// transforms <button css={...}> usages
|
|
25
|
+
JSXElement(node) {
|
|
26
|
+
JSXElement.lint(node, {
|
|
27
|
+
context
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
export default rule;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* eslint-disable @repo/internal/react/require-jsdoc */
|
|
2
|
+
|
|
3
|
+
import * as ast from '../../../../ast-nodes';
|
|
4
|
+
import { isSupportedForLint } from '../supported';
|
|
5
|
+
export const JSXElement = {
|
|
6
|
+
lint(node, {
|
|
7
|
+
context
|
|
8
|
+
}) {
|
|
9
|
+
if (!isSupportedForLint(node)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
context.report({
|
|
13
|
+
node: node.openingElement,
|
|
14
|
+
messageId: 'noHtmlButton',
|
|
15
|
+
data: {
|
|
16
|
+
name: ast.JSXElement.getName(node)
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
};
|
package/dist/es2019/rules/no-html-button/node-types/styled-component/get-styled-component-call.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { closestOfType, isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a styled component
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export const getStyledComponentCall = node => {
|
|
8
|
+
// halts unless we are dealing with a styled component
|
|
9
|
+
if (!isStyledCallExpression(node)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
// halts if the component is being exported directly
|
|
13
|
+
if (closestOfType(node, 'ExportNamedDeclaration')) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const styledComponentVariableRef = node.parent;
|
|
17
|
+
// halts if the styled component is not assigned to a variable immediately
|
|
18
|
+
if (!isNodeOfType(styledComponentVariableRef, 'VariableDeclarator')) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
return styledComponentVariableRef;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Some verbose precondition checks but all it does is check
|
|
26
|
+
* a call expression is of form `styled.button` or `styled2.button`
|
|
27
|
+
*/
|
|
28
|
+
const isStyledCallExpression = call => {
|
|
29
|
+
if (!isNodeOfType(call, 'CallExpression')) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (!isNodeOfType(call.callee, 'MemberExpression')) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (!isNodeOfType(call.callee.object, 'Identifier') || !isNodeOfType(call.callee.property, 'Identifier')) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if (/^styled2?$/.test(call.callee.object.name)) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* eslint-disable @repo/internal/react/require-jsdoc */
|
|
2
|
+
|
|
3
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
4
|
+
import { getJsxElementByName } from '../../utils/get-jsx-element-by-name';
|
|
5
|
+
import { isSupportedForLint } from '../supported';
|
|
6
|
+
import { getStyledComponentCall } from './get-styled-component-call';
|
|
7
|
+
export const StyledComponent = {
|
|
8
|
+
lint(node, {
|
|
9
|
+
context
|
|
10
|
+
}) {
|
|
11
|
+
var _getJsxElementByName;
|
|
12
|
+
if (!isNodeOfType(node, 'CallExpression') || !isNodeOfType(node.callee, 'MemberExpression') || !isNodeOfType(node.callee.object, 'Identifier') || !isNodeOfType(node.callee.property, 'Identifier')) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const styles = getStyledComponentCall(node);
|
|
16
|
+
const elementName = node.callee.property.name;
|
|
17
|
+
if (!styles || !isNodeOfType(styles.id, 'Identifier')) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const jsxElement = (_getJsxElementByName = getJsxElementByName(styles.id.name, context.getScope())) === null || _getJsxElementByName === void 0 ? void 0 : _getJsxElementByName.parent;
|
|
21
|
+
if (jsxElement && !isSupportedForLint(jsxElement, elementName)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
context.report({
|
|
25
|
+
node: styles,
|
|
26
|
+
messageId: 'noHtmlButton',
|
|
27
|
+
data: {
|
|
28
|
+
name: node.callee.property.name
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
import * as ast from '../../../ast-nodes';
|
|
3
|
+
const supportedElements = [{
|
|
4
|
+
name: 'button'
|
|
5
|
+
}, {
|
|
6
|
+
name: 'input',
|
|
7
|
+
attributes: [{
|
|
8
|
+
name: 'type',
|
|
9
|
+
// Values from https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/button_role#best_practices
|
|
10
|
+
values: ['submit', 'reset', 'button', 'image']
|
|
11
|
+
}]
|
|
12
|
+
}, {
|
|
13
|
+
name: '*',
|
|
14
|
+
attributes: [{
|
|
15
|
+
name: 'role',
|
|
16
|
+
values: ['button']
|
|
17
|
+
}]
|
|
18
|
+
}];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Determines if the given JSX element is a supported element to lint with this rule.
|
|
22
|
+
*/
|
|
23
|
+
export function isSupportedForLint(jsxNode, elementName) {
|
|
24
|
+
if (!isNodeOfType(jsxNode, 'JSXElement')) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Allow passing in the element name because the jsxNode doesn't
|
|
29
|
+
// represent the element name with styled components
|
|
30
|
+
const elName = elementName || ast.JSXElement.getName(jsxNode);
|
|
31
|
+
if (!elName) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Only check native HTML elements, not components
|
|
36
|
+
if (elName[0] !== elName[0].toLowerCase()) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
let supportedElement = supportedElements.find(({
|
|
40
|
+
name
|
|
41
|
+
}) => name === elName);
|
|
42
|
+
if (!supportedElement) {
|
|
43
|
+
supportedElement = supportedElements.find(({
|
|
44
|
+
name
|
|
45
|
+
}) => name === '*');
|
|
46
|
+
}
|
|
47
|
+
if (!supportedElement) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Check if the element has any attributes that are not supported
|
|
52
|
+
const attributes = ast.JSXElement.getAttributes(jsxNode);
|
|
53
|
+
if (supportedElement.attributes && !supportedElement.attributes.every(({
|
|
54
|
+
name,
|
|
55
|
+
values
|
|
56
|
+
}) => {
|
|
57
|
+
return attributes.some(attribute => {
|
|
58
|
+
if (attribute.type === 'JSXSpreadAttribute') {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
const isMatchingName = attribute.name.name === name;
|
|
62
|
+
const isMatchingValues = values && attribute.value && attribute.value.type === 'Literal' && typeof attribute.value.value === 'string' && (values === null || values === void 0 ? void 0 : values.includes(attribute.value.value));
|
|
63
|
+
return isMatchingName && isMatchingValues;
|
|
64
|
+
});
|
|
65
|
+
})) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Given a component name finds its JSX usage
|
|
5
|
+
*/
|
|
6
|
+
export const getJsxElementByName = (componentName, scope) => {
|
|
7
|
+
var _variableDeclaration$;
|
|
8
|
+
const variableDeclaration = scope.variables.find(v => v.name === componentName);
|
|
9
|
+
if (!variableDeclaration) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// length here should be exactly 2 to indicate only two references:
|
|
14
|
+
// one being the variable declaration itself
|
|
15
|
+
// second being the JSX call site
|
|
16
|
+
// we might consider handling multiple local JSX call sites in the future
|
|
17
|
+
// but "this is good enough for now"™️
|
|
18
|
+
if (variableDeclaration.references.length !== 2) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
let jsxUsage = (_variableDeclaration$ = variableDeclaration.references[1]) === null || _variableDeclaration$ === void 0 ? void 0 : _variableDeclaration$.identifier;
|
|
22
|
+
const [firstIdentifier, secondIdentifier] = variableDeclaration.references.map(ref => ref === null || ref === void 0 ? void 0 : ref.identifier);
|
|
23
|
+
// Check if the first reference is a JSXOpeningElement and the second is not or vice versa
|
|
24
|
+
if (isNodeOfType(firstIdentifier, 'JSXIdentifier') && !isNodeOfType(secondIdentifier, 'JSXIdentifier')) {
|
|
25
|
+
jsxUsage = firstIdentifier;
|
|
26
|
+
} else if (isNodeOfType(secondIdentifier, 'JSXIdentifier') && !isNodeOfType(firstIdentifier, 'JSXIdentifier')) {
|
|
27
|
+
jsxUsage = secondIdentifier;
|
|
28
|
+
} else {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (!isNodeOfType(jsxUsage, 'JSXIdentifier')) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const jsxOpeningElement = jsxUsage.parent;
|
|
35
|
+
if (!isNodeOfType(jsxOpeningElement, 'JSXOpeningElement')) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
return jsxOpeningElement;
|
|
39
|
+
};
|