@atlaskit/eslint-plugin-platform 0.13.1 → 0.14.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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # @atlaskit/eslint-plugin-platform
2
2
 
3
+ ## 0.14.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#173404](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/173404)
8
+ [`7818ef3312ccd`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/7818ef3312ccd) -
9
+ Eslint rule for expand-background-shorthand with token
10
+
3
11
  ## 0.13.1
4
12
 
5
13
  ### Patch Changes
@@ -16,5 +16,9 @@
16
16
  "../src/**/*.test.*",
17
17
  "../src/**/test.*"
18
18
  ],
19
- "references": []
19
+ "references": [
20
+ {
21
+ "path": "../../../design-system/eslint-utils/afm-cc/tsconfig.json"
22
+ }
23
+ ]
20
24
  }
package/dist/cjs/index.js CHANGED
@@ -30,6 +30,7 @@ var _preferFg = _interopRequireDefault(require("./rules/feature-gating/prefer-fg
30
30
  var _noAlias = _interopRequireDefault(require("./rules/feature-gating/no-alias"));
31
31
  var _useEntrypointsInExamples = _interopRequireDefault(require("./rules/use-entrypoints-in-examples"));
32
32
  var _useRecommendedUtils = _interopRequireDefault(require("./rules/feature-gating/use-recommended-utils"));
33
+ var _expandBackgroundShorthand = _interopRequireDefault(require("./rules/compiled/expand-background-shorthand"));
33
34
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
34
35
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } // eslint-disable-next-line import/no-extraneous-dependencies
35
36
  var rules = exports.rules = {
@@ -42,6 +43,7 @@ var rules = exports.rules = {
42
43
  'ensure-valid-platform-yarn-protocol-usage': _ensureValidPlatformYarnProtocolUsage.default,
43
44
  'ensure-valid-bin-values': _ensureValidBinValues.default,
44
45
  'expand-border-shorthand': _expandBorderShorthand.default,
46
+ 'expand-background-shorthand': _expandBackgroundShorthand.default,
45
47
  'no-duplicate-dependencies': _noDuplicateDependencies.default,
46
48
  'no-invalid-feature-flag-usage': _noInvalidFeatureFlagUsage.default,
47
49
  'no-pre-post-install-scripts': _noPrePostInstalls.default,
@@ -67,6 +69,7 @@ var commonConfig = {
67
69
  '@atlaskit/platform/no-module-level-eval-nav4': 'error',
68
70
  // Compiled: rules that are not included via `@compiled/recommended
69
71
  '@atlaskit/platform/expand-border-shorthand': 'warn',
72
+ '@atlaskit/platform/expand-background-shorthand': 'warn',
70
73
  '@compiled/jsx-pragma': ['error', {
71
74
  importSources: ['@atlaskit/css'],
72
75
  onlyRunIfImportingCompiled: true,
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.expandBackgroundShorthand = exports.default = void 0;
7
+ var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
8
+ // Checks if the function that holds the border property is using an import package that this rule is targeting
9
+ var isCompiledAPI = function isCompiledAPI(context) {
10
+ var importSources = (0, _isSupportedImport.getImportSources)(context);
11
+ var _context$getScope = context.getScope(),
12
+ references = _context$getScope.references;
13
+ var ancestors = context.getAncestors();
14
+ if (ancestors.some(function (ancestor) {
15
+ return ancestor.type === 'CallExpression' && ancestor.callee && ((0, _isSupportedImport.isCompiled)(ancestor.callee, references, importSources) || (0, _isSupportedImport.isAtlasKitCSS)(ancestor.callee, references, importSources));
16
+ })) {
17
+ return true;
18
+ }
19
+ return false;
20
+ };
21
+
22
+ // Checks if node is a call expression with identifier 'token'
23
+ var isTokenCallExpression = function isTokenCallExpression(node) {
24
+ if (node.type === 'CallExpression') {
25
+ if (node.callee.type === 'Identifier' && node.callee.name === 'token') {
26
+ return true;
27
+ }
28
+ }
29
+ return false;
30
+ };
31
+ var expandBackgroundShorthand = exports.expandBackgroundShorthand = {
32
+ meta: {
33
+ docs: {
34
+ url: 'https://bitbucket.org/atlassian/atlassian-frontend-monorepo/src/master/platform/packages/platform/eslint-plugin/src/rules/compiled/expand-background-shorthand/'
35
+ },
36
+ messages: {
37
+ expandBackgroundShorthand: 'Use backgroundColor instead of background shorthand'
38
+ },
39
+ type: 'problem',
40
+ fixable: 'code'
41
+ },
42
+ create: function create(context) {
43
+ return {
44
+ 'Property[key.name="background"]': function PropertyKeyNameBackground(node) {
45
+ if (isCompiledAPI(context) && isTokenCallExpression(node.value)) {
46
+ context.report({
47
+ node: node,
48
+ messageId: 'expandBackgroundShorthand',
49
+ fix: function fix(fixer) {
50
+ return fixer.replaceText(node.key, "backgroundColor");
51
+ }
52
+ });
53
+ return;
54
+ }
55
+ }
56
+ };
57
+ }
58
+ };
59
+ var _default = exports.default = expandBackgroundShorthand;
@@ -24,6 +24,7 @@ import preferFG from './rules/feature-gating/prefer-fg';
24
24
  import noAlias from './rules/feature-gating/no-alias';
25
25
  import useEntrypointsInExamples from './rules/use-entrypoints-in-examples';
26
26
  import useRecommendedUtils from './rules/feature-gating/use-recommended-utils';
27
+ import expandBackgroundShorthand from './rules/compiled/expand-background-shorthand';
27
28
  export const rules = {
28
29
  'ensure-feature-flag-registration': ensureFeatureFlagRegistration,
29
30
  'ensure-feature-flag-prefix': ensureFeatureFlagPrefix,
@@ -34,6 +35,7 @@ export const rules = {
34
35
  'ensure-valid-platform-yarn-protocol-usage': ensureValidPlatformYarnProtocolUsage,
35
36
  'ensure-valid-bin-values': ensureValidBinValues,
36
37
  'expand-border-shorthand': expandBorderShorthand,
38
+ 'expand-background-shorthand': expandBackgroundShorthand,
37
39
  'no-duplicate-dependencies': noDuplicateDependencies,
38
40
  'no-invalid-feature-flag-usage': noInvalidFeatureFlagUsage,
39
41
  'no-pre-post-install-scripts': noPreAndPostInstallScripts,
@@ -59,6 +61,7 @@ const commonConfig = {
59
61
  '@atlaskit/platform/no-module-level-eval-nav4': 'error',
60
62
  // Compiled: rules that are not included via `@compiled/recommended
61
63
  '@atlaskit/platform/expand-border-shorthand': 'warn',
64
+ '@atlaskit/platform/expand-background-shorthand': 'warn',
62
65
  '@compiled/jsx-pragma': ['error', {
63
66
  importSources: ['@atlaskit/css'],
64
67
  onlyRunIfImportingCompiled: true,
@@ -0,0 +1,53 @@
1
+ import { getImportSources, isCompiled, isAtlasKitCSS } from '@atlaskit/eslint-utils/is-supported-import';
2
+
3
+ // Checks if the function that holds the border property is using an import package that this rule is targeting
4
+ const isCompiledAPI = context => {
5
+ const importSources = getImportSources(context);
6
+ const {
7
+ references
8
+ } = context.getScope();
9
+ const ancestors = context.getAncestors();
10
+ if (ancestors.some(ancestor => ancestor.type === 'CallExpression' && ancestor.callee && (isCompiled(ancestor.callee, references, importSources) || isAtlasKitCSS(ancestor.callee, references, importSources)))) {
11
+ return true;
12
+ }
13
+ return false;
14
+ };
15
+
16
+ // Checks if node is a call expression with identifier 'token'
17
+ const isTokenCallExpression = node => {
18
+ if (node.type === 'CallExpression') {
19
+ if (node.callee.type === 'Identifier' && node.callee.name === 'token') {
20
+ return true;
21
+ }
22
+ }
23
+ return false;
24
+ };
25
+ export const expandBackgroundShorthand = {
26
+ meta: {
27
+ docs: {
28
+ url: 'https://bitbucket.org/atlassian/atlassian-frontend-monorepo/src/master/platform/packages/platform/eslint-plugin/src/rules/compiled/expand-background-shorthand/'
29
+ },
30
+ messages: {
31
+ expandBackgroundShorthand: 'Use backgroundColor instead of background shorthand'
32
+ },
33
+ type: 'problem',
34
+ fixable: 'code'
35
+ },
36
+ create(context) {
37
+ return {
38
+ 'Property[key.name="background"]': function (node) {
39
+ if (isCompiledAPI(context) && isTokenCallExpression(node.value)) {
40
+ context.report({
41
+ node,
42
+ messageId: 'expandBackgroundShorthand',
43
+ fix(fixer) {
44
+ return fixer.replaceText(node.key, `backgroundColor`);
45
+ }
46
+ });
47
+ return;
48
+ }
49
+ }
50
+ };
51
+ }
52
+ };
53
+ export default expandBackgroundShorthand;
package/dist/esm/index.js CHANGED
@@ -27,6 +27,7 @@ import preferFG from './rules/feature-gating/prefer-fg';
27
27
  import noAlias from './rules/feature-gating/no-alias';
28
28
  import useEntrypointsInExamples from './rules/use-entrypoints-in-examples';
29
29
  import useRecommendedUtils from './rules/feature-gating/use-recommended-utils';
30
+ import expandBackgroundShorthand from './rules/compiled/expand-background-shorthand';
30
31
  export var rules = {
31
32
  'ensure-feature-flag-registration': ensureFeatureFlagRegistration,
32
33
  'ensure-feature-flag-prefix': ensureFeatureFlagPrefix,
@@ -37,6 +38,7 @@ export var rules = {
37
38
  'ensure-valid-platform-yarn-protocol-usage': ensureValidPlatformYarnProtocolUsage,
38
39
  'ensure-valid-bin-values': ensureValidBinValues,
39
40
  'expand-border-shorthand': expandBorderShorthand,
41
+ 'expand-background-shorthand': expandBackgroundShorthand,
40
42
  'no-duplicate-dependencies': noDuplicateDependencies,
41
43
  'no-invalid-feature-flag-usage': noInvalidFeatureFlagUsage,
42
44
  'no-pre-post-install-scripts': noPreAndPostInstallScripts,
@@ -62,6 +64,7 @@ var commonConfig = {
62
64
  '@atlaskit/platform/no-module-level-eval-nav4': 'error',
63
65
  // Compiled: rules that are not included via `@compiled/recommended
64
66
  '@atlaskit/platform/expand-border-shorthand': 'warn',
67
+ '@atlaskit/platform/expand-background-shorthand': 'warn',
65
68
  '@compiled/jsx-pragma': ['error', {
66
69
  importSources: ['@atlaskit/css'],
67
70
  onlyRunIfImportingCompiled: true,
@@ -0,0 +1,54 @@
1
+ import { getImportSources, isCompiled, isAtlasKitCSS } from '@atlaskit/eslint-utils/is-supported-import';
2
+
3
+ // Checks if the function that holds the border property is using an import package that this rule is targeting
4
+ var isCompiledAPI = function isCompiledAPI(context) {
5
+ var importSources = getImportSources(context);
6
+ var _context$getScope = context.getScope(),
7
+ references = _context$getScope.references;
8
+ var ancestors = context.getAncestors();
9
+ if (ancestors.some(function (ancestor) {
10
+ return ancestor.type === 'CallExpression' && ancestor.callee && (isCompiled(ancestor.callee, references, importSources) || isAtlasKitCSS(ancestor.callee, references, importSources));
11
+ })) {
12
+ return true;
13
+ }
14
+ return false;
15
+ };
16
+
17
+ // Checks if node is a call expression with identifier 'token'
18
+ var isTokenCallExpression = function isTokenCallExpression(node) {
19
+ if (node.type === 'CallExpression') {
20
+ if (node.callee.type === 'Identifier' && node.callee.name === 'token') {
21
+ return true;
22
+ }
23
+ }
24
+ return false;
25
+ };
26
+ export var expandBackgroundShorthand = {
27
+ meta: {
28
+ docs: {
29
+ url: 'https://bitbucket.org/atlassian/atlassian-frontend-monorepo/src/master/platform/packages/platform/eslint-plugin/src/rules/compiled/expand-background-shorthand/'
30
+ },
31
+ messages: {
32
+ expandBackgroundShorthand: 'Use backgroundColor instead of background shorthand'
33
+ },
34
+ type: 'problem',
35
+ fixable: 'code'
36
+ },
37
+ create: function create(context) {
38
+ return {
39
+ 'Property[key.name="background"]': function PropertyKeyNameBackground(node) {
40
+ if (isCompiledAPI(context) && isTokenCallExpression(node.value)) {
41
+ context.report({
42
+ node: node,
43
+ messageId: 'expandBackgroundShorthand',
44
+ fix: function fix(fixer) {
45
+ return fixer.replaceText(node.key, "backgroundColor");
46
+ }
47
+ });
48
+ return;
49
+ }
50
+ }
51
+ };
52
+ }
53
+ };
54
+ export default expandBackgroundShorthand;
@@ -9,6 +9,7 @@ export declare const rules: {
9
9
  'ensure-valid-platform-yarn-protocol-usage': import("eslint").Rule.RuleModule;
10
10
  'ensure-valid-bin-values': import("eslint").Rule.RuleModule;
11
11
  'expand-border-shorthand': import("eslint").Rule.RuleModule;
12
+ 'expand-background-shorthand': import("eslint").Rule.RuleModule;
12
13
  'no-duplicate-dependencies': import("eslint").Rule.RuleModule;
13
14
  'no-invalid-feature-flag-usage': import("eslint").Rule.RuleModule;
14
15
  'no-pre-post-install-scripts': import("eslint").Rule.RuleModule;
@@ -46,6 +47,7 @@ export declare const configs: {
46
47
  '@atlaskit/platform/ensure-atlassian-team': string;
47
48
  '@atlaskit/platform/no-module-level-eval-nav4': string;
48
49
  '@atlaskit/platform/expand-border-shorthand': string;
50
+ '@atlaskit/platform/expand-background-shorthand': string;
49
51
  '@compiled/jsx-pragma': (string | {
50
52
  importSources: string[];
51
53
  onlyRunIfImportingCompiled: boolean;
@@ -63,6 +65,7 @@ export declare const configs: {
63
65
  '@atlaskit/platform/ensure-atlassian-team': string;
64
66
  '@atlaskit/platform/no-module-level-eval-nav4': string;
65
67
  '@atlaskit/platform/expand-border-shorthand': string;
68
+ '@atlaskit/platform/expand-background-shorthand': string;
66
69
  '@compiled/jsx-pragma': (string | {
67
70
  importSources: string[];
68
71
  onlyRunIfImportingCompiled: boolean;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const expandBackgroundShorthand: Rule.RuleModule;
3
+ export default expandBackgroundShorthand;
@@ -9,6 +9,7 @@ export declare const rules: {
9
9
  'ensure-valid-platform-yarn-protocol-usage': import("eslint").Rule.RuleModule;
10
10
  'ensure-valid-bin-values': import("eslint").Rule.RuleModule;
11
11
  'expand-border-shorthand': import("eslint").Rule.RuleModule;
12
+ 'expand-background-shorthand': import("eslint").Rule.RuleModule;
12
13
  'no-duplicate-dependencies': import("eslint").Rule.RuleModule;
13
14
  'no-invalid-feature-flag-usage': import("eslint").Rule.RuleModule;
14
15
  'no-pre-post-install-scripts': import("eslint").Rule.RuleModule;
@@ -46,6 +47,7 @@ export declare const configs: {
46
47
  '@atlaskit/platform/ensure-atlassian-team': string;
47
48
  '@atlaskit/platform/no-module-level-eval-nav4': string;
48
49
  '@atlaskit/platform/expand-border-shorthand': string;
50
+ '@atlaskit/platform/expand-background-shorthand': string;
49
51
  '@compiled/jsx-pragma': (string | {
50
52
  importSources: string[];
51
53
  onlyRunIfImportingCompiled: boolean;
@@ -63,6 +65,7 @@ export declare const configs: {
63
65
  '@atlaskit/platform/ensure-atlassian-team': string;
64
66
  '@atlaskit/platform/no-module-level-eval-nav4': string;
65
67
  '@atlaskit/platform/expand-border-shorthand': string;
68
+ '@atlaskit/platform/expand-background-shorthand': string;
66
69
  '@compiled/jsx-pragma': (string | {
67
70
  importSources: string[];
68
71
  onlyRunIfImportingCompiled: boolean;
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const expandBackgroundShorthand: Rule.RuleModule;
3
+ export default expandBackgroundShorthand;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@atlaskit/eslint-plugin-platform",
3
3
  "description": "The essential plugin for use with Atlassian frontend platform tools",
4
- "version": "0.13.1",
4
+ "version": "0.14.0",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "atlassian": {
7
7
  "team": "Build Infra",
@@ -31,6 +31,7 @@
31
31
  ".": "./src/index.tsx"
32
32
  },
33
33
  "dependencies": {
34
+ "@atlaskit/eslint-utils": "^1.7.0",
34
35
  "@babel/runtime": "^7.0.0",
35
36
  "@manypkg/find-root": "^1.1.0",
36
37
  "fuse.js": "^6.6.2",
package/src/index.tsx CHANGED
@@ -24,6 +24,7 @@ import preferFG from './rules/feature-gating/prefer-fg';
24
24
  import noAlias from './rules/feature-gating/no-alias';
25
25
  import useEntrypointsInExamples from './rules/use-entrypoints-in-examples';
26
26
  import useRecommendedUtils from './rules/feature-gating/use-recommended-utils';
27
+ import expandBackgroundShorthand from './rules/compiled/expand-background-shorthand';
27
28
 
28
29
  export const rules = {
29
30
  'ensure-feature-flag-registration': ensureFeatureFlagRegistration,
@@ -35,6 +36,7 @@ export const rules = {
35
36
  'ensure-valid-platform-yarn-protocol-usage': ensureValidPlatformYarnProtocolUsage,
36
37
  'ensure-valid-bin-values': ensureValidBinValues,
37
38
  'expand-border-shorthand': expandBorderShorthand,
39
+ 'expand-background-shorthand': expandBackgroundShorthand,
38
40
  'no-duplicate-dependencies': noDuplicateDependencies,
39
41
  'no-invalid-feature-flag-usage': noInvalidFeatureFlagUsage,
40
42
  'no-pre-post-install-scripts': noPreAndPostInstallScripts,
@@ -61,6 +63,7 @@ const commonConfig = {
61
63
  '@atlaskit/platform/no-module-level-eval-nav4': 'error',
62
64
  // Compiled: rules that are not included via `@compiled/recommended
63
65
  '@atlaskit/platform/expand-border-shorthand': 'warn',
66
+ '@atlaskit/platform/expand-background-shorthand': 'warn',
64
67
  '@compiled/jsx-pragma': [
65
68
  'error',
66
69
  {
@@ -0,0 +1,23 @@
1
+ # `expand-background-shorthand`
2
+
3
+ This ESLint rule enforces the expansion of the CSS `background` shorthand property into
4
+ its longhand equivalent `backgroundColor`, where the `background`'s value is an Atlassian
5
+ Design System color token and the function call originates from `@compiled/react` or `@atlaskit/css`.
6
+
7
+ ## Rule details
8
+
9
+ 👎 Examples of **incorrect** code for this rule:
10
+
11
+ ```js
12
+ const styles = css({
13
+ background: token('color.background.neutral.hovered'),
14
+ });
15
+ ```
16
+
17
+ 👍 Examples of **correct** code for this rule:
18
+
19
+ ```js
20
+ const styles = css({
21
+ backgroundColor: token('color.background.neutral.hovered'),
22
+ });
23
+ ```
@@ -0,0 +1,160 @@
1
+ import { outdent } from 'outdent';
2
+ import { tester } from '../../../../__tests__/utils/_tester';
3
+ import { expandBackgroundShorthand } from '../index';
4
+
5
+ const included_packages_calls_and_imports = [
6
+ ['css', 'css', '@atlaskit/css'],
7
+ ['css', 'css', '@compiled/react'],
8
+ ['styled', 'styled.div', '@compiled/react'],
9
+ ];
10
+ const exempt_packages_calls_and_imports = [
11
+ ['css', '@atlaskit/primitives'],
12
+ ['css', '@emotion'],
13
+ ['css', 'styled-components'],
14
+ ['xcss', '@atlaskit/primitives'],
15
+ ];
16
+ tester.run('expand-border-shorthand', expandBackgroundShorthand, {
17
+ valid: [
18
+ ...exempt_packages_calls_and_imports.map(([pkg, imp]) => ({
19
+ name: `do not have to handle non-Compiled packages (${pkg}, ${imp})`,
20
+ code: outdent`
21
+ import {${pkg}} from '${imp}';
22
+ const styles = ${pkg}({
23
+ background: token('color.background.accent.gray.subtlest')
24
+ });
25
+ `,
26
+ })),
27
+ {
28
+ name: 'no background shorthand',
29
+ code: outdent`
30
+ import {css} from '@compiled/react';
31
+ const styles = css({
32
+ backgroundColor: token('color.background.neutral.hovered'),
33
+ });
34
+ const styles2 = css({
35
+ backgroundColor: token('color.border', colors.N40),
36
+ });
37
+ const styles3 = css({
38
+ backgroundColor: token('color.background.inverse.subtle', '#00000029'),
39
+ });
40
+ const styles4 = css({
41
+ backgroundColor: token('color.background.brand.bold', 'lightblue'),
42
+ });
43
+ `,
44
+ },
45
+ // Other background shorthands that do not have token is out of scope for now
46
+ {
47
+ name: 'background shorthand without token',
48
+ code: outdent`
49
+ import {css} from '@compiled/react';
50
+ const styles = css({
51
+ background: 'transparent'
52
+ });
53
+ const styles2 = css({
54
+ background: url(image.png)
55
+ });
56
+ const styles3 = css({
57
+ background: 0
58
+ });
59
+ `,
60
+ },
61
+ // Usages of token within a template string are not used, so ESLint rule does not handle this case
62
+ {
63
+ name: 'background shorthand in a template literal as string text',
64
+ code: outdent`
65
+ import {css} from '@compiled/react';
66
+ const styles = css({
67
+ background: \`token('color.background.brand.bold', 'lightblue')\`,
68
+ });
69
+ `,
70
+ },
71
+ {
72
+ name: 'background as key in cssMap call (cssMap, @compiled/react)',
73
+ code: outdent`
74
+ import { cssMap } from '@compiled/react';
75
+ const colorMap = cssMap({
76
+ background: {
77
+ color: token('some.token')
78
+ },
79
+ foreground: {
80
+ color: token('some.other.token')
81
+ },
82
+ });
83
+ `,
84
+ },
85
+ ],
86
+ invalid: [
87
+ ...included_packages_calls_and_imports.map(([pkg, call, imp]) => ({
88
+ name: `simple case (${call}, ${imp})`,
89
+ code: outdent`
90
+ import {${pkg}} from '${imp}';
91
+ const styles = ${call}({
92
+ background: token('color.background.accent.gray.subtlest'),
93
+ });
94
+ `,
95
+ output: outdent`
96
+ import {${pkg}} from '${imp}';
97
+ const styles = ${call}({
98
+ backgroundColor: token('color.background.accent.gray.subtlest'),
99
+ });
100
+ `,
101
+ errors: [{ messageId: 'expandBackgroundShorthand' }],
102
+ })),
103
+ {
104
+ name: `simple case (styled, @compiled/react)`,
105
+ code: outdent`
106
+ import { styled } from '@compiled/react';
107
+ const Button = styled.div({
108
+ paddingTop: '10px',
109
+ });
110
+ const StyledButton = styled(Button)({
111
+ background: token('elevation.surface', colors.N0),
112
+ });
113
+ `,
114
+ output: outdent`
115
+ import { styled } from '@compiled/react';
116
+ const Button = styled.div({
117
+ paddingTop: '10px',
118
+ });
119
+ const StyledButton = styled(Button)({
120
+ backgroundColor: token('elevation.surface', colors.N0),
121
+ });
122
+ `,
123
+ errors: [{ messageId: 'expandBackgroundShorthand' }],
124
+ },
125
+ {
126
+ name: `simple case (cssMap, @compiled/react)`,
127
+ code: outdent`
128
+ import { cssMap } from '@compiled/react';
129
+ const backgroundMap = cssMap({
130
+ firstBackground: { background: token('some.token') },
131
+ secondBackground: { background: token('some.other.token') },
132
+ });
133
+ `,
134
+ output: outdent`
135
+ import { cssMap } from '@compiled/react';
136
+ const backgroundMap = cssMap({
137
+ firstBackground: { backgroundColor: token('some.token') },
138
+ secondBackground: { backgroundColor: token('some.other.token') },
139
+ });
140
+ `,
141
+ errors: Array.from(Array(2), () => ({ messageId: 'expandBackgroundShorthand' })),
142
+ },
143
+ {
144
+ name: `compiled import with alias`,
145
+ code: outdent`
146
+ import { styled as styled2 } from '@compiled/react';
147
+ const style = styled2.span({
148
+ background: token('color.background.neutral.subtle', '#fff'),
149
+ });
150
+ `,
151
+ output: outdent`
152
+ import { styled as styled2 } from '@compiled/react';
153
+ const style = styled2.span({
154
+ backgroundColor: token('color.background.neutral.subtle', '#fff'),
155
+ });
156
+ `,
157
+ errors: [{ messageId: 'expandBackgroundShorthand' }],
158
+ },
159
+ ],
160
+ });
@@ -0,0 +1,67 @@
1
+ import type { Rule } from 'eslint';
2
+ import type { Property, Node } from 'estree';
3
+ import {
4
+ getImportSources,
5
+ isCompiled,
6
+ isAtlasKitCSS,
7
+ } from '@atlaskit/eslint-utils/is-supported-import';
8
+
9
+ // Checks if the function that holds the border property is using an import package that this rule is targeting
10
+ const isCompiledAPI = (context: Rule.RuleContext): boolean => {
11
+ const importSources = getImportSources(context);
12
+ const { references } = context.getScope();
13
+ const ancestors = context.getAncestors();
14
+ if (
15
+ ancestors.some(
16
+ (ancestor) =>
17
+ ancestor.type === 'CallExpression' &&
18
+ ancestor.callee &&
19
+ (isCompiled(ancestor.callee, references, importSources) ||
20
+ isAtlasKitCSS(ancestor.callee, references, importSources)),
21
+ )
22
+ ) {
23
+ return true;
24
+ }
25
+ return false;
26
+ };
27
+
28
+ // Checks if node is a call expression with identifier 'token'
29
+ const isTokenCallExpression = (node: Node) => {
30
+ if (node.type === 'CallExpression') {
31
+ if (node.callee.type === 'Identifier' && node.callee.name === 'token') {
32
+ return true;
33
+ }
34
+ }
35
+ return false;
36
+ };
37
+
38
+ export const expandBackgroundShorthand: Rule.RuleModule = {
39
+ meta: {
40
+ docs: {
41
+ url: 'https://bitbucket.org/atlassian/atlassian-frontend-monorepo/src/master/platform/packages/platform/eslint-plugin/src/rules/compiled/expand-background-shorthand/',
42
+ },
43
+ messages: {
44
+ expandBackgroundShorthand: 'Use backgroundColor instead of background shorthand',
45
+ },
46
+ type: 'problem',
47
+ fixable: 'code',
48
+ },
49
+ create(context) {
50
+ return {
51
+ 'Property[key.name="background"]': function (node: Property) {
52
+ if (isCompiledAPI(context) && isTokenCallExpression(node.value)) {
53
+ context.report({
54
+ node,
55
+ messageId: 'expandBackgroundShorthand',
56
+ fix(fixer) {
57
+ return fixer.replaceText(node.key, `backgroundColor`);
58
+ },
59
+ });
60
+ return;
61
+ }
62
+ },
63
+ };
64
+ },
65
+ };
66
+
67
+ export default expandBackgroundShorthand;
package/tsconfig.dev.json CHANGED
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "references": [
36
36
  {
37
- "path": "tsconfig.app.json"
37
+ "path": "./tsconfig.app.json"
38
38
  }
39
39
  ],
40
40
  "files": []