Skip to content

Commit ea6481f

Browse files
author
junaidrsd
committed
Merge pull request #564 from hannu/downgrade-gonzales
Downgrade gonzales to 3.0.0-12. Improve variable parsing tests
2 parents af53e02 + 9915c3d commit ea6481f

File tree

7 files changed

+303
-196
lines changed

7 files changed

+303
-196
lines changed

lib/modules/kss-splitter.js

+52-52
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,77 @@
11
'use strict';
22

3-
var gonzales = require('gonzales-pe');
3+
var gonzales = require('gonzales-pe'),
4+
gonzo = require('gonzales-ast');
45

56
module.exports = {
67

78
/* Split string source into array of code and comment blocks */
89
pureSplitter: function(source, syntax) {
910
syntax = syntax || 'scss';
10-
var ast = gonzales.parse(source, {
11+
var ast = gonzales.srcToAST({
12+
src: source,
1113
syntax: syntax
1214
}),
13-
sourceLines = source.split(/\n/),
1415
block,
1516
blocks = [],
1617
prevNode;
1718

18-
ast.map(function(node) {
19-
var startNewBlock = function(position) {
20-
block && blocks.push(block);
21-
block = {
22-
type: '',
23-
position: position,
24-
content: ''
25-
};
19+
gonzo.traverse(ast, [{
20+
// Visitor for SASS and SCSS syntaxes
21+
test: function(name) {
22+
return name !== 'stylesheet';
2623
},
27-
type;
28-
if (node.type === 'stylesheet') {
29-
return;
30-
}
31-
// Calculate type of the current block
32-
// Multiline comment is comment
33-
if (node.type === 'commentML') {
34-
type = 'comment';
35-
}
36-
// Singleline comment is comment
37-
else if (node.type === 'commentSL') {
38-
type = 'comment';
39-
}
40-
// Single breaklines between singleline comments are comments
41-
else if (node.type === 's' && node.content.split('\n').length <= 2 && prevNode && prevNode.type === 'commentSL') {
42-
type = 'comment';
43-
}
44-
else {
45-
type = 'code';
46-
}
47-
// If type has been changed, start a new block
48-
if (!block || block.type !== type) {
49-
startNewBlock(node.start);
24+
process: function(nodes) {
25+
var startNewBlock = function() {
26+
block && blocks.push(block);
27+
block = {
28+
type: '',
29+
content: ''
30+
};
31+
},
32+
type;
33+
34+
// Calculate type of the current block
35+
36+
// Multiline comment is comment
37+
if (nodes[0] === 'commentML') {
38+
type = 'comment';
39+
}
40+
// Singleline comment is comment
41+
else if (nodes[0] === 'commentSL') {
42+
type = 'comment';
43+
}
44+
// Single breaklines between singleline comments are comments
45+
else if (nodes[0] === 's' && nodes[1].split('\n').length <= 2 && prevNode && prevNode[0] === 'commentSL') {
46+
type = 'comment';
47+
}
48+
else {
49+
type = 'code';
50+
}
51+
52+
// If type has been changed, start a new block
53+
if (!block || block.type !== type) {
54+
startNewBlock();
55+
}
56+
57+
// Extend current block content
58+
block.type = type;
59+
block.content += gonzales.astToSrc({
60+
ast: nodes,
61+
syntax: syntax
62+
});
63+
prevNode = nodes;
64+
5065
}
51-
// Extend current block content
52-
block.type = type;
53-
prevNode = node;
54-
});
66+
}]);
67+
5568
// push last block
5669
if (block) {
5770
blocks.push(block);
5871
}
59-
// Fill blocks with content
60-
blocks.map(function(block, pos) {
61-
var from = block.position,
62-
to = blocks[pos + 1] ? blocks[pos + 1].position : {line: sourceLines.length, column: 1000000},
63-
content;
64-
// Get the code between given positions
65-
// Get the lines between given
66-
content = sourceLines.slice(from.line - 1, to.line);
67-
// Crop the first line
68-
content[0] = content[0].substr(from.column - 1);
69-
// Crop teh last line
70-
content[content.length - 1] = content[content.length - 1].substring(0, to.column - 1);
71-
block.content = content.join('\n');
72-
});
7372
return blocks;
7473
},
74+
7575
getBlocks: function(source, syntax) {
7676
var blocks = this.pureSplitter(source, syntax),
7777
pair = {

lib/modules/variable-parser.js

+79-52
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,77 @@
11
'use strict';
22

33
var gonzales = require('gonzales-pe'),
4-
jspath = require('jspath'),
4+
gonzo = require('gonzales-ast'),
55
path = require('path'),
66
Q = require('q'),
77
_ = require('lodash');
88

9+
function astToSrc(ast, syntax) {
10+
return gonzales.astToSrc({
11+
ast: ast,
12+
syntax: syntax
13+
});
14+
}
15+
916
// Parse Style variables to object
1017
function parseVariableDeclarations(string, syntax) {
1118
syntax = syntax || 'scss';
1219

1320
var out = [],
14-
ast = gonzales.parse(string, {
21+
ast = gonzales.srcToAST({
22+
src: string,
1523
syntax: syntax
1624
}),
17-
traversers = {
18-
sass: function(node) {
19-
var variable = {};
20-
21-
if (node.type !== 'declaration') {
22-
return;
25+
visitors = {
26+
sass: {
27+
// Visitor for SASS, SCSS and plain CSS syntaxes
28+
test: function(name, nodes) {
29+
return name === 'declaration' && nodes[1][0] === 'variable';
30+
},
31+
process: function(nodes) {
32+
var varName = nodes[1][1][1].filter(function(element) {
33+
return element !== 'ident';
34+
})[0];
35+
36+
out.push({name: varName, value: astToSrc(nodes[4], syntax)});
2337
}
24-
25-
node.map(function(nnode) {
26-
if (nnode.type === 'variable') {
27-
nnode.map(function(nnnode) {
28-
if (nnnode.type === 'ident') {
29-
variable.name = nnnode.content;
38+
},
39+
less: {
40+
// Visitor for LESS syntax
41+
test: function(name) {
42+
return name === 'atrules';
43+
},
44+
process: function(nodes) {
45+
var varName = nodes[1][1][1],
46+
varVal = '';
47+
48+
// Skip at-keywords that do not decrade variable (Ex. @imports)
49+
if (nodes[2][0] === 'operator' && nodes[2][1] === ':') {
50+
/* Grabs all the listed values
51+
* Fix then https://github.com/tonyganch/gonzales-pe/issues/17 is fixed */
52+
nodes.forEach(function(element) {
53+
if (element === 'atrules' || element[0] === 'atkeyword') {
54+
return;
55+
}
56+
if (element[0] === 'operator' && element[1] === ':'
57+
) {
58+
return;
3059
}
60+
varVal += astToSrc(element, syntax); // Syntax is always less as this visitor is only for LESS
3161
});
62+
63+
out.push({name: varName, value: varVal.trim()});
3264
}
33-
if (nnode.type === 'value') {
34-
variable.value = nnode.toCSS(syntax);
35-
}
36-
});
37-
out.push(variable);
65+
}
3866
}
3967
},
40-
traverser;
41-
42-
traversers.scss = traversers.sass;
43-
traverser = traversers[syntax];
44-
45-
if (syntax === 'less') {
46-
// Skip at-keywords that do not decrade variable (Ex. @imports)
47-
var atRulesWithOperators = jspath.apply('..content{.type === "atrules" && ..content{.type === "operator" && .content === ":" }}', ast);
48-
/* Grabs all the listed values
49-
* Fix then https://github.com/tonyganch/gonzales-pe/issues/17 is fixed */
50-
atRulesWithOperators.forEach(function(atRule) {
51-
var variable = {};
52-
variable.name = jspath.apply('..content{.type == "atkeyword"}..content{.type == "ident"}.content', atRule).toString();
53-
54-
atRule.content = atRule.content.filter(function(node) {
55-
if (node.type === 'atkeyword') {
56-
return false;
57-
}
58-
if (node.type === 'operator' && node.content === ':') {
59-
return false;
60-
}
61-
return node;
62-
});
68+
visitor;
6369

64-
variable.value = atRule.toCSS(syntax).trim();
65-
out.push(variable);
66-
});
67-
} else {
68-
ast.map(traverser);
69-
}
70+
visitors.css = visitors.sass;
71+
visitors.scss = visitors.sass;
72+
visitor = visitors[syntax];
73+
74+
gonzo.traverse(ast, [visitor]);
7075

7176
return out;
7277
}
@@ -75,12 +80,34 @@ function parseVariableDeclarations(string, syntax) {
7580
function findVariables(string, syntax) {
7681
syntax = syntax || 'scss';
7782

78-
var out,
79-
ast = gonzales.parse(string, {
83+
var out = [],
84+
ast = gonzales.srcToAST({
85+
src: string,
8086
syntax: syntax
81-
});
87+
}),
88+
visitors = {
89+
sass: {
90+
// Visitor for SASS, SCSS and plain CSS syntaxes
91+
test: function(name, nodes) {
92+
return (name === 'declaration' && nodes[1][0] === 'variable') || (name === 'variable' && nodes[0] === 'ident');
93+
},
94+
process: function(nodes) {
95+
if (nodes[0] !== 'declaration') {
96+
out.push(nodes[1][1]);
97+
}
98+
}
99+
}
100+
},
101+
visitor;
102+
103+
// For this task LESS visitor is identical to SASS
104+
visitors.less = visitors.sass;
105+
visitors.css = visitors.sass;
106+
visitors.scss = visitors.sass;
107+
visitor = visitors[syntax];
82108

83-
out = jspath.apply('..content{.type === "value" }..content{.type === "variable" }..content{.type === "ident"}.content', ast);
109+
// Find variables that are used in the styles
110+
gonzo.traverse(ast, [visitor]);
84111
return out;
85112
}
86113

0 commit comments

Comments
 (0)