Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gonzales based KSS splitter + more complex tests #180

Merged
merged 10 commits into from
Nov 13, 2014
103 changes: 58 additions & 45 deletions lib/modules/kss-splitter.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,76 @@
'use strict'

var kssBlocksParser = require('./kss-blocks-parser').kssBlocksParser;
var gonzales = require('gonzales-pe'),
gonzo = require('gonzales-ast');

module.exports = {

/* Split string source into array of code and comment blocks */
pureSplitter: function(source) {
var lines = source.split(/\n|$/g),
anInlineComment = /^ *\/\//,
multilineCommentStarted = /\/\*/,
multilineCommentFinished = /\*\//,
isMultilineCommentStarted,
blocks = [],
block,
type,
prevType;

lines.forEach(function(line) {

if (isMultilineCommentStarted) {
type = 'comment'
if (multilineCommentFinished.test(line)) {
isMultilineCommentStarted = false;
pureSplitter: function(source, syntax) {

syntax = syntax || 'scss';
var out = {},
ast = gonzales.srcToAST({
src: source,
syntax: syntax
}),
block,
blocks = [],
prevNode;

gonzo.traverse(ast, [{
// Visitor for SASS and SCSS syntaxes
test: function(name, nodes) {
return name !== 'stylesheet';
},
process: function(nodes) {
var startNewBlock = function() {
block && blocks.push(block);
block = {
type: '',
content: ''
};
},
type;

// Calculate type of the current block

// Multiline comment is comment
if (nodes[0] == 'commentML') {
type = 'comment';
}
} else {
isMultilineCommentStarted = multilineCommentStarted.test(line);
if (isMultilineCommentStarted) {
type = 'comment'
} else {
type = anInlineComment.test(line) ? 'comment' : 'code';
// Singleline comment is comment
else if (nodes[0] === 'commentSL') {
type = 'comment';
}
}

if (prevType != type) {
// Save old block if a new type appears
if (block) {
block.content = block.content.join('\n');
blocks.push(block);
// Single breaklines between singleline comments are comments
else if (nodes[0] === 's' && nodes[1].split('\n').length <= 2 && prevNode[0] === 'commentSL') {
type = 'comment';
}
// Start a new block after changing the type
block = {
type: type,
content: [line]
else {
type = 'code';
}
} else {
block.content.push(line);
}

prevType = type;
// If type has been changed, start a new block
if (!block || block.type != type) {
startNewBlock();
}

});
// Extend current block content
block.type = type;
block.content += gonzales.astToSrc({
ast: nodes,
syntax: syntax
});
prevNode = nodes;

// Push the last block
block.content = block.content.join('\n');
blocks.push(block);
}
}]);

// push last block
blocks.push(block);
return blocks;

},

getBlocks: function(source) {
Expand Down Expand Up @@ -86,11 +101,9 @@ module.exports = {
}
} else {
// Not KSS comments are considered to be parts of code
pair.code.push('\n')
pair.code.push(block.content)
}
} else {
pair.code.push('\n')
pair.code.push(block.content)
}

Expand Down
Loading