Skip to content

Commit

Permalink
Upgrade gonzales to 3.0.0-16
Browse files Browse the repository at this point in the history
  • Loading branch information
Junaid committed Mar 30, 2015
1 parent 542935e commit 01a3532
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 206 deletions.
104 changes: 52 additions & 52 deletions lib/modules/kss-splitter.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
'use strict';

var gonzales = require('gonzales-pe'),
gonzo = require('gonzales-ast');
var gonzales = require('gonzales-pe');

module.exports = {

/* Split string source into array of code and comment blocks */
pureSplitter: function(source, syntax) {
syntax = syntax || 'scss';
var ast = gonzales.srcToAST({
src: source,
var ast = gonzales.parse(source, {
syntax: syntax
}),
sourceLines = source.split(/\n/),
block,
blocks = [],
prevNode;

gonzo.traverse(ast, [{
// Visitor for SASS and SCSS syntaxes
test: function(name) {
return name !== 'stylesheet';
ast.map(function(node) {
var startNewBlock = function(position) {
block && blocks.push(block);
block = {
type: '',
position: position,
content: ''
};
},
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';
}
// Singleline comment is comment
else if (nodes[0] === 'commentSL') {
type = 'comment';
}
// Single breaklines between singleline comments are comments
else if (nodes[0] === 's' && nodes[1].split('\n').length <= 2 && prevNode && prevNode[0] === 'commentSL') {
type = 'comment';
}
else {
type = 'code';
}

// 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;

type;
if (node.type === 'stylesheet') {
return;
}
}]);

// Calculate type of the current block
// Multiline comment is comment
if (node.type === 'commentML') {
type = 'comment';
}
// Singleline comment is comment
else if (node.type === 'commentSL') {
type = 'comment';
}
// Single breaklines between singleline comments are comments
else if (node.type === 's' && node.content.split('\n').length <= 2 && prevNode && prevNode.type === 'commentSL') {
type = 'comment';
}
else {
type = 'code';
}
// If type has been changed, start a new block
if (!block || block.type !== type) {
startNewBlock(node.start);
}
// Extend current block content
block.type = type;
prevNode = node;
});
// push last block
if (block) {
blocks.push(block);
}
// Fill blocks with content
blocks.map(function(block, pos) {
var from = block.position,
to = blocks[pos + 1] ? blocks[pos + 1].position : {line: sourceLines.length, column: 1000000},
content;
// Get the code between given positions
// Get the lines between given
content = sourceLines.slice(from.line - 1, to.line);
// Crop the first line
content[0] = content[0].substr(from.column - 1);
// Crop teh last line
content[content.length - 1] = content[content.length - 1].substring(0, to.column - 1);
block.content = content.join('\n');
});
return blocks;
},

getBlocks: function(source, syntax) {
var blocks = this.pureSplitter(source, syntax),
pair = {
Expand Down
131 changes: 52 additions & 79 deletions lib/modules/variable-parser.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,72 @@
'use strict';

var gonzales = require('gonzales-pe'),
gonzo = require('gonzales-ast'),
jspath = require('jspath'),
path = require('path'),
Q = require('q'),
_ = require('lodash');

function astToSrc(ast, syntax) {
return gonzales.astToSrc({
ast: ast,
syntax: syntax
});
}

// Parse Style variables to object
function parseVariableDeclarations(string, syntax) {
syntax = syntax || 'scss';

var out = [],
ast = gonzales.srcToAST({
src: string,
ast = gonzales.parse(string, {
syntax: syntax
}),
visitors = {
sass: {
// Visitor for SASS, SCSS and plain CSS syntaxes
test: function(name, nodes) {
return name === 'declaration' && nodes[1][0] === 'variable';
},
process: function(nodes) {
var varName = nodes[1][1][1].filter(function(element) {
return element !== 'ident';
})[0];

out.push({name: varName, value: astToSrc(nodes[4], syntax)});
traversers = {
sass: function(node) {
var variable = {};

if (node.type !== 'declaration') {
return;
}
},
less: {
// Visitor for LESS syntax
test: function(name) {
return name === 'atrules';
},
process: function(nodes) {
var varName = nodes[1][1][1],
varVal = '';

// Skip at-keywords that do not decrade variable (Ex. @imports)
if (nodes[2][0] === 'operator' && nodes[2][1] === ':') {
/* Grabs all the listed values
* Fix then https://github.com/tonyganch/gonzales-pe/issues/17 is fixed */
nodes.forEach(function(element) {
if (element === 'atrules' || element[0] === 'atkeyword') {
return;
}
if (element[0] === 'operator' && element[1] === ':'
) {
return;

node.map(function(nnode) {
if (nnode.type === 'variable') {
nnode.map(function(nnnode) {
if (nnnode.type === 'ident') {
variable.name = nnnode.content;
}
varVal += astToSrc(element, syntax); // Syntax is always less as this visitor is only for LESS
});

out.push({name: varName, value: varVal.trim()});
}
}
if (nnode.type === 'value') {
variable.value = nnode.toCSS(syntax);
}
});
out.push(variable);
}
},
visitor;

visitors.css = visitors.sass;
visitors.scss = visitors.sass;
visitor = visitors[syntax];
traverser;

traversers.scss = traversers.sass;
traverser = traversers[syntax];

if (syntax === 'less') {
// Skip at-keywords that do not decrade variable (Ex. @imports)
var atRulesWithOperators = jspath.apply('..content{.type === "atrules" && ..content{.type === "operator" && .content === ":" }}', ast);
/* Grabs all the listed values
* Fix then https://github.com/tonyganch/gonzales-pe/issues/17 is fixed */
atRulesWithOperators.forEach(function(atRule) {
var variable = {};
variable.name = jspath.apply('..content{.type == "atkeyword"}..content{.type == "ident"}.content', atRule).toString();

atRule.content = atRule.content.filter(function(node) {
if (node.type === 'atkeyword') {
return false;
}
if (node.type === 'operator' && node.content === ':') {
return false;
}
return node;
});

gonzo.traverse(ast, [visitor]);
variable.value = atRule.toCSS(syntax).trim();
out.push(variable);
});
} else {
ast.map(traverser);
}

return out;
}
Expand All @@ -80,34 +75,12 @@ function parseVariableDeclarations(string, syntax) {
function findVariables(string, syntax) {
syntax = syntax || 'scss';

var out = [],
ast = gonzales.srcToAST({
src: string,
var out,
ast = gonzales.parse(string, {
syntax: syntax
}),
visitors = {
sass: {
// Visitor for SASS, SCSS and plain CSS syntaxes
test: function(name, nodes) {
return (name === 'declaration' && nodes[1][0] === 'variable') || (name === 'variable' && nodes[0] === 'ident');
},
process: function(nodes) {
if (nodes[0] !== 'declaration') {
out.push(nodes[1][1]);
}
}
}
},
visitor;

// For this task LESS visitor is identical to SASS
visitors.less = visitors.sass;
visitors.css = visitors.sass;
visitors.scss = visitors.sass;
visitor = visitors[syntax];
});

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

Expand Down
Loading

0 comments on commit 01a3532

Please sign in to comment.