-
Notifications
You must be signed in to change notification settings - Fork 39
/
parse.js
172 lines (139 loc) · 4.67 KB
/
parse.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import createQueryWrapper from 'query-ast';
import gonzales from 'gonzales-pe';
import { stringify } from './stringify';
const SCOPE_IMPICIT = 'implicit';
const SCOPE_EXPLICIT = 'explicit';
const DEP_KEYWORDS = {
mixin: 'mixin',
function: 'function',
};
const DEP_HOST = {
mixin: depParentNode => depParentNode,
function: depParentNode => depParentNode.children('function'),
};
/**
* Check if a declaration node has a '!' operator followed by a '<flag>' identifier in its value
*/
function declarationHasFlag($ast, node, flag) {
return $ast(node)
.children('value').children(flag)
.length() > 0
}
/**
* Check if a declaration node has a '!' operator followed by the 'default' identifier in its value
*/
function isExplicitGlobalDeclaration($ast, node) {
return declarationHasFlag($ast, node, 'global');
}
/**
* Check if a declaration node has a '!' operator followed by the 'global' identifier in its value
*/
function isDefaultDeclaration($ast, node) {
return declarationHasFlag($ast, node, 'default');
}
/**
* Parse the raw expression of a variable declaration excluding flags
*/
function parseExpression($ast, declaration) {
return stringify($ast(declaration)
.children('value')
.get(0))
.trim();
}
/**
* Get dependencies required to extract variables such as mixins or function invocations
*/
function getDeclarationDeps($ast, declaration, scope) {
if(scope !== SCOPE_EXPLICIT) {
return {};
}
const depParentNodes = $ast(declaration).parents(node => {
if(node.node.type === 'mixin') {
return true;
} else if(node.node.type === 'atrule') {
const atruleIdentNode = $ast(node).children('atkeyword').children('ident');
return atruleIdentNode.length() > 0 && atruleIdentNode.first().value() === 'function'
} else {
return false;
}
});
if(depParentNodes.length() === 0) {
return {};
}
const depParentNode = depParentNodes.last();
const depKeywordNode = depParentNode.children('atkeyword').children('ident');
if(depKeywordNode.length() === 0) {
return {};
}
const atKeyword = depKeywordNode.first().value();
if(!DEP_HOST[atKeyword]) {
return {};
}
const depHostNode = DEP_HOST[atKeyword](depParentNode);
const atKeywordIdentifierNode = depHostNode.children('ident');
if(atKeywordIdentifierNode.length() === 0) {
return {};
}
const atIdentifier = atKeywordIdentifierNode.first().value();
const argumentsNode = depHostNode.children('arguments');
// Count arguments to mixin/function @atrule
const requiredArgsCount = argumentsNode.children('variable').length();
const optionalArgsCount = argumentsNode.children('declaration').length();
const totalArgsCount = requiredArgsCount + optionalArgsCount;
if(!DEP_KEYWORDS[atKeyword]) {
return {};
}
return {
[DEP_KEYWORDS[atKeyword]]: {
name: atIdentifier,
argsCount: {
total: totalArgsCount,
required: requiredArgsCount,
optional: optionalArgsCount,
},
},
};
}
/**
* Parse declaration node into declaration object
*/
function parseDeclaration($ast, declaration, scope) {
const variable = {};
const propertyNode = $ast(declaration)
.children('property');
variable.declarationClean = propertyNode.value();
variable.position = propertyNode.get(0).start;
variable.declaration = `$${variable.declarationClean}`;
variable.expression = parseExpression($ast, declaration);
variable.flags = {
default: isDefaultDeclaration($ast, declaration),
global: isExplicitGlobalDeclaration($ast, declaration),
};
variable.deps = getDeclarationDeps($ast, declaration, scope);
return variable;
}
/**
* Parse variable declarations from a chunk of sass source
*/
export function parseDeclarations(data) {
const ast = JSON.parse(gonzales.parse(data, {syntax: 'scss'}).toJson());
let options = {
hasChildren: (node) => Array.isArray(node.content),
getChildren: (node) => node.content,
toJSON: (node, children) => {
return Object.assign({}, node, {
content: children ? children : node.content
})
},
toString: (node) => {
return typeof node.content === 'string' ? node.content : ''
}
}
const $ast = createQueryWrapper(ast, options);
const implicitGlobalDeclarations = $ast('declaration').hasParent('stylesheet');
const explicitGlobalDeclarations = $ast('declaration').hasParent('block')
.filter(node => isExplicitGlobalDeclaration($ast, node));
let implicitGlobals = implicitGlobalDeclarations.map(declaration => parseDeclaration($ast, declaration, SCOPE_IMPICIT));
let explicitGlobals = explicitGlobalDeclarations.map(declaration => parseDeclaration($ast, declaration, SCOPE_EXPLICIT));
return { explicitGlobals, implicitGlobals };
}