-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
120 lines (108 loc) · 3.5 KB
/
index.js
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
import { tokenTypes as TYPE } from 'css-tree';
import * as SassVariable from './SassVariable.js';
import * as SassInterpolation from './SassInterpolation.js';
import * as SassNamespace from './SassNamespace.js';
const NUMBERSIGN = 0x0023; // U+0023 NUMBER SIGN (#)
const DOLLARSIGN = 0x0024; // U+0024 DOLLAR SIGN ($)
const PERCENTAGESIGN = 0x0025; // U+0025 PERCENTAGE SIGN (%)
const FULLSTOP = 0x002E; // U+002E FULL STOP (.)
// custom error
class PreprocessorExtensionError {
constructor() {
this.type = 'PreprocessorExtensionError';
}
}
export default function sass(syntaxConfig) {
// new node types
syntaxConfig.node.SassVariable = SassVariable;
syntaxConfig.node.SassInterpolation = SassInterpolation;
syntaxConfig.node.SassNamespace = SassNamespace;
// custom at-rules
syntaxConfig.atrules['at-root'] = {
prelude: '<any-value>'
};
syntaxConfig.atrules.content = {
prelude: '<any-value>'
};
syntaxConfig.atrules.debug = {
prelude: '<any-value>'
};
syntaxConfig.atrules.each = {
prelude: '<any-value>'
};
syntaxConfig.atrules.else = {
prelude: '<any-value>'
};
syntaxConfig.atrules.error = {
prelude: '<any-value>'
};
syntaxConfig.atrules.extend = {
prelude: '<any-value>'
};
syntaxConfig.atrules.for = {
prelude: '<any-value>'
};
syntaxConfig.atrules.forward = {
prelude: '<any-value>'
};
syntaxConfig.atrules.function = {
prelude: '<any-value>'
};
syntaxConfig.atrules.if = {
prelude: '<any-value>'
};
syntaxConfig.atrules.import = {
prelude: syntaxConfig.atrules.import.prelude + '| <string>#' // FIXME: fix prelude extension in css-tree
};
syntaxConfig.atrules.include = {
prelude: '<any-value>'
};
syntaxConfig.atrules.mixin = {
prelude: '<any-value>'
};
syntaxConfig.atrules.return = {
prelude: '<any-value>'
};
syntaxConfig.atrules.use = {
prelude: '<any-value>'
};
syntaxConfig.atrules.warn = {
prelude: '<any-value>'
};
syntaxConfig.atrules.while = {
prelude: '<any-value>'
};
// extend parser value parser
const originalGetNode = syntaxConfig.scope.Value.getNode;
syntaxConfig.scope.Value.getNode = function(context) {
let node = null;
switch (this.tokenType) {
case TYPE.Ident:
if (this.isDelim(FULLSTOP, 1)) {
node = this.SassNamespace();
}
break;
case TYPE.Delim:
switch (this.source.charCodeAt(this.tokenStart)) {
case DOLLARSIGN: // sass: $var
node = this.SassVariable();
break;
case NUMBERSIGN: // sass: #{ }
if (this.lookupType(1) === TYPE.LeftCurlyBracket) {
node = this.SassInterpolation(this.scope.Value, this.readSequence);
}
break;
case PERCENTAGESIGN: // sass: 5 % 4
node = this.Operator();
break;
}
break;
}
// currently we can't validate values that contain less/sass extensions
if (node !== null) {
throw new PreprocessorExtensionError();
}
return originalGetNode.call(this, context);
};
return syntaxConfig;
};