forked from popcodeorg/popcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprettycss.js
More file actions
157 lines (132 loc) · 3.64 KB
/
prettycss.js
File metadata and controls
157 lines (132 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import trim from 'lodash/trim';
import endsWith from 'lodash/endsWith';
import Validator from '../Validator';
import importLinters from '../importLinters';
const RADIAL_GRADIENT_EXPR =
/^(?:(?:-(?:ms|moz|o|webkit)-)?radial-gradient|-webkit-gradient)/;
const FILTER_VALUE_EXPR =
new RegExp([
'^blur\\(',
'^brightness\\(',
'^contrast\\(',
'^drop-shadow\\(',
'^grayscale\\(',
'^hue-rotate\\(',
'^invert\\(',
'^opacity\\(',
'^saturate\\(',
'^sepia\\(',
'^inherit$',
].join('|'));
function isIncorrectlyRejectedValue(value) {
return isIncorrectlyRejectedRadialGradientValue(value) ||
isIncorrectlyRejectedFilterValue(value);
}
function isIncorrectlyRejectedRadialGradientValue(value) {
return RADIAL_GRADIENT_EXPR.test(value);
}
function isIncorrectlyRejectedFilterValue(value) {
return FILTER_VALUE_EXPR.test(value);
}
const errorMap = {
'block-expected': (error) => {
const tokenType = error.token.type;
const token = error.token.content;
if (tokenType === 'IDENT' || tokenType === 'S') {
return {
reason: 'block-expected',
payload: {error: token},
suppresses: ['missing-opening-curly'],
};
}
return {
reason: 'invalid-token-in-selector',
payload: {token},
};
},
'extra-tokens-after-value': (error, source) => {
const errorToken = error.token;
const lineNumber = errorToken.line;
if (lineNumber > 1) {
const lines = source.split('\n');
const previousLine = lines[lineNumber - 2];
const thisLine = lines[lineNumber - 1];
if (
errorToken.charNum - 1 === /\S/.exec(thisLine).index &&
!endsWith(trim(previousLine), ';')
) {
return {
reason: 'missing-semicolon',
row: lineNumber - 2,
column: previousLine.length - 1,
};
}
}
return ({
reason: 'extra-tokens-after-value',
payload: {token: errorToken.content},
});
},
'illegal-token-after-combinator': () => ({
reason: 'illegal-token-after-combinator',
suppresses: ['block-expected'],
}),
'invalid-token': () => ({
reason: 'invalid-token',
suppresses: [
'illegal-token-after-combinator',
'invalid-token-in-selector',
'missing-opening-curly',
],
}),
'invalid-value': (error) => {
if (isIncorrectlyRejectedValue(error.token.content)) {
return null;
}
return {
reason: 'invalid-value',
payload: {error: error.token.content},
};
},
'require-value': error => ({
reason: 'require-value',
payload: {error: error.token.content},
}),
'require-positive-value': error => ({
reason: 'invalid-negative-value',
payload: {error: error.token.content},
}),
'require-integer': error => ({
reason: 'invalid-fractional-value',
payload: {error: error.token.content},
}),
'selector-expected': () => ({reason: 'selector-expected'}),
'unknown-property': error => ({
reason: 'unknown-property',
payload: {error: error.token.content},
}),
};
class PrettyCssValidator extends Validator {
constructor(source) {
super(source, 'css', errorMap);
}
async _getRawErrors() {
const {prettyCSS} = await importLinters();
try {
const result = prettyCSS.parse(this._source);
return result.getProblems();
} catch (_e) {
return [];
}
}
_keyForError(error) {
return error.code.split(':')[0];
}
_locationForError(error) {
if (!error.token) {
return {row: 0, column: 0};
}
return {row: error.token.line - 1, column: error.token.charNum - 1};
}
}
export default source => new PrettyCssValidator(source).getAnnotations();