@atlaskit/eslint-plugin-ui-styling-standard 2.1.2 → 2.1.4
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 +16 -0
- package/README.md +18 -18
- package/dist/cjs/rules/no-important-styles/index.js +79 -99
- package/dist/cjs/rules/no-nested-selectors/index.js +48 -36
- package/dist/cjs/rules/no-unsafe-selectors/index.js +88 -87
- package/dist/cjs/rules/no-unsafe-selectors/lint-selector.js +140 -101
- package/dist/cjs/rules/no-unsafe-selectors/walk-css-map.js +20 -50
- package/dist/cjs/rules/utils/create-rule-with-typed-config.js +29 -10
- package/dist/cjs/rules/utils/is-simple-selector.js +70 -0
- package/dist/cjs/rules/utils/parse-selector.js +30 -0
- package/dist/cjs/rules/utils/style-calls.js +127 -0
- package/dist/cjs/rules/utils/walk-style-call-properties.js +50 -0
- package/dist/es2019/rules/no-important-styles/index.js +79 -70
- package/dist/es2019/rules/no-nested-selectors/index.js +38 -36
- package/dist/es2019/rules/no-unsafe-selectors/index.js +68 -89
- package/dist/es2019/rules/no-unsafe-selectors/lint-selector.js +29 -23
- package/dist/es2019/rules/no-unsafe-selectors/walk-css-map.js +4 -30
- package/dist/es2019/rules/utils/create-rule-with-typed-config.js +30 -10
- package/dist/es2019/rules/utils/is-simple-selector.js +63 -0
- package/dist/es2019/rules/utils/parse-selector.js +23 -0
- package/dist/es2019/rules/utils/style-calls.js +89 -0
- package/dist/es2019/rules/utils/walk-style-call-properties.js +23 -0
- package/dist/esm/rules/no-important-styles/index.js +80 -100
- package/dist/esm/rules/no-nested-selectors/index.js +48 -34
- package/dist/esm/rules/no-unsafe-selectors/index.js +89 -87
- package/dist/esm/rules/no-unsafe-selectors/lint-selector.js +141 -102
- package/dist/esm/rules/no-unsafe-selectors/walk-css-map.js +19 -49
- package/dist/esm/rules/utils/create-rule-with-typed-config.js +30 -10
- package/dist/esm/rules/utils/is-simple-selector.js +64 -0
- package/dist/esm/rules/utils/parse-selector.js +23 -0
- package/dist/esm/rules/utils/style-calls.js +120 -0
- package/dist/esm/rules/utils/walk-style-call-properties.js +44 -0
- package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -1
- package/dist/types/rules/no-unsafe-selectors/walk-css-map.d.ts +1 -7
- package/dist/types/rules/utils/create-rule-with-typed-config.d.ts +1 -1
- package/dist/types/rules/utils/is-simple-selector.d.ts +11 -0
- package/dist/types/rules/utils/parse-selector.d.ts +9 -0
- package/dist/types/rules/utils/style-calls.d.ts +17 -0
- package/dist/types/rules/utils/walk-style-call-properties.d.ts +5 -0
- package/package.json +3 -3
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import cssSelectorParser from 'postcss-selector-parser';
|
|
2
|
-
import { getScope } from '@atlaskit/eslint-utils/context-compat';
|
|
3
|
-
import { isXcss } from '@atlaskit/eslint-utils/is-supported-import';
|
|
4
1
|
import { importSources } from '@atlaskit/eslint-utils/schema';
|
|
5
|
-
import { walkStyleProperties } from '@atlaskit/eslint-utils/walk-style-properties';
|
|
6
2
|
import { createLintRuleWithTypedConfig } from '../utils/create-rule-with-typed-config';
|
|
3
|
+
import { isSimpleSelector } from '../utils/is-simple-selector';
|
|
4
|
+
import { parseSelector } from '../utils/parse-selector';
|
|
5
|
+
import { getStyleCalls } from '../utils/style-calls';
|
|
6
|
+
import { walkStyleCallProperties } from '../utils/walk-style-call-properties';
|
|
7
7
|
import { lintSelector } from './lint-selector';
|
|
8
|
-
import {
|
|
8
|
+
import { walkCssMapCall } from './walk-css-map';
|
|
9
|
+
import { allowedPseudos } from './constants';
|
|
9
10
|
const ignoredAtRules = new Set(['@container',
|
|
10
11
|
// ignored because it's covered by `no-container-queries`
|
|
11
12
|
'@media',
|
|
12
13
|
// ignored because it's covered by `no-nested-styles`
|
|
13
14
|
'@supports', '@property', '@starting-style']);
|
|
14
|
-
const cssSelectorProcessor = cssSelectorParser();
|
|
15
15
|
const rule = createLintRuleWithTypedConfig({
|
|
16
16
|
meta: {
|
|
17
17
|
name: 'no-unsafe-selectors',
|
|
@@ -57,95 +57,74 @@ const rule = createLintRuleWithTypedConfig({
|
|
|
57
57
|
},
|
|
58
58
|
create(context, config) {
|
|
59
59
|
return {
|
|
60
|
-
Program(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
60
|
+
Program() {
|
|
61
|
+
for (const styleCall of getStyleCalls(context)) {
|
|
62
|
+
if (!config.importSources.includes(styleCall.importSource)) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (styleCall.styleFunction === 'cssMap') {
|
|
66
|
+
walkCssMapCall(styleCall.node, property => {
|
|
67
|
+
const {
|
|
68
|
+
type,
|
|
69
|
+
node
|
|
70
|
+
} = property;
|
|
71
|
+
if (type === 'grouped-at-rules') {
|
|
72
|
+
context.report({
|
|
73
|
+
node: node.key,
|
|
74
|
+
messageId: 'no-grouped-at-rules'
|
|
75
|
+
});
|
|
76
|
+
} else if (type === 'selectors') {
|
|
77
|
+
context.report({
|
|
78
|
+
node: node.key,
|
|
79
|
+
messageId: 'no-selectors-object'
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
walkStyleCallProperties(styleCall, ({
|
|
85
|
+
key,
|
|
86
|
+
value
|
|
87
|
+
}) => {
|
|
88
|
+
if (value.type !== 'ObjectExpression' || key.type !== 'Literal' || typeof key.value !== 'string') {
|
|
75
89
|
return;
|
|
76
90
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
91
|
+
const selectorText = key.value;
|
|
92
|
+
if (selectorText.includes('@')) {
|
|
93
|
+
lintAtRule({
|
|
94
|
+
context,
|
|
95
|
+
sourceNode: key,
|
|
96
|
+
atRule: selectorText
|
|
81
97
|
});
|
|
82
98
|
return;
|
|
83
99
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
*/
|
|
101
|
-
if (value.type !== 'ObjectExpression') {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Technically the key could be an identifier too, e.g.
|
|
107
|
-
*
|
|
108
|
-
* ```ts
|
|
109
|
-
* css({
|
|
110
|
-
* // This key is an identifier, not a literal
|
|
111
|
-
* selector: {}
|
|
112
|
-
* });
|
|
113
|
-
* ```
|
|
114
|
-
*
|
|
115
|
-
* But an identifier couldn't violate anything in this rule,
|
|
116
|
-
* because it can't contain '@' or ':' characters.
|
|
117
|
-
*/
|
|
118
|
-
if (key.type !== 'Literal' || typeof key.value !== 'string') {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
const selectorText = key.value;
|
|
122
|
-
if (selectorText.includes('@')) {
|
|
123
|
-
lintAtRule({
|
|
124
|
-
context,
|
|
125
|
-
sourceNode: key,
|
|
126
|
-
atRule: selectorText
|
|
127
|
-
});
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
try {
|
|
131
|
-
const selectorList = cssSelectorProcessor.astSync(selectorText);
|
|
132
|
-
selectorList.nodes.forEach(selector => lintSelector({
|
|
133
|
-
context,
|
|
134
|
-
sourceNode: key,
|
|
135
|
-
selector,
|
|
136
|
-
config,
|
|
137
|
-
isXcssCall
|
|
138
|
-
}));
|
|
139
|
-
} catch {
|
|
140
|
-
context.report({
|
|
141
|
-
node: key,
|
|
142
|
-
messageId: 'no-unparsable-selectors',
|
|
143
|
-
data: {
|
|
144
|
-
selectorText
|
|
100
|
+
if (isSimpleSelector(selectorText, {
|
|
101
|
+
allowedPseudos,
|
|
102
|
+
allowLeadingPseudo: styleCall.styleFunction === 'xcss'
|
|
103
|
+
})) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
const selectorList = parseSelector(context, selectorText);
|
|
108
|
+
for (const selector of selectorList.nodes) {
|
|
109
|
+
lintSelector({
|
|
110
|
+
context,
|
|
111
|
+
sourceNode: key,
|
|
112
|
+
selector,
|
|
113
|
+
config,
|
|
114
|
+
isXcssCall: styleCall.styleFunction === 'xcss'
|
|
115
|
+
});
|
|
145
116
|
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
117
|
+
} catch {
|
|
118
|
+
context.report({
|
|
119
|
+
node: key,
|
|
120
|
+
messageId: 'no-unparsable-selectors',
|
|
121
|
+
data: {
|
|
122
|
+
selectorText
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
149
128
|
}
|
|
150
129
|
};
|
|
151
130
|
}
|
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
import { isNesting } from 'postcss-selector-parser';
|
|
1
|
+
import { isNesting, isPseudo } from 'postcss-selector-parser';
|
|
2
2
|
import { allowedPseudos } from './constants';
|
|
3
3
|
import { getSourceLocationFromRange } from './get-source-location-from-range';
|
|
4
4
|
import { getRangeFromNode } from './source-position-utils';
|
|
5
5
|
export function lintSelector(args) {
|
|
6
6
|
checkNoAmbiguousPseudos(args);
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const pseudos = [];
|
|
8
|
+
const nestings = [];
|
|
9
|
+
args.selector.walk(node => {
|
|
10
|
+
if (isPseudo(node)) {
|
|
11
|
+
pseudos.push(node);
|
|
12
|
+
} else if (isNesting(node)) {
|
|
13
|
+
nestings.push(node);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
checkNoRestrictedPseudos(args, pseudos);
|
|
17
|
+
checkNoLegacyPseudoElementSyntax(args, pseudos);
|
|
18
|
+
checkNoIncreasedSpecificity(args, nestings);
|
|
10
19
|
}
|
|
11
20
|
const legacyPseudoElements = new Set([':after', ':before', ':first-letter', ':first-line']);
|
|
12
21
|
|
|
@@ -59,12 +68,11 @@ function checkNoAmbiguousPseudos({
|
|
|
59
68
|
}
|
|
60
69
|
function checkNoRestrictedPseudos({
|
|
61
70
|
context,
|
|
62
|
-
sourceNode
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
selector.walkPseudos(pseudo => {
|
|
71
|
+
sourceNode
|
|
72
|
+
}, pseudos) {
|
|
73
|
+
for (const pseudo of pseudos) {
|
|
66
74
|
if (allowedPseudos.has(pseudo.value)) {
|
|
67
|
-
|
|
75
|
+
continue;
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
/**
|
|
@@ -74,7 +82,7 @@ function checkNoRestrictedPseudos({
|
|
|
74
82
|
* This will be caught by the `no-legacy-pseudo-element-syntax` check.
|
|
75
83
|
*/
|
|
76
84
|
if (legacyPseudoElements.has(pseudo.value) && allowedPseudos.has(`:${pseudo.value}`)) {
|
|
77
|
-
|
|
85
|
+
continue;
|
|
78
86
|
}
|
|
79
87
|
if (!allowedPseudos.has(pseudo.value)) {
|
|
80
88
|
const range = getRangeFromNode({
|
|
@@ -90,7 +98,7 @@ function checkNoRestrictedPseudos({
|
|
|
90
98
|
}
|
|
91
99
|
});
|
|
92
100
|
}
|
|
93
|
-
}
|
|
101
|
+
}
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
/**
|
|
@@ -100,10 +108,9 @@ function checkNoRestrictedPseudos({
|
|
|
100
108
|
*/
|
|
101
109
|
function checkNoLegacyPseudoElementSyntax({
|
|
102
110
|
context,
|
|
103
|
-
sourceNode
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
selector.walkPseudos(pseudo => {
|
|
111
|
+
sourceNode
|
|
112
|
+
}, pseudos) {
|
|
113
|
+
for (const pseudo of pseudos) {
|
|
107
114
|
if (legacyPseudoElements.has(pseudo.value)) {
|
|
108
115
|
const range = getRangeFromNode({
|
|
109
116
|
node: pseudo,
|
|
@@ -118,19 +125,18 @@ function checkNoLegacyPseudoElementSyntax({
|
|
|
118
125
|
}
|
|
119
126
|
});
|
|
120
127
|
}
|
|
121
|
-
}
|
|
128
|
+
}
|
|
122
129
|
}
|
|
123
130
|
function checkNoIncreasedSpecificity({
|
|
124
131
|
context,
|
|
125
|
-
sourceNode
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
selector.walkNesting(nesting => {
|
|
132
|
+
sourceNode
|
|
133
|
+
}, nestings) {
|
|
134
|
+
for (const nesting of nestings) {
|
|
129
135
|
/**
|
|
130
136
|
* If it's not the start of a chain, then it's already been reported.
|
|
131
137
|
*/
|
|
132
138
|
if (isNesting(nesting.prev())) {
|
|
133
|
-
|
|
139
|
+
continue;
|
|
134
140
|
}
|
|
135
141
|
let next = nesting.next();
|
|
136
142
|
|
|
@@ -139,7 +145,7 @@ function checkNoIncreasedSpecificity({
|
|
|
139
145
|
* then it isn't just there to increase specificity — it is actually necessary.
|
|
140
146
|
*/
|
|
141
147
|
if (next && !isNesting(next)) {
|
|
142
|
-
|
|
148
|
+
continue;
|
|
143
149
|
}
|
|
144
150
|
let lastNesting = nesting;
|
|
145
151
|
while (isNesting(next)) {
|
|
@@ -159,5 +165,5 @@ function checkNoIncreasedSpecificity({
|
|
|
159
165
|
loc,
|
|
160
166
|
messageId: 'no-increased-specificity'
|
|
161
167
|
});
|
|
162
|
-
}
|
|
168
|
+
}
|
|
163
169
|
}
|
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
* This is because the API is likely to change and should not be considered stable.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
|
|
8
|
-
|
|
9
7
|
/**
|
|
10
8
|
* At-rules that are allowed to be used in "grouped" form within cssMap.
|
|
11
9
|
*
|
|
@@ -52,38 +50,14 @@ import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
|
|
|
52
50
|
*/
|
|
53
51
|
const allowedGroupedAtRules = new Set(['@starting-style' // Has no parameters, can only be written in grouped form
|
|
54
52
|
]);
|
|
55
|
-
export function
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
visitor
|
|
59
|
-
}) {
|
|
60
|
-
const program = getSourceCode(context).ast;
|
|
61
|
-
const importDeclaration = program.body.find(node => node.type === 'ImportDeclaration' && importSources.includes(node.source.value));
|
|
62
|
-
const specifier = importDeclaration === null || importDeclaration === void 0 ? void 0 : importDeclaration.specifiers.find(specifier => specifier.type === 'ImportSpecifier' && 'name' in specifier.imported && specifier.imported.name === 'cssMap');
|
|
63
|
-
if (!specifier) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
const [variable] = getSourceCode(context).scopeManager.getDeclaredVariables(specifier);
|
|
67
|
-
if (!variable) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
variable.references.forEach(reference => {
|
|
71
|
-
const identifier = reference.identifier;
|
|
72
|
-
const {
|
|
73
|
-
parent
|
|
74
|
-
} = identifier;
|
|
75
|
-
if (parent.type !== 'CallExpression') {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
const variantMap = parent.arguments[0];
|
|
79
|
-
if (variantMap.type !== 'ObjectExpression') {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
53
|
+
export function walkCssMapCall(call, visitor) {
|
|
54
|
+
const variantMap = call.arguments[0];
|
|
55
|
+
if ((variantMap === null || variantMap === void 0 ? void 0 : variantMap.type) === 'ObjectExpression') {
|
|
82
56
|
walkCssMapVariantMap({
|
|
83
57
|
variantMap,
|
|
84
58
|
visitor
|
|
85
59
|
});
|
|
86
|
-
}
|
|
60
|
+
}
|
|
87
61
|
}
|
|
88
62
|
function walkCssMapVariantMap({
|
|
89
63
|
variantMap,
|
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
import Ajv from 'ajv';
|
|
2
2
|
import { getRuleUrl } from './get-rule-url';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A single shared Ajv instance is reused across every rule instead of constructing
|
|
6
|
+
* a new one each time a default config is needed. Ajv's constructor is expensive
|
|
7
|
+
* (it compiles its own meta-schemas), so we only ever want to pay for it once.
|
|
8
|
+
*/
|
|
9
|
+
const ajv = new Ajv({
|
|
10
|
+
useDefaults: true
|
|
11
|
+
});
|
|
3
12
|
export const createLintRuleWithTypedConfig = rule => {
|
|
13
|
+
/**
|
|
14
|
+
* Lazily-computed, cached default config.
|
|
15
|
+
*
|
|
16
|
+
* The default config is derived purely from the rule's (static) schema, so it is
|
|
17
|
+
* constant for the life of the process. Compiling the schema with Ajv involves
|
|
18
|
+
* runtime code generation (`new Function`), which is expensive — previously this
|
|
19
|
+
* ran on every `getDefaultConfig()` call, i.e. once per linted file. We now compute
|
|
20
|
+
* it at most once per rule and reuse the result.
|
|
21
|
+
*/
|
|
22
|
+
let defaultConfig;
|
|
23
|
+
|
|
4
24
|
/**
|
|
5
25
|
* Gets the default config by running the same validator
|
|
6
26
|
* ESLint does against an empty config object with
|
|
@@ -10,16 +30,16 @@ export const createLintRuleWithTypedConfig = rule => {
|
|
|
10
30
|
* will be populated with the specified default value.
|
|
11
31
|
*/
|
|
12
32
|
function getDefaultConfig() {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return
|
|
33
|
+
if (defaultConfig === undefined) {
|
|
34
|
+
const validate = ajv.compile(rule.meta.schema);
|
|
35
|
+
const config = {};
|
|
36
|
+
/**
|
|
37
|
+
* The `validate()` function mutates the config object.
|
|
38
|
+
*/
|
|
39
|
+
validate(config);
|
|
40
|
+
defaultConfig = config;
|
|
41
|
+
}
|
|
42
|
+
return defaultConfig;
|
|
23
43
|
}
|
|
24
44
|
return {
|
|
25
45
|
...rule,
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recognizes simple, parseable selector lists made only from nesting and non-functional pseudos.
|
|
3
|
+
* Returning false means the caller must use the full selector parser.
|
|
4
|
+
*/
|
|
5
|
+
export function isSimpleSelector(selectorText, options = {}) {
|
|
6
|
+
let index = 0;
|
|
7
|
+
while (index < selectorText.length) {
|
|
8
|
+
index = skipWhitespace(selectorText, index);
|
|
9
|
+
const hasNesting = selectorText.charCodeAt(index) === 38;
|
|
10
|
+
if (hasNesting) {
|
|
11
|
+
index++;
|
|
12
|
+
if (selectorText.charCodeAt(index) === 38) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
} else if (!options.allowLeadingPseudo || selectorText.charCodeAt(index) !== 58) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
let pseudoCount = 0;
|
|
19
|
+
while (selectorText.charCodeAt(index) === 58) {
|
|
20
|
+
const pseudoStart = index++;
|
|
21
|
+
if (selectorText.charCodeAt(index) === 58) {
|
|
22
|
+
index++;
|
|
23
|
+
}
|
|
24
|
+
const nameStart = index;
|
|
25
|
+
while (index < selectorText.length && isPseudoNameCharacter(selectorText.charCodeAt(index))) {
|
|
26
|
+
index++;
|
|
27
|
+
}
|
|
28
|
+
if (index === nameStart || selectorText.charCodeAt(index) === 40) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
if (options.allowedPseudos && !options.allowedPseudos.has(selectorText.slice(pseudoStart, index))) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
pseudoCount++;
|
|
35
|
+
}
|
|
36
|
+
if (pseudoCount === 0 && (!hasNesting || !options.allowBareNesting)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
index = skipWhitespace(selectorText, index);
|
|
40
|
+
if (index === selectorText.length) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
if (selectorText.charCodeAt(index) !== 44) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
index++;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
function skipWhitespace(value, start) {
|
|
51
|
+
let index = start;
|
|
52
|
+
while (index < value.length) {
|
|
53
|
+
const character = value.charCodeAt(index);
|
|
54
|
+
if (character !== 32 && (character < 9 || character > 13)) {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
index++;
|
|
58
|
+
}
|
|
59
|
+
return index;
|
|
60
|
+
}
|
|
61
|
+
function isPseudoNameCharacter(character) {
|
|
62
|
+
return character === 45 || character === 95 || character >= 48 && character <= 57 || character >= 65 && character <= 90 || character >= 97 && character <= 122;
|
|
63
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import cssSelectorParser from 'postcss-selector-parser';
|
|
2
|
+
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
|
|
3
|
+
const cssSelectorProcessor = cssSelectorParser();
|
|
4
|
+
const selectorCache = new WeakMap();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parse each selector once per linted source file. Consumers treat the returned AST as read-only.
|
|
8
|
+
*/
|
|
9
|
+
export function parseSelector(context, selectorText) {
|
|
10
|
+
const sourceCode = getSourceCode(context);
|
|
11
|
+
let sourceFileCache = selectorCache.get(sourceCode);
|
|
12
|
+
if (!sourceFileCache) {
|
|
13
|
+
sourceFileCache = new Map();
|
|
14
|
+
selectorCache.set(sourceCode, sourceFileCache);
|
|
15
|
+
}
|
|
16
|
+
const cachedSelector = sourceFileCache.get(selectorText);
|
|
17
|
+
if (cachedSelector) {
|
|
18
|
+
return cachedSelector;
|
|
19
|
+
}
|
|
20
|
+
const selector = cssSelectorProcessor.astSync(selectorText);
|
|
21
|
+
sourceFileCache.set(selectorText, selector);
|
|
22
|
+
return selector;
|
|
23
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { getSourceCode } from '@atlaskit/eslint-utils/context-compat';
|
|
2
|
+
const styleFunctionNames = new Set(['css', 'cssMap', 'keyframes', 'styled', 'xcss']);
|
|
3
|
+
const defaultStyledImportSources = new Set(['@emotion/styled', 'styled' + '-components']);
|
|
4
|
+
const styleCallsBySourceCode = new WeakMap();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Visit imported style calls without resolving every CallExpression against its current scope.
|
|
8
|
+
*
|
|
9
|
+
* ESLint has already linked import bindings to all of their references. Building this index once
|
|
10
|
+
* per source file makes each styling rule proportional to the number of style calls instead of the
|
|
11
|
+
* number of all calls in the file.
|
|
12
|
+
*/
|
|
13
|
+
export function getStyleCalls(context) {
|
|
14
|
+
const sourceCode = getSourceCode(context);
|
|
15
|
+
let styleCalls = styleCallsBySourceCode.get(sourceCode);
|
|
16
|
+
if (!styleCalls) {
|
|
17
|
+
styleCalls = buildStyleCallIndex(sourceCode);
|
|
18
|
+
styleCallsBySourceCode.set(sourceCode, styleCalls);
|
|
19
|
+
}
|
|
20
|
+
return styleCalls;
|
|
21
|
+
}
|
|
22
|
+
function buildStyleCallIndex(sourceCode) {
|
|
23
|
+
const styleCalls = [];
|
|
24
|
+
const seenCalls = new Set();
|
|
25
|
+
const {
|
|
26
|
+
scopeManager
|
|
27
|
+
} = sourceCode;
|
|
28
|
+
if (!scopeManager) {
|
|
29
|
+
return styleCalls;
|
|
30
|
+
}
|
|
31
|
+
for (const statement of sourceCode.ast.body) {
|
|
32
|
+
if (statement.type !== 'ImportDeclaration' || typeof statement.source.value !== 'string') {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const importSource = statement.source.value;
|
|
36
|
+
for (const specifier of statement.specifiers) {
|
|
37
|
+
const styleFunction = getStyleFunctionFromSpecifier(specifier, importSource);
|
|
38
|
+
if (!styleFunction) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const [variable] = scopeManager.getDeclaredVariables(specifier);
|
|
42
|
+
if (!variable) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
for (const reference of variable.references) {
|
|
46
|
+
const call = getStyleCall(reference.identifier, styleFunction);
|
|
47
|
+
if (!call || seenCalls.has(call)) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
seenCalls.add(call);
|
|
51
|
+
styleCalls.push({
|
|
52
|
+
importSource,
|
|
53
|
+
node: call,
|
|
54
|
+
styleFunction
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return styleCalls;
|
|
60
|
+
}
|
|
61
|
+
function getStyleFunctionFromSpecifier(specifier, importSource) {
|
|
62
|
+
if (specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && styleFunctionNames.has(specifier.imported.name)) {
|
|
63
|
+
return specifier.imported.name;
|
|
64
|
+
}
|
|
65
|
+
if (specifier.type !== 'ImportDefaultSpecifier') {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
if (styleFunctionNames.has(specifier.local.name)) {
|
|
69
|
+
return specifier.local.name;
|
|
70
|
+
}
|
|
71
|
+
return defaultStyledImportSources.has(importSource) ? 'styled' : null;
|
|
72
|
+
}
|
|
73
|
+
function getStyleCall(identifier, styleFunction) {
|
|
74
|
+
var _parent2, _parent$parent;
|
|
75
|
+
let callee = identifier;
|
|
76
|
+
let parent = callee.parent;
|
|
77
|
+
while (((_parent = parent) === null || _parent === void 0 ? void 0 : _parent.type) === 'MemberExpression' && parent.object === callee) {
|
|
78
|
+
var _parent;
|
|
79
|
+
callee = parent;
|
|
80
|
+
parent = callee.parent;
|
|
81
|
+
}
|
|
82
|
+
if (((_parent2 = parent) === null || _parent2 === void 0 ? void 0 : _parent2.type) !== 'CallExpression' || parent.callee !== callee) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (styleFunction === 'styled' && ((_parent$parent = parent.parent) === null || _parent$parent === void 0 ? void 0 : _parent$parent.type) === 'CallExpression' && parent.parent.callee === parent) {
|
|
86
|
+
return parent.parent;
|
|
87
|
+
}
|
|
88
|
+
return parent;
|
|
89
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function walkStyleCallProperties(styleCall, visitor, options) {
|
|
2
|
+
const skipTopLevel = !(options !== null && options !== void 0 && options.includeTopLevel) && (styleCall.styleFunction === 'cssMap' || styleCall.styleFunction === 'keyframes');
|
|
3
|
+
for (const argument of styleCall.node.arguments) {
|
|
4
|
+
if (argument.type === 'ObjectExpression') {
|
|
5
|
+
walkStyleObject(argument, visitor, skipTopLevel);
|
|
6
|
+
} else if (argument.type === 'ArrowFunctionExpression' && argument.expression && argument.body.type === 'ObjectExpression') {
|
|
7
|
+
walkStyleObject(argument.body, visitor, skipTopLevel);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function walkStyleObject(object, visitor, skipVisitor) {
|
|
12
|
+
for (const property of object.properties) {
|
|
13
|
+
if (property.type !== 'Property') {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (!skipVisitor) {
|
|
17
|
+
visitor(property);
|
|
18
|
+
}
|
|
19
|
+
if (property.value.type === 'ObjectExpression') {
|
|
20
|
+
walkStyleObject(property.value, visitor, false);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|