@atlaskit/eslint-plugin-ui-styling-standard 0.11.3 → 0.11.5
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 +152 -53
- package/README.md +30 -3
- package/constellation/index/props.mdx +2 -9
- package/constellation/no-unsafe-values/usage.mdx +1 -1
- package/dist/cjs/rules/enforce-style-prop/index.js +39 -26
- package/dist/cjs/rules/no-important-styles/index.js +41 -29
- package/dist/es2019/rules/enforce-style-prop/index.js +34 -19
- package/dist/es2019/rules/no-important-styles/index.js +28 -30
- package/dist/esm/rules/enforce-style-prop/index.js +39 -26
- package/dist/esm/rules/no-important-styles/index.js +42 -30
- package/package.json +3 -3
- package/scripts/codegen.tsx +3 -10
- package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +18 -0
- package/src/rules/enforce-style-prop/index.tsx +42 -23
- package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +16 -0
- package/src/rules/no-exported-styles/index.tsx +1 -1
- package/src/rules/no-important-styles/__tests__/rule.test.tsx +22 -9
- package/src/rules/no-important-styles/index.tsx +32 -37
- package/src/rules/no-imported-style-values/index.tsx +1 -1
- package/src/rules/no-unsafe-values/README.md +1 -1
- package/tsconfig.app.json +3 -0
|
@@ -5,9 +5,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.rule = exports.default = void 0;
|
|
8
|
-
var
|
|
8
|
+
var _esquery = _interopRequireDefault(require("esquery"));
|
|
9
9
|
var _createRule = require("../utils/create-rule");
|
|
10
10
|
var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import");
|
|
11
|
+
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; } } }; }
|
|
12
|
+
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); }
|
|
13
|
+
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; }
|
|
11
14
|
var schema = [{
|
|
12
15
|
type: 'object',
|
|
13
16
|
properties: {
|
|
@@ -40,36 +43,45 @@ var rule = exports.rule = (0, _createRule.createLintRule)({
|
|
|
40
43
|
CallExpression: function CallExpression(node) {
|
|
41
44
|
var _context$getScope = context.getScope(),
|
|
42
45
|
references = _context$getScope.references;
|
|
43
|
-
if ((0, _isSupportedImport.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
* This is needed to handle unknown node types. Otherwise an error is thrown.
|
|
67
|
-
*/
|
|
68
|
-
fallback: 'iteration'
|
|
69
|
-
});
|
|
46
|
+
if (!(0, _isSupportedImport.hasStyleObjectArguments)(node.callee, references, importSources)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
var matches = (0, _esquery.default)(node, 'Property');
|
|
50
|
+
var _iterator = _createForOfIteratorHelper(matches),
|
|
51
|
+
_step;
|
|
52
|
+
try {
|
|
53
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
54
|
+
var match = _step.value;
|
|
55
|
+
if (match.type !== 'Property') {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (isImportant(match.value)) {
|
|
59
|
+
context.report({
|
|
60
|
+
node: match.value,
|
|
61
|
+
messageId: 'no-important-styles'
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
} catch (err) {
|
|
66
|
+
_iterator.e(err);
|
|
67
|
+
} finally {
|
|
68
|
+
_iterator.f();
|
|
70
69
|
}
|
|
71
70
|
}
|
|
72
71
|
};
|
|
73
72
|
}
|
|
74
73
|
});
|
|
75
|
-
var _default = exports.default = rule;
|
|
74
|
+
var _default = exports.default = rule;
|
|
75
|
+
var importantRegex = /!\s*important\s*$/;
|
|
76
|
+
function isImportant(node) {
|
|
77
|
+
if (node.type === 'Literal') {
|
|
78
|
+
return typeof node.value === 'string' && importantRegex.test(node.value);
|
|
79
|
+
}
|
|
80
|
+
if (node.type === 'TemplateLiteral') {
|
|
81
|
+
var joinedRaw = node.quasis.map(function (element) {
|
|
82
|
+
return element.value.raw;
|
|
83
|
+
}).join('');
|
|
84
|
+
return importantRegex.test(joinedRaw);
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
2
|
import { createLintRule } from '../utils/create-rule';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const isParameter = (context, variableName) => {
|
|
3
|
+
import { getAllowedFunctionCalls, isAllowListedVariable } from '@atlaskit/eslint-utils/allowed-function-calls';
|
|
4
|
+
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
5
|
+
const isParameter = (context, identifier) => {
|
|
7
6
|
var _findVariable;
|
|
8
|
-
const definitions = (_findVariable = findVariable(
|
|
7
|
+
const definitions = (_findVariable = findVariable({
|
|
8
|
+
sourceCode: context.sourceCode,
|
|
9
|
+
identifier: identifier
|
|
10
|
+
})) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
|
|
9
11
|
if (!definitions || !definitions.length) {
|
|
10
12
|
return false;
|
|
11
13
|
}
|
|
12
14
|
const result = definitions[0];
|
|
13
15
|
return result.type === 'Parameter';
|
|
14
16
|
};
|
|
15
|
-
const
|
|
17
|
+
const isIdentifierAllowed = (context, identifier) => {
|
|
16
18
|
var _parent;
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
+
const variable = findVariable({
|
|
20
|
+
sourceCode: context.sourceCode,
|
|
21
|
+
identifier: identifier
|
|
22
|
+
});
|
|
19
23
|
if (!variable) {
|
|
20
24
|
return false;
|
|
21
25
|
}
|
|
@@ -28,7 +32,7 @@ const checkIdentifier = (context, identifier) => {
|
|
|
28
32
|
|
|
29
33
|
// Checking if identifier has been destructured from props within function
|
|
30
34
|
if (variableDefinition.type === 'VariableDeclarator') {
|
|
31
|
-
var _variableDefinition$i, _variableDefinition$i2
|
|
35
|
+
var _variableDefinition$i, _variableDefinition$i2;
|
|
32
36
|
// If it's a let variable, ignore
|
|
33
37
|
if (variableDefinition.parent.kind === 'let') {
|
|
34
38
|
return true;
|
|
@@ -40,16 +44,14 @@ const checkIdentifier = (context, identifier) => {
|
|
|
40
44
|
// Destructured from member expression
|
|
41
45
|
// e.g. const width = props.width
|
|
42
46
|
if (((_variableDefinition$i2 = variableDefinition.init) === null || _variableDefinition$i2 === void 0 ? void 0 : _variableDefinition$i2.type) === 'MemberExpression') {
|
|
43
|
-
|
|
44
|
-
return isParameter(context, memberIdentifierName);
|
|
47
|
+
return isParameter(context, variableDefinition.init.object);
|
|
45
48
|
}
|
|
46
49
|
// Destructured from object pattern
|
|
47
50
|
// e.g. const { width } = props
|
|
48
|
-
|
|
49
|
-
return isParameter(context, propName);
|
|
51
|
+
return isParameter(context, variableDefinition.init);
|
|
50
52
|
}
|
|
51
53
|
// If identifier has not been deconstructed from parameter or is a RestElement, report warning
|
|
52
|
-
return isParameter(context,
|
|
54
|
+
return isParameter(context, identifier);
|
|
53
55
|
};
|
|
54
56
|
function isPropertyValueAllowed(property, context) {
|
|
55
57
|
if (property.type === 'SpreadElement') {
|
|
@@ -59,13 +61,26 @@ function isPropertyValueAllowed(property, context) {
|
|
|
59
61
|
if (property.value.type === 'Literal') {
|
|
60
62
|
return false;
|
|
61
63
|
} else if (property.value.type === 'Identifier') {
|
|
62
|
-
return
|
|
63
|
-
} else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier'
|
|
64
|
-
|
|
64
|
+
return isIdentifierAllowed(context, property.value);
|
|
65
|
+
} else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier') {
|
|
66
|
+
const variable = findVariable({
|
|
67
|
+
sourceCode: context.sourceCode,
|
|
68
|
+
identifier: property.value.callee
|
|
69
|
+
});
|
|
70
|
+
// Anything returned by getAllowedFunctionCalls should be used in css prop instead
|
|
71
|
+
const functionAllowList = getAllowedFunctionCalls(context.options);
|
|
72
|
+
if (!variable || isAllowListedVariable({
|
|
73
|
+
allowList: functionAllowList,
|
|
74
|
+
variable
|
|
75
|
+
})) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
65
78
|
} else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
|
|
66
79
|
var _findVariable2;
|
|
67
|
-
const
|
|
68
|
-
|
|
80
|
+
const definitions = (_findVariable2 = findVariable({
|
|
81
|
+
sourceCode: context.sourceCode,
|
|
82
|
+
identifier: property.value.object
|
|
83
|
+
})) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
|
|
69
84
|
if (!definitions || !definitions.length) {
|
|
70
85
|
return false;
|
|
71
86
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import esquery from 'esquery';
|
|
2
2
|
import { createLintRule } from '../utils/create-rule';
|
|
3
|
-
import {
|
|
3
|
+
import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
|
|
4
4
|
const schema = [{
|
|
5
5
|
type: 'object',
|
|
6
6
|
properties: {
|
|
@@ -34,36 +34,34 @@ export const rule = createLintRule({
|
|
|
34
34
|
const {
|
|
35
35
|
references
|
|
36
36
|
} = context.getScope();
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
*/
|
|
52
|
-
if ((_node$value$value = node.value.value) !== null && _node$value$value !== void 0 && _node$value$value.toString().match(/!\s*important\s*$/gm)) {
|
|
53
|
-
context.report({
|
|
54
|
-
node: node.value,
|
|
55
|
-
messageId: 'no-important-styles'
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
/**
|
|
60
|
-
* This is needed to handle unknown node types. Otherwise an error is thrown.
|
|
61
|
-
*/
|
|
62
|
-
fallback: 'iteration'
|
|
63
|
-
});
|
|
37
|
+
if (!hasStyleObjectArguments(node.callee, references, importSources)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const matches = esquery(node, 'Property');
|
|
41
|
+
for (const match of matches) {
|
|
42
|
+
if (match.type !== 'Property') {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (isImportant(match.value)) {
|
|
46
|
+
context.report({
|
|
47
|
+
node: match.value,
|
|
48
|
+
messageId: 'no-important-styles'
|
|
49
|
+
});
|
|
50
|
+
}
|
|
64
51
|
}
|
|
65
52
|
}
|
|
66
53
|
};
|
|
67
54
|
}
|
|
68
55
|
});
|
|
69
|
-
export default rule;
|
|
56
|
+
export default rule;
|
|
57
|
+
const importantRegex = /!\s*important\s*$/;
|
|
58
|
+
function isImportant(node) {
|
|
59
|
+
if (node.type === 'Literal') {
|
|
60
|
+
return typeof node.value === 'string' && importantRegex.test(node.value);
|
|
61
|
+
}
|
|
62
|
+
if (node.type === 'TemplateLiteral') {
|
|
63
|
+
const joinedRaw = node.quasis.map(element => element.value.raw).join('');
|
|
64
|
+
return importantRegex.test(joinedRaw);
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
import { isNodeOfType } from 'eslint-codemod-utils';
|
|
2
2
|
import { createLintRule } from '../utils/create-rule';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
});
|
|
7
|
-
};
|
|
8
|
-
var isParameter = function isParameter(context, variableName) {
|
|
3
|
+
import { getAllowedFunctionCalls, isAllowListedVariable } from '@atlaskit/eslint-utils/allowed-function-calls';
|
|
4
|
+
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
5
|
+
var isParameter = function isParameter(context, identifier) {
|
|
9
6
|
var _findVariable;
|
|
10
|
-
var definitions = (_findVariable = findVariable(
|
|
7
|
+
var definitions = (_findVariable = findVariable({
|
|
8
|
+
sourceCode: context.sourceCode,
|
|
9
|
+
identifier: identifier
|
|
10
|
+
})) === null || _findVariable === void 0 ? void 0 : _findVariable.defs;
|
|
11
11
|
if (!definitions || !definitions.length) {
|
|
12
12
|
return false;
|
|
13
13
|
}
|
|
14
14
|
var result = definitions[0];
|
|
15
15
|
return result.type === 'Parameter';
|
|
16
16
|
};
|
|
17
|
-
var
|
|
17
|
+
var isIdentifierAllowed = function isIdentifierAllowed(context, identifier) {
|
|
18
18
|
var _parent;
|
|
19
|
-
var
|
|
20
|
-
|
|
19
|
+
var variable = findVariable({
|
|
20
|
+
sourceCode: context.sourceCode,
|
|
21
|
+
identifier: identifier
|
|
22
|
+
});
|
|
21
23
|
if (!variable) {
|
|
22
24
|
return false;
|
|
23
25
|
}
|
|
@@ -30,7 +32,7 @@ var checkIdentifier = function checkIdentifier(context, identifier) {
|
|
|
30
32
|
|
|
31
33
|
// Checking if identifier has been destructured from props within function
|
|
32
34
|
if (variableDefinition.type === 'VariableDeclarator') {
|
|
33
|
-
var _variableDefinition$i, _variableDefinition$i2
|
|
35
|
+
var _variableDefinition$i, _variableDefinition$i2;
|
|
34
36
|
// If it's a let variable, ignore
|
|
35
37
|
if (variableDefinition.parent.kind === 'let') {
|
|
36
38
|
return true;
|
|
@@ -42,16 +44,14 @@ var checkIdentifier = function checkIdentifier(context, identifier) {
|
|
|
42
44
|
// Destructured from member expression
|
|
43
45
|
// e.g. const width = props.width
|
|
44
46
|
if (((_variableDefinition$i2 = variableDefinition.init) === null || _variableDefinition$i2 === void 0 ? void 0 : _variableDefinition$i2.type) === 'MemberExpression') {
|
|
45
|
-
|
|
46
|
-
return isParameter(context, memberIdentifierName);
|
|
47
|
+
return isParameter(context, variableDefinition.init.object);
|
|
47
48
|
}
|
|
48
49
|
// Destructured from object pattern
|
|
49
50
|
// e.g. const { width } = props
|
|
50
|
-
|
|
51
|
-
return isParameter(context, propName);
|
|
51
|
+
return isParameter(context, variableDefinition.init);
|
|
52
52
|
}
|
|
53
53
|
// If identifier has not been deconstructed from parameter or is a RestElement, report warning
|
|
54
|
-
return isParameter(context,
|
|
54
|
+
return isParameter(context, identifier);
|
|
55
55
|
};
|
|
56
56
|
function isPropertyValueAllowed(property, context) {
|
|
57
57
|
if (property.type === 'SpreadElement') {
|
|
@@ -61,26 +61,39 @@ function isPropertyValueAllowed(property, context) {
|
|
|
61
61
|
if (property.value.type === 'Literal') {
|
|
62
62
|
return false;
|
|
63
63
|
} else if (property.value.type === 'Identifier') {
|
|
64
|
-
return
|
|
65
|
-
} else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier'
|
|
66
|
-
|
|
64
|
+
return isIdentifierAllowed(context, property.value);
|
|
65
|
+
} else if (property.value.type === 'CallExpression' && property.value.callee.type === 'Identifier') {
|
|
66
|
+
var variable = findVariable({
|
|
67
|
+
sourceCode: context.sourceCode,
|
|
68
|
+
identifier: property.value.callee
|
|
69
|
+
});
|
|
70
|
+
// Anything returned by getAllowedFunctionCalls should be used in css prop instead
|
|
71
|
+
var functionAllowList = getAllowedFunctionCalls(context.options);
|
|
72
|
+
if (!variable || isAllowListedVariable({
|
|
73
|
+
allowList: functionAllowList,
|
|
74
|
+
variable: variable
|
|
75
|
+
})) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
67
78
|
} else if (property.value.type === 'MemberExpression' && property.value.object.type === 'Identifier') {
|
|
68
79
|
var _findVariable2;
|
|
69
|
-
var
|
|
70
|
-
|
|
80
|
+
var definitions = (_findVariable2 = findVariable({
|
|
81
|
+
sourceCode: context.sourceCode,
|
|
82
|
+
identifier: property.value.object
|
|
83
|
+
})) === null || _findVariable2 === void 0 ? void 0 : _findVariable2.defs;
|
|
71
84
|
if (!definitions || !definitions.length) {
|
|
72
85
|
return false;
|
|
73
86
|
}
|
|
74
|
-
var
|
|
75
|
-
if (!
|
|
87
|
+
var _variable = definitions[0];
|
|
88
|
+
if (!_variable) {
|
|
76
89
|
return false;
|
|
77
90
|
}
|
|
78
|
-
var varType =
|
|
91
|
+
var varType = _variable.type;
|
|
79
92
|
if (varType === 'Variable') {
|
|
80
|
-
if (!
|
|
93
|
+
if (!_variable.node.init) {
|
|
81
94
|
return false;
|
|
82
95
|
}
|
|
83
|
-
var varSource =
|
|
96
|
+
var varSource = _variable.node.init.type;
|
|
84
97
|
// Variables initialised by a call expression or object expression are ok
|
|
85
98
|
if (varSource === 'CallExpression' || varSource === 'ObjectExpression') {
|
|
86
99
|
return true;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
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; } } }; }
|
|
2
|
+
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); }
|
|
3
|
+
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; }
|
|
4
|
+
import esquery from 'esquery';
|
|
2
5
|
import { createLintRule } from '../utils/create-rule';
|
|
3
|
-
import {
|
|
6
|
+
import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import';
|
|
4
7
|
var schema = [{
|
|
5
8
|
type: 'object',
|
|
6
9
|
properties: {
|
|
@@ -33,36 +36,45 @@ export var rule = createLintRule({
|
|
|
33
36
|
CallExpression: function CallExpression(node) {
|
|
34
37
|
var _context$getScope = context.getScope(),
|
|
35
38
|
references = _context$getScope.references;
|
|
36
|
-
if (
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
* This is needed to handle unknown node types. Otherwise an error is thrown.
|
|
60
|
-
*/
|
|
61
|
-
fallback: 'iteration'
|
|
62
|
-
});
|
|
39
|
+
if (!hasStyleObjectArguments(node.callee, references, importSources)) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
var matches = esquery(node, 'Property');
|
|
43
|
+
var _iterator = _createForOfIteratorHelper(matches),
|
|
44
|
+
_step;
|
|
45
|
+
try {
|
|
46
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
47
|
+
var match = _step.value;
|
|
48
|
+
if (match.type !== 'Property') {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (isImportant(match.value)) {
|
|
52
|
+
context.report({
|
|
53
|
+
node: match.value,
|
|
54
|
+
messageId: 'no-important-styles'
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} catch (err) {
|
|
59
|
+
_iterator.e(err);
|
|
60
|
+
} finally {
|
|
61
|
+
_iterator.f();
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
64
|
};
|
|
66
65
|
}
|
|
67
66
|
});
|
|
68
|
-
export default rule;
|
|
67
|
+
export default rule;
|
|
68
|
+
var importantRegex = /!\s*important\s*$/;
|
|
69
|
+
function isImportant(node) {
|
|
70
|
+
if (node.type === 'Literal') {
|
|
71
|
+
return typeof node.value === 'string' && importantRegex.test(node.value);
|
|
72
|
+
}
|
|
73
|
+
if (node.type === 'TemplateLiteral') {
|
|
74
|
+
var joinedRaw = node.quasis.map(function (element) {
|
|
75
|
+
return element.value.raw;
|
|
76
|
+
}).join('');
|
|
77
|
+
return importantRegex.test(joinedRaw);
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaskit/eslint-plugin-ui-styling-standard",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.5",
|
|
4
4
|
"author": "Atlassian Pty Ltd",
|
|
5
5
|
"description": "The eslint plugin to enforce and educate on Atlassian's UI Styling Standard",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -49,8 +49,9 @@
|
|
|
49
49
|
"outdent": "^0.5.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
+
"@af/formatting": "*",
|
|
52
53
|
"@atlaskit/ds-lib": "^2.3.0",
|
|
53
|
-
"@atlaskit/eslint-plugin-design-system": "^9.
|
|
54
|
+
"@atlaskit/eslint-plugin-design-system": "^9.7.0",
|
|
54
55
|
"@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
|
|
55
56
|
"@atlassian/codegen": "*",
|
|
56
57
|
"@compiled/eslint-plugin": "^0.13.6",
|
|
@@ -58,7 +59,6 @@
|
|
|
58
59
|
"@types/estraverse": "^5.1.7",
|
|
59
60
|
"eslint": "^8.49.0",
|
|
60
61
|
"lodash": "^4.17.21",
|
|
61
|
-
"prettier": "^2.8.0",
|
|
62
62
|
"ts-node": "^10.9.1",
|
|
63
63
|
"tsconfig-paths": "^4.2.0",
|
|
64
64
|
"typescript": "~5.4.2"
|
package/scripts/codegen.tsx
CHANGED
|
@@ -4,7 +4,7 @@ import { extname, join, relative } from 'path';
|
|
|
4
4
|
|
|
5
5
|
import camelCase from 'lodash/camelCase';
|
|
6
6
|
import outdent from 'outdent';
|
|
7
|
-
import
|
|
7
|
+
import format from '@af/formatting/sync';
|
|
8
8
|
|
|
9
9
|
import { createSignedArtifact } from '@atlassian/codegen';
|
|
10
10
|
|
|
@@ -155,19 +155,12 @@ async function generatePresetConfig(
|
|
|
155
155
|
* Write contents to a given file, creating a signed artifact if it's not a markdown file.
|
|
156
156
|
*/
|
|
157
157
|
async function writeFile(filepath: string, code: string) {
|
|
158
|
-
const config = await resolveConfig(filepath);
|
|
159
158
|
await fs.writeFile(
|
|
160
159
|
filepath,
|
|
161
160
|
extname(filepath).includes('.md')
|
|
162
|
-
? format(code,
|
|
163
|
-
...config,
|
|
164
|
-
filepath,
|
|
165
|
-
})
|
|
161
|
+
? format(code, 'markdown')
|
|
166
162
|
: createSignedArtifact(
|
|
167
|
-
format(code,
|
|
168
|
-
...config,
|
|
169
|
-
filepath,
|
|
170
|
-
}),
|
|
163
|
+
format(code, 'typescript'),
|
|
171
164
|
codegenCommand,
|
|
172
165
|
),
|
|
173
166
|
);
|
|
@@ -145,6 +145,24 @@ tester.run('enforce-style-prop', rule, {
|
|
|
145
145
|
}
|
|
146
146
|
`,
|
|
147
147
|
},
|
|
148
|
+
{
|
|
149
|
+
name: 'Ignore function calls',
|
|
150
|
+
code: `
|
|
151
|
+
import { calcDefaultLayoutWidthByAppearance } from '../shared';
|
|
152
|
+
const Component = () => {
|
|
153
|
+
return (
|
|
154
|
+
<div
|
|
155
|
+
style={{
|
|
156
|
+
width: calcDefaultLayoutWidthByAppearance(
|
|
157
|
+
rendererAppearance,
|
|
158
|
+
tableNode,
|
|
159
|
+
),
|
|
160
|
+
}}
|
|
161
|
+
/>
|
|
162
|
+
);
|
|
163
|
+
};
|
|
164
|
+
`,
|
|
165
|
+
},
|
|
148
166
|
],
|
|
149
167
|
invalid: [
|
|
150
168
|
{
|
|
@@ -1,22 +1,26 @@
|
|
|
1
|
-
import { Identifier, isNodeOfType, Node } from 'eslint-codemod-utils';
|
|
1
|
+
import { type Identifier, isNodeOfType, type Node } from 'eslint-codemod-utils';
|
|
2
2
|
import type { SpreadElement, Property } from 'estree-jsx';
|
|
3
3
|
|
|
4
4
|
import { createLintRule } from '../utils/create-rule';
|
|
5
5
|
import type { Rule, Scope } from 'eslint';
|
|
6
|
+
import {
|
|
7
|
+
getAllowedFunctionCalls,
|
|
8
|
+
isAllowListedVariable,
|
|
9
|
+
} from '@atlaskit/eslint-utils/allowed-function-calls';
|
|
10
|
+
import { findVariable } from '@atlaskit/eslint-utils/find-variable';
|
|
6
11
|
|
|
7
12
|
type ObjPropertyType = Property | SpreadElement;
|
|
8
13
|
type IdentifierWithParent = Scope.Reference['identifier'] &
|
|
9
14
|
Rule.NodeParentExtension;
|
|
10
15
|
|
|
11
|
-
const
|
|
16
|
+
const isParameter = (
|
|
12
17
|
context: Rule.RuleContext,
|
|
13
|
-
|
|
14
|
-
):
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const definitions = findVariable(context, variableName)?.defs;
|
|
18
|
+
identifier: Identifier,
|
|
19
|
+
): boolean => {
|
|
20
|
+
const definitions = findVariable({
|
|
21
|
+
sourceCode: context.sourceCode,
|
|
22
|
+
identifier: identifier,
|
|
23
|
+
})?.defs;
|
|
20
24
|
if (!definitions || !definitions.length) {
|
|
21
25
|
return false;
|
|
22
26
|
}
|
|
@@ -25,12 +29,14 @@ const isParameter = (context: Rule.RuleContext, variableName: string) => {
|
|
|
25
29
|
return result.type === 'Parameter';
|
|
26
30
|
};
|
|
27
31
|
|
|
28
|
-
const
|
|
32
|
+
const isIdentifierAllowed = (
|
|
29
33
|
context: Rule.RuleContext,
|
|
30
34
|
identifier: Identifier,
|
|
31
35
|
): boolean => {
|
|
32
|
-
const
|
|
33
|
-
|
|
36
|
+
const variable = findVariable({
|
|
37
|
+
sourceCode: context.sourceCode,
|
|
38
|
+
identifier: identifier,
|
|
39
|
+
});
|
|
34
40
|
|
|
35
41
|
if (!variable) {
|
|
36
42
|
return false;
|
|
@@ -59,16 +65,14 @@ const checkIdentifier = (
|
|
|
59
65
|
// Destructured from member expression
|
|
60
66
|
// e.g. const width = props.width
|
|
61
67
|
if (variableDefinition.init?.type === 'MemberExpression') {
|
|
62
|
-
|
|
63
|
-
return isParameter(context, memberIdentifierName);
|
|
68
|
+
return isParameter(context, variableDefinition.init.object);
|
|
64
69
|
}
|
|
65
70
|
// Destructured from object pattern
|
|
66
71
|
// e.g. const { width } = props
|
|
67
|
-
|
|
68
|
-
return isParameter(context, propName);
|
|
72
|
+
return isParameter(context, variableDefinition.init);
|
|
69
73
|
}
|
|
70
74
|
// If identifier has not been deconstructed from parameter or is a RestElement, report warning
|
|
71
|
-
return isParameter(context,
|
|
75
|
+
return isParameter(context, identifier);
|
|
72
76
|
};
|
|
73
77
|
|
|
74
78
|
function isPropertyValueAllowed(
|
|
@@ -82,19 +86,34 @@ function isPropertyValueAllowed(
|
|
|
82
86
|
if (property.value.type === 'Literal') {
|
|
83
87
|
return false;
|
|
84
88
|
} else if (property.value.type === 'Identifier') {
|
|
85
|
-
return
|
|
89
|
+
return isIdentifierAllowed(context, property.value);
|
|
86
90
|
} else if (
|
|
87
91
|
property.value.type === 'CallExpression' &&
|
|
88
|
-
property.value.callee.type === 'Identifier'
|
|
89
|
-
property.value.callee.name === 'token'
|
|
92
|
+
property.value.callee.type === 'Identifier'
|
|
90
93
|
) {
|
|
91
|
-
|
|
94
|
+
const variable = findVariable({
|
|
95
|
+
sourceCode: context.sourceCode,
|
|
96
|
+
identifier: property.value.callee,
|
|
97
|
+
});
|
|
98
|
+
// Anything returned by getAllowedFunctionCalls should be used in css prop instead
|
|
99
|
+
const functionAllowList = getAllowedFunctionCalls(context.options);
|
|
100
|
+
if (
|
|
101
|
+
!variable ||
|
|
102
|
+
isAllowListedVariable({
|
|
103
|
+
allowList: functionAllowList,
|
|
104
|
+
variable,
|
|
105
|
+
})
|
|
106
|
+
) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
92
109
|
} else if (
|
|
93
110
|
property.value.type === 'MemberExpression' &&
|
|
94
111
|
property.value.object.type === 'Identifier'
|
|
95
112
|
) {
|
|
96
|
-
const
|
|
97
|
-
|
|
113
|
+
const definitions = findVariable({
|
|
114
|
+
sourceCode: context.sourceCode,
|
|
115
|
+
identifier: property.value.object,
|
|
116
|
+
})?.defs;
|
|
98
117
|
if (!definitions || !definitions.length) {
|
|
99
118
|
return false;
|
|
100
119
|
}
|