Skip to content

Commit 24f315e

Browse files
committed
Merge pull request #180 from varya/features/gonzales-based-splitter
Gonzales based KSS splitter + more complex tests
2 parents 234cbbb + 2ce75a6 commit 24f315e

File tree

2 files changed

+396
-323
lines changed

2 files changed

+396
-323
lines changed

lib/modules/kss-splitter.js

+58-45
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,76 @@
11
'use strict'
22

3-
var kssBlocksParser = require('./kss-blocks-parser').kssBlocksParser;
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 */
8-
pureSplitter: function(source) {
9-
var lines = source.split(/\n|$/g),
10-
anInlineComment = /^ *\/\//,
11-
multilineCommentStarted = /\/\*/,
12-
multilineCommentFinished = /\*\//,
13-
isMultilineCommentStarted,
14-
blocks = [],
15-
block,
16-
type,
17-
prevType;
18-
19-
lines.forEach(function(line) {
20-
21-
if (isMultilineCommentStarted) {
22-
type = 'comment'
23-
if (multilineCommentFinished.test(line)) {
24-
isMultilineCommentStarted = false;
9+
pureSplitter: function(source, syntax) {
10+
11+
syntax = syntax || 'scss';
12+
var out = {},
13+
ast = gonzales.srcToAST({
14+
src: source,
15+
syntax: syntax
16+
}),
17+
block,
18+
blocks = [],
19+
prevNode;
20+
21+
gonzo.traverse(ast, [{
22+
// Visitor for SASS and SCSS syntaxes
23+
test: function(name, nodes) {
24+
return name !== 'stylesheet';
25+
},
26+
process: function(nodes) {
27+
var startNewBlock = function() {
28+
block && blocks.push(block);
29+
block = {
30+
type: '',
31+
content: ''
32+
};
33+
},
34+
type;
35+
36+
// Calculate type of the current block
37+
38+
// Multiline comment is comment
39+
if (nodes[0] == 'commentML') {
40+
type = 'comment';
2541
}
26-
} else {
27-
isMultilineCommentStarted = multilineCommentStarted.test(line);
28-
if (isMultilineCommentStarted) {
29-
type = 'comment'
30-
} else {
31-
type = anInlineComment.test(line) ? 'comment' : 'code';
42+
// Singleline comment is comment
43+
else if (nodes[0] === 'commentSL') {
44+
type = 'comment';
3245
}
33-
}
34-
35-
if (prevType != type) {
36-
// Save old block if a new type appears
37-
if (block) {
38-
block.content = block.content.join('\n');
39-
blocks.push(block);
46+
// Single breaklines between singleline comments are comments
47+
else if (nodes[0] === 's' && nodes[1].split('\n').length <= 2 && prevNode[0] === 'commentSL') {
48+
type = 'comment';
4049
}
41-
// Start a new block after changing the type
42-
block = {
43-
type: type,
44-
content: [line]
50+
else {
51+
type = 'code';
4552
}
46-
} else {
47-
block.content.push(line);
48-
}
4953

50-
prevType = type;
54+
// If type has been changed, start a new block
55+
if (!block || block.type != type) {
56+
startNewBlock();
57+
}
5158

52-
});
59+
// Extend current block content
60+
block.type = type;
61+
block.content += gonzales.astToSrc({
62+
ast: nodes,
63+
syntax: syntax
64+
});
65+
prevNode = nodes;
5366

54-
// Push the last block
55-
block.content = block.content.join('\n');
56-
blocks.push(block);
67+
}
68+
}]);
5769

70+
// push last block
71+
blocks.push(block);
5872
return blocks;
73+
5974
},
6075

6176
getBlocks: function(source) {
@@ -86,11 +101,9 @@ module.exports = {
86101
}
87102
} else {
88103
// Not KSS comments are considered to be parts of code
89-
pair.code.push('\n')
90104
pair.code.push(block.content)
91105
}
92106
} else {
93-
pair.code.push('\n')
94107
pair.code.push(block.content)
95108
}
96109

0 commit comments

Comments
 (0)