Skip to content

Commit 9087678

Browse files
author
Juuso Backman
committed
Merge pull request #470 from jpbackman/revert-gonzales-to-old-version
Revert gonzales to old version
2 parents 82cea7d + 465de65 commit 9087678

File tree

7 files changed

+254
-240
lines changed

7 files changed

+254
-240
lines changed

lib/modules/kss-splitter.js

+46-60
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,74 @@
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-
29-
if (node.type === 'stylesheet') {
30-
return;
31-
}
24+
process: function(nodes) {
25+
var startNewBlock = function() {
26+
block && blocks.push(block);
27+
block = {
28+
type: '',
29+
content: ''
30+
};
31+
},
32+
type;
3233

33-
// Calculate type of the current block
34+
// Calculate type of the current block
3435

35-
// Multiline comment is comment
36-
if (node.type === 'commentML') {
37-
type = 'comment';
38-
}
39-
// Singleline comment is comment
40-
else if (node.type === 'commentSL') {
41-
type = 'comment';
42-
}
43-
// Single breaklines between singleline comments are comments
44-
else if (node.type === 's' && node.content.split('\n').length <= 2 && prevNode && prevNode.type === 'commentSL') {
45-
type = 'comment';
46-
}
47-
else {
48-
type = 'code';
49-
}
50-
// If type has been changed, start a new block
51-
if (!block || block.type !== type) {
52-
startNewBlock(node.start);
53-
}
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+
}
5451

55-
// Extend current block content
56-
block.type = type;
52+
// If type has been changed, start a new block
53+
if (!block || block.type !== type) {
54+
startNewBlock();
55+
}
5756

58-
prevNode = node;
57+
// Extend current block content
58+
block.type = type;
59+
block.content += gonzales.astToSrc({
60+
ast: nodes,
61+
syntax: syntax
62+
});
63+
prevNode = nodes;
5964

60-
});
65+
}
66+
}]);
6167

6268
// push last block
6369
if (block) {
6470
blocks.push(block);
6571
}
66-
67-
// Fill blocks with content
68-
69-
blocks.map(function(block, pos) {
70-
var from = block.position,
71-
to = blocks[pos + 1] ? blocks[pos + 1].position : {line: sourceLines.length, column: 1000000},
72-
content;
73-
74-
// Get the code between given positions
75-
76-
// Get the lines between given
77-
content = sourceLines.slice(from.line - 1, to.line);
78-
// Crop the first line
79-
content[0] = content[0].substr(from.column - 1);
80-
// Crop teh last line
81-
content[content.length - 1] = content[content.length - 1].substring(0, to.column - 1);
82-
83-
block.content = content.join('\n');
84-
85-
});
8672
return blocks;
8773
},
8874

lib/modules/variable-parser.js

+75-52
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,76 @@
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 and SCSS 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();
70+
visitors.scss = visitors.sass;
71+
visitor = visitors[syntax];
6572

66-
out.push(variable);
67-
});
68-
} else {
69-
ast.map(traverser);
70-
}
73+
gonzo.traverse(ast, [visitor]);
7174

7275
return out;
7376
}
@@ -76,13 +79,33 @@ function parseVariableDeclarations(string, syntax) {
7679
function findVariables(string, syntax) {
7780
syntax = syntax || 'scss';
7881

79-
var out,
80-
ast = gonzales.parse(string, {
82+
var out = [],
83+
ast = gonzales.srcToAST({
84+
src: string,
8185
syntax: syntax
82-
});
86+
}),
87+
visitors = {
88+
sass: {
89+
// Visitor for SASS and SCSS syntaxes
90+
test: function(name, nodes) {
91+
return (name === 'declaration' && nodes[1][0] === 'variable') || (name === 'variable' && nodes[0] === 'ident');
92+
},
93+
process: function(nodes) {
94+
if (nodes[0] !== 'declaration') {
95+
out.push(nodes[1][1]);
96+
}
97+
}
98+
}
99+
},
100+
visitor;
83101

84-
out = jspath.apply('..content{.type === "value" }..content{.type === "variable" }..content{.type === "ident"}.content', ast);
102+
// For this task LESS visitor is identical to SASS
103+
visitors.less = visitors.sass;
104+
visitors.scss = visitors.sass;
105+
visitor = visitors[syntax];
85106

107+
// Find variables that are used in the styles
108+
gonzo.traverse(ast, [visitor]);
86109
return out;
87110
}
88111

0 commit comments

Comments
 (0)