@atlaskit/eslint-plugin-ui-styling-standard 0.11.1 → 0.11.2

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 (33) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -0
  3. package/constellation/index/overview.mdx +1 -0
  4. package/constellation/no-imported-style-values/usage.mdx +146 -0
  5. package/dist/cjs/presets/all.codegen.js +2 -1
  6. package/dist/cjs/presets/recommended.codegen.js +2 -1
  7. package/dist/cjs/rules/index.codegen.js +3 -1
  8. package/dist/cjs/rules/no-imported-style-values/index.js +145 -0
  9. package/dist/es2019/presets/all.codegen.js +2 -1
  10. package/dist/es2019/presets/recommended.codegen.js +2 -1
  11. package/dist/es2019/rules/index.codegen.js +3 -1
  12. package/dist/es2019/rules/no-imported-style-values/index.js +125 -0
  13. package/dist/esm/presets/all.codegen.js +2 -1
  14. package/dist/esm/presets/recommended.codegen.js +2 -1
  15. package/dist/esm/rules/index.codegen.js +3 -1
  16. package/dist/esm/rules/no-imported-style-values/index.js +138 -0
  17. package/dist/types/index.codegen.d.ts +2 -0
  18. package/dist/types/presets/all.codegen.d.ts +2 -1
  19. package/dist/types/presets/recommended.codegen.d.ts +2 -1
  20. package/dist/types/rules/index.codegen.d.ts +1 -0
  21. package/dist/types/rules/no-imported-style-values/index.d.ts +3 -0
  22. package/dist/types-ts4.5/index.codegen.d.ts +2 -0
  23. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -1
  24. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +2 -1
  25. package/dist/types-ts4.5/rules/index.codegen.d.ts +1 -0
  26. package/dist/types-ts4.5/rules/no-imported-style-values/index.d.ts +3 -0
  27. package/package.json +2 -2
  28. package/src/presets/all.codegen.tsx +2 -1
  29. package/src/presets/recommended.codegen.tsx +2 -1
  30. package/src/rules/index.codegen.tsx +3 -1
  31. package/src/rules/no-imported-style-values/README.md +144 -0
  32. package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +265 -0
  33. package/src/rules/no-imported-style-values/index.tsx +154 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atlaskit/eslint-plugin-ui-styling-standard
2
2
 
3
+ ## 0.11.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#94765](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/94765) [`8e5a3bddb686`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/8e5a3bddb686) - Adding no imported style values ESlint rule
8
+
3
9
  ## 0.11.1
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -22,6 +22,7 @@ Not intended for use outside the Atlassian frontend repository.
22
22
  | <a href="./src/rules/no-dynamic-styles/README.md">no-dynamic-styles</a> | Disallows use of dynamic styles in CSS-in-JS calls | Yes | | |
23
23
  | <a href="./src/rules/no-exported-styles/README.md">no-exported-styles</a> | Disallows exports of css, keyframes, styled and xcss | Yes | | |
24
24
  | <a href="./src/rules/no-important-styles/README.md">no-important-styles</a> | Disallows important style declarations | Yes | | |
25
+ | <a href="./src/rules/no-imported-style-values/README.md">no-imported-style-values</a> | Disallows imports of style values | Yes | | |
25
26
  | <a href="./src/rules/no-nested-selectors/README.md">no-nested-selectors</a> | Prevents usage of nested selectors within css styling | Yes | | |
26
27
  | <a href="./src/rules/no-unsafe-values/README.md">no-unsafe-values</a> | Disallows styles that are difficult/impossible to statically anaylze. | Yes | | |
27
28
  | <a href="https://atlassian.design/components/eslint-plugin-ui-styling-standard/consistent-css-prop-usage/usage">@atlaskit/design-system/consistent-css-prop-usage</a> | Ensures consistency with `css` and `xcss` prop usages | Yes | Yes | |
@@ -20,6 +20,7 @@ but more are planned to support the standard over time.
20
20
  | <a href="no-dynamic-styles/usage">no-dynamic-styles</a> | Disallows use of dynamic styles in CSS-in-JS calls | Yes | | |
21
21
  | <a href="no-exported-styles/usage">no-exported-styles</a> | Disallows exports of css, keyframes, styled and xcss | Yes | | |
22
22
  | <a href="no-important-styles/usage">no-important-styles</a> | Disallows important style declarations | Yes | | |
23
+ | <a href="no-imported-style-values/usage">no-imported-style-values</a> | Disallows imports of style values | Yes | | |
23
24
  | <a href="no-nested-selectors/usage">no-nested-selectors</a> | Prevents usage of nested selectors within css styling | Yes | | |
24
25
  | <a href="no-unsafe-values/usage">no-unsafe-values</a> | Disallows styles that are difficult/impossible to statically anaylze. | Yes | | |
25
26
  | <a href="consistent-css-prop-usage/usage">@atlaskit/design-system/consistent-css-prop-usage</a> | Ensures consistency with `css` and `xcss` prop usages | Yes | Yes | |
@@ -0,0 +1,146 @@
1
+ # no-imported-style-values
2
+
3
+ This rule disallows using imported style values in `css`, `cssMap`, `styled`, `keyframes` and `xcss` calls.
4
+
5
+ Importing style values may result in unexpected errors when used in these APIs, because its value will be null at runtime when using `@compiled/react`. Additionally, co-locating style definitions with their usage is considered best practice in order to improve code readability and build performance.
6
+
7
+ ## Examples
8
+
9
+ ### Incorrect
10
+
11
+ ```js
12
+ import { css } from '@compiled/react';
13
+ import { colors, getColor } from '@mui/theme';
14
+
15
+ const styles = css({
16
+ [colorKey]: getColor('red'),
17
+ background: colors['red'],
18
+ });
19
+ ```
20
+
21
+ ```js
22
+ import { css } from '@compiled/react';
23
+ import { buttonStyles, cssShared, HEIGHT, colorKey } from '../shared';
24
+ import { ff } from '@atlaskit/ff';
25
+
26
+ const sharedObject = { padding: 0 };
27
+
28
+ const styles = css({
29
+ ...cssShared,
30
+ ...sharedObject,
31
+ height: `${HEIGHT}px`,
32
+ width: ff('…') ? `${HEIGHT}px` : undefined,
33
+ });
34
+ ```
35
+
36
+ Importing styles to use in style composition is not allowed.
37
+
38
+ ```js
39
+ import { css } from '@compiled/react';
40
+ import { buttonStyles } from '../shared';
41
+
42
+ const styles = css({
43
+ color: 'red',
44
+ });
45
+
46
+ export default () => <div css={[styles, buttonStyles]} />;
47
+ ```
48
+
49
+ Importing styles to pass to the style prop is also not allowed.
50
+
51
+ ```js
52
+ import { importedWidth } from '../shared';
53
+
54
+ export default () => <div style={{ width: importedWidth }} />;
55
+ ```
56
+
57
+ ### Correct
58
+
59
+ Co-locate your styles next to your components to improve code readability, linting, and build performance.
60
+
61
+ ```js
62
+ import { css } from '@compiled/react';
63
+ import { token } from '@atlaskit/tokens';
64
+
65
+ const styles = css({
66
+ color: 'red',
67
+ padding: token('space.150'),
68
+ });
69
+ export const Component = ({ children }) => <div css={styles}>{children}></div>;
70
+ ```
71
+
72
+ ```js
73
+ import { keyframes } from '@compiled/react';
74
+
75
+ const animation = keyframes({});
76
+ const styles = css({ animate: `${animation} 1s ease-in` });
77
+ export const Component = ({ children }) => <div css={styles}>{children}></div>;
78
+ ```
79
+
80
+ ## Options
81
+
82
+ ### `allowedDynamicKeys: [string, string][]`
83
+
84
+ Use this to allow specified imports as dynamic keys, in addition to the built-in allow-list.
85
+
86
+ Each value should be a two-element array. The first item is the entrypoint, and the second item is a named export.
87
+
88
+ Default imports are not supported.
89
+
90
+ ```tsx
91
+ // .eslintrc.js
92
+
93
+ // ...
94
+ rules: {
95
+ '@atlaskit/eslint-plugin-ui-styling-standard/no-unsafe-values': [
96
+ 'error',
97
+ {
98
+ allowedDynamicKeys: [
99
+ ['@atlaskit/primitives/responsive', 'media'],
100
+ ]
101
+ },
102
+ ],
103
+ // ...
104
+ },
105
+ // ...
106
+ ```
107
+
108
+ ### `allowedFunctionCalls: [string, string][]`
109
+
110
+ Use this to allow specific functions to be called, in addition to the built-in allow-list.
111
+
112
+ Each value should be a two-element array. The first item is the entrypoint, and the second item is a named export.
113
+
114
+ Default imports are not currently supported.
115
+
116
+ ```tsx
117
+ // .eslintrc.js
118
+
119
+ // ...
120
+ rules: {
121
+ '@atlaskit/eslint-plugin-ui-styling-standard/no-unsafe-values': [
122
+ 'error',
123
+ {
124
+ allowedFunctionCalls: [
125
+ ['@atlaskit/tokens', 'token'],
126
+ ]
127
+ },
128
+ ],
129
+ // ...
130
+ },
131
+ // ...
132
+ ```
133
+
134
+ ### `importSources: string[]`
135
+
136
+ By default, this rule will check `css` usages from:
137
+
138
+ - `@atlaskit/css`
139
+ - `@atlaskit/primitives`
140
+ - `@compiled/react`
141
+ - `@emotion/react`
142
+ - `@emotion/core`
143
+ - `@emotion/styled`
144
+ - `styled-components`
145
+
146
+ To change this list of libraries, you can define a custom set of `importSources`, which accepts an array of package names (strings).
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  /**
8
8
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
9
- * @codegen <<SignedSource::42dc97374e2e4fd9ea068b3564bdcb72>>
9
+ * @codegen <<SignedSource::96336e6094dbcbf456aaef313b624fbf>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -21,6 +21,7 @@ var _default = exports.default = {
21
21
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
22
22
  '@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
23
23
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
24
+ '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
24
25
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
25
26
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
26
27
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  /**
8
8
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
9
- * @codegen <<SignedSource::42dc97374e2e4fd9ea068b3564bdcb72>>
9
+ * @codegen <<SignedSource::96336e6094dbcbf456aaef313b624fbf>>
10
10
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
11
11
  */
12
12
  var _default = exports.default = {
@@ -21,6 +21,7 @@ var _default = exports.default = {
21
21
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
22
22
  '@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
23
23
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
24
+ '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
24
25
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
25
26
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
26
27
  '@atlaskit/design-system/consistent-css-prop-usage': ['warn', {
@@ -14,11 +14,12 @@ var _noContainerQueries = _interopRequireDefault(require("./no-container-queries
14
14
  var _noDynamicStyles = _interopRequireDefault(require("./no-dynamic-styles"));
15
15
  var _noExportedStyles = _interopRequireDefault(require("./no-exported-styles"));
16
16
  var _noImportantStyles = _interopRequireDefault(require("./no-important-styles"));
17
+ var _noImportedStyleValues = _interopRequireDefault(require("./no-imported-style-values"));
17
18
  var _noNestedSelectors = _interopRequireDefault(require("./no-nested-selectors"));
18
19
  var _noUnsafeValues = _interopRequireDefault(require("./no-unsafe-values"));
19
20
  /**
20
21
  * THIS FILE WAS CREATED VIA CODEGEN DO NOT MODIFY {@see http://go/af-codegen}
21
- * @codegen <<SignedSource::1c60c9984196cab3d54e3144c59ba97c>>
22
+ * @codegen <<SignedSource::648acb81f0f43d8ce56f7d6c22db8856>>
22
23
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
23
24
  */
24
25
  var _default = exports.default = {
@@ -31,6 +32,7 @@ var _default = exports.default = {
31
32
  'no-dynamic-styles': _noDynamicStyles.default,
32
33
  'no-exported-styles': _noExportedStyles.default,
33
34
  'no-important-styles': _noImportantStyles.default,
35
+ 'no-imported-style-values': _noImportedStyleValues.default,
34
36
  'no-nested-selectors': _noNestedSelectors.default,
35
37
  'no-unsafe-values': _noUnsafeValues.default
36
38
  };
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.rule = exports.default = void 0;
8
+ var _createRule = require("../utils/create-rule");
9
+ var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
10
+ var _findVariable = require("@atlaskit/eslint-utils/find-variable");
11
+ var _allowedFunctionCalls = require("@atlaskit/eslint-utils/allowed-function-calls");
12
+ var _esquery = _interopRequireDefault(require("esquery"));
13
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
14
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
15
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
16
+ var schema = [{
17
+ type: 'object',
18
+ properties: {
19
+ importSources: {
20
+ type: 'array',
21
+ items: {
22
+ type: 'string'
23
+ },
24
+ uniqueItems: true
25
+ },
26
+ allowedFunctionCalls: {
27
+ type: 'array',
28
+ items: {
29
+ type: 'array',
30
+ minItems: 2,
31
+ maxItems: 2,
32
+ items: [{
33
+ type: 'string'
34
+ }, {
35
+ type: 'string'
36
+ }]
37
+ },
38
+ uniqueItems: true
39
+ },
40
+ allowedDynamicKeys: {
41
+ type: 'array',
42
+ items: {
43
+ type: 'array',
44
+ minItems: 2,
45
+ maxItems: 2,
46
+ items: [{
47
+ type: 'string'
48
+ }, {
49
+ type: 'string'
50
+ }]
51
+ },
52
+ uniqueItems: true
53
+ }
54
+ }
55
+ }];
56
+ var isIdentifierImported = function isIdentifierImported(identifier, context, functionAllowList, keysAllowList) {
57
+ var variable = (0, _findVariable.findVariable)({
58
+ identifier: identifier,
59
+ sourceCode: context.sourceCode
60
+ });
61
+ if (!variable) {
62
+ return false;
63
+ }
64
+ var isImported = variable.defs.some(function (definition) {
65
+ return definition.type === 'ImportBinding';
66
+ });
67
+
68
+ // check if imported variable is allowed
69
+ return isImported && !(0, _allowedFunctionCalls.isAllowListedVariable)({
70
+ allowList: functionAllowList,
71
+ variable: variable
72
+ }) && !(0, _allowedFunctionCalls.isAllowListedVariable)({
73
+ allowList: keysAllowList,
74
+ variable: variable
75
+ });
76
+ };
77
+ var checkIdentifier = function checkIdentifier(identifier, context, functionAllowList, keysAllowList) {
78
+ if (isIdentifierImported(identifier, context, functionAllowList, keysAllowList)) {
79
+ context.report({
80
+ node: identifier,
81
+ messageId: 'no-imported-style-values'
82
+ });
83
+ }
84
+ };
85
+ var rule = exports.rule = (0, _createRule.createLintRule)({
86
+ meta: {
87
+ name: 'no-imported-style-values',
88
+ docs: {
89
+ description: 'Disallows imports of style values',
90
+ recommended: true,
91
+ severity: 'warn'
92
+ },
93
+ messages: {
94
+ 'no-imported-style-values': 'Styles should not be imported to use in css, cssMap, styled, keyframes or xcss calls. All styles should be defined and used in the same file instead.'
95
+ },
96
+ type: 'problem',
97
+ schema: schema
98
+ },
99
+ create: function create(context) {
100
+ var importSources = (0, _isSupportedImport.getImportSources)(context);
101
+ var functionAllowList = (0, _allowedFunctionCalls.getAllowedFunctionCalls)(context.options);
102
+ var keysAllowList = (0, _allowedFunctionCalls.getAllowedDynamicKeys)(context.options);
103
+ var checkForIdentifiers = function checkForIdentifiers(node) {
104
+ var matches = (0, _esquery.default)(node, 'Identifier');
105
+ var _iterator = _createForOfIteratorHelper(matches),
106
+ _step;
107
+ try {
108
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
109
+ var match = _step.value;
110
+ if (match.type === 'Identifier') {
111
+ checkIdentifier(match, context, functionAllowList, keysAllowList);
112
+ }
113
+ }
114
+ } catch (err) {
115
+ _iterator.e(err);
116
+ } finally {
117
+ _iterator.f();
118
+ }
119
+ };
120
+ return {
121
+ // Checking css/cssMap/keyframes/styled/xcss calls
122
+ CallExpression: function CallExpression(node) {
123
+ var _context$sourceCode$g = context.sourceCode.getScope(node.callee),
124
+ references = _context$sourceCode$g.references;
125
+ if (!(0, _isSupportedImport.hasStyleObjectArguments)(node.callee, references, importSources)) {
126
+ return;
127
+ }
128
+ node.arguments.forEach(checkForIdentifiers);
129
+ },
130
+ 'JSXAttribute[name.name="css"] Identifier': function JSXAttributeNameNameCssIdentifier(node) {
131
+ if (node.type !== 'Identifier') {
132
+ return;
133
+ }
134
+ checkIdentifier(node, context, functionAllowList, keysAllowList);
135
+ },
136
+ 'JSXAttribute[name.name="style"] Identifier': function JSXAttributeNameNameStyleIdentifier(node) {
137
+ if (node.type !== 'Identifier') {
138
+ return;
139
+ }
140
+ checkIdentifier(node, context, functionAllowList, keysAllowList);
141
+ }
142
+ };
143
+ }
144
+ });
145
+ var _default = exports.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::42dc97374e2e4fd9ea068b3564bdcb72>>
3
+ * @codegen <<SignedSource::96336e6094dbcbf456aaef313b624fbf>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -15,6 +15,7 @@ export default {
15
15
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
16
16
  '@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
17
17
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
18
+ '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
18
19
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
19
20
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
20
21
  '@atlaskit/design-system/consistent-css-prop-usage': ['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::42dc97374e2e4fd9ea068b3564bdcb72>>
3
+ * @codegen <<SignedSource::96336e6094dbcbf456aaef313b624fbf>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -15,6 +15,7 @@ export default {
15
15
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
16
16
  '@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
17
17
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
18
+ '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
18
19
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
19
20
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
20
21
  '@atlaskit/design-system/consistent-css-prop-usage': ['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::1c60c9984196cab3d54e3144c59ba97c>>
3
+ * @codegen <<SignedSource::648acb81f0f43d8ce56f7d6c22db8856>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
@@ -12,6 +12,7 @@ import noContainerQueries from './no-container-queries';
12
12
  import noDynamicStyles from './no-dynamic-styles';
13
13
  import noExportedStyles from './no-exported-styles';
14
14
  import noImportantStyles from './no-important-styles';
15
+ import noImportedStyleValues from './no-imported-style-values';
15
16
  import noNestedSelectors from './no-nested-selectors';
16
17
  import noUnsafeValues from './no-unsafe-values';
17
18
  export default {
@@ -24,6 +25,7 @@ export default {
24
25
  'no-dynamic-styles': noDynamicStyles,
25
26
  'no-exported-styles': noExportedStyles,
26
27
  'no-important-styles': noImportantStyles,
28
+ 'no-imported-style-values': noImportedStyleValues,
27
29
  'no-nested-selectors': noNestedSelectors,
28
30
  'no-unsafe-values': noUnsafeValues
29
31
  };
@@ -0,0 +1,125 @@
1
+ import { createLintRule } from '../utils/create-rule';
2
+ import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
3
+ import { findVariable } from '@atlaskit/eslint-utils/find-variable';
4
+ import { getAllowedDynamicKeys, getAllowedFunctionCalls, isAllowListedVariable } from '@atlaskit/eslint-utils/allowed-function-calls';
5
+ import esquery from 'esquery';
6
+ const schema = [{
7
+ type: 'object',
8
+ properties: {
9
+ importSources: {
10
+ type: 'array',
11
+ items: {
12
+ type: 'string'
13
+ },
14
+ uniqueItems: true
15
+ },
16
+ allowedFunctionCalls: {
17
+ type: 'array',
18
+ items: {
19
+ type: 'array',
20
+ minItems: 2,
21
+ maxItems: 2,
22
+ items: [{
23
+ type: 'string'
24
+ }, {
25
+ type: 'string'
26
+ }]
27
+ },
28
+ uniqueItems: true
29
+ },
30
+ allowedDynamicKeys: {
31
+ type: 'array',
32
+ items: {
33
+ type: 'array',
34
+ minItems: 2,
35
+ maxItems: 2,
36
+ items: [{
37
+ type: 'string'
38
+ }, {
39
+ type: 'string'
40
+ }]
41
+ },
42
+ uniqueItems: true
43
+ }
44
+ }
45
+ }];
46
+ const isIdentifierImported = (identifier, context, functionAllowList, keysAllowList) => {
47
+ const variable = findVariable({
48
+ identifier: identifier,
49
+ sourceCode: context.sourceCode
50
+ });
51
+ if (!variable) {
52
+ return false;
53
+ }
54
+ const isImported = variable.defs.some(definition => definition.type === 'ImportBinding');
55
+
56
+ // check if imported variable is allowed
57
+ return isImported && !isAllowListedVariable({
58
+ allowList: functionAllowList,
59
+ variable
60
+ }) && !isAllowListedVariable({
61
+ allowList: keysAllowList,
62
+ variable
63
+ });
64
+ };
65
+ const checkIdentifier = (identifier, context, functionAllowList, keysAllowList) => {
66
+ if (isIdentifierImported(identifier, context, functionAllowList, keysAllowList)) {
67
+ context.report({
68
+ node: identifier,
69
+ messageId: 'no-imported-style-values'
70
+ });
71
+ }
72
+ };
73
+ export const rule = createLintRule({
74
+ meta: {
75
+ name: 'no-imported-style-values',
76
+ docs: {
77
+ description: 'Disallows imports of style values',
78
+ recommended: true,
79
+ severity: 'warn'
80
+ },
81
+ messages: {
82
+ 'no-imported-style-values': 'Styles should not be imported to use in css, cssMap, styled, keyframes or xcss calls. All styles should be defined and used in the same file instead.'
83
+ },
84
+ type: 'problem',
85
+ schema
86
+ },
87
+ create(context) {
88
+ const importSources = getImportSources(context);
89
+ const functionAllowList = getAllowedFunctionCalls(context.options);
90
+ const keysAllowList = getAllowedDynamicKeys(context.options);
91
+ const checkForIdentifiers = node => {
92
+ const matches = esquery(node, 'Identifier');
93
+ for (const match of matches) {
94
+ if (match.type === 'Identifier') {
95
+ checkIdentifier(match, context, functionAllowList, keysAllowList);
96
+ }
97
+ }
98
+ };
99
+ return {
100
+ // Checking css/cssMap/keyframes/styled/xcss calls
101
+ CallExpression(node) {
102
+ const {
103
+ references
104
+ } = context.sourceCode.getScope(node.callee);
105
+ if (!hasStyleObjectArguments(node.callee, references, importSources)) {
106
+ return;
107
+ }
108
+ node.arguments.forEach(checkForIdentifiers);
109
+ },
110
+ 'JSXAttribute[name.name="css"] Identifier': node => {
111
+ if (node.type !== 'Identifier') {
112
+ return;
113
+ }
114
+ checkIdentifier(node, context, functionAllowList, keysAllowList);
115
+ },
116
+ 'JSXAttribute[name.name="style"] Identifier': node => {
117
+ if (node.type !== 'Identifier') {
118
+ return;
119
+ }
120
+ checkIdentifier(node, context, functionAllowList, keysAllowList);
121
+ }
122
+ };
123
+ }
124
+ });
125
+ 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::42dc97374e2e4fd9ea068b3564bdcb72>>
3
+ * @codegen <<SignedSource::96336e6094dbcbf456aaef313b624fbf>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -15,6 +15,7 @@ export default {
15
15
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
16
16
  '@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
17
17
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
18
+ '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
18
19
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
19
20
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
20
21
  '@atlaskit/design-system/consistent-css-prop-usage': ['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::42dc97374e2e4fd9ea068b3564bdcb72>>
3
+ * @codegen <<SignedSource::96336e6094dbcbf456aaef313b624fbf>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  export default {
@@ -15,6 +15,7 @@ export default {
15
15
  '@atlaskit/ui-styling-standard/no-dynamic-styles': 'warn',
16
16
  '@atlaskit/ui-styling-standard/no-exported-styles': 'warn',
17
17
  '@atlaskit/ui-styling-standard/no-important-styles': 'warn',
18
+ '@atlaskit/ui-styling-standard/no-imported-style-values': 'warn',
18
19
  '@atlaskit/ui-styling-standard/no-nested-selectors': 'warn',
19
20
  '@atlaskit/ui-styling-standard/no-unsafe-values': 'warn',
20
21
  '@atlaskit/design-system/consistent-css-prop-usage': ['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::1c60c9984196cab3d54e3144c59ba97c>>
3
+ * @codegen <<SignedSource::648acb81f0f43d8ce56f7d6c22db8856>>
4
4
  * @codegenCommand yarn workspace @atlaskit/eslint-plugin-ui-styling-standard codegen
5
5
  */
6
6
  import convertPropsSyntax from './convert-props-syntax';
@@ -12,6 +12,7 @@ import noContainerQueries from './no-container-queries';
12
12
  import noDynamicStyles from './no-dynamic-styles';
13
13
  import noExportedStyles from './no-exported-styles';
14
14
  import noImportantStyles from './no-important-styles';
15
+ import noImportedStyleValues from './no-imported-style-values';
15
16
  import noNestedSelectors from './no-nested-selectors';
16
17
  import noUnsafeValues from './no-unsafe-values';
17
18
  export default {
@@ -24,6 +25,7 @@ export default {
24
25
  'no-dynamic-styles': noDynamicStyles,
25
26
  'no-exported-styles': noExportedStyles,
26
27
  'no-important-styles': noImportantStyles,
28
+ 'no-imported-style-values': noImportedStyleValues,
27
29
  'no-nested-selectors': noNestedSelectors,
28
30
  'no-unsafe-values': noUnsafeValues
29
31
  };