Skip to content

Commit 811a69d

Browse files
author
Juuso Backman
committed
Merge pull request #267 from hannu/function-parameter-variables
Find used variables also from function parameters
2 parents 447463f + 474b3a8 commit 811a69d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/modules/variable-parser.js

+15
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module.exports.findVariables = function(string, syntax) {
7676
syntax: syntax
7777
});
7878

79+
// Find variables that are used in the styles
7980
gonzo.traverse(ast, [{
8081
// Visitor for SASS and SCSS syntaxes
8182
test: function(name, nodes) {
@@ -102,6 +103,20 @@ module.exports.findVariables = function(string, syntax) {
102103
}
103104
}]);
104105

106+
// Find variables that are used as function parameters in SASS/SCSS
107+
gonzo.traverse(ast, [{
108+
// Visitor for SASS and SCSS syntaxes
109+
test: function(name, nodes) {
110+
return name === 'arguments';
111+
},
112+
process: function(nodes) {
113+
nodes.forEach(function(element) {
114+
if (element[0] === 'variable' && element[1][0] === 'ident') {
115+
out.push(element[1][1]);
116+
}
117+
});
118+
}
119+
}]);
105120
return out;
106121
};
107122

test/unit/modules/variable-parser.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ describe('Parser', function() {
3636
result = ['mycolor2'];
3737
expect(parser.findVariables(str)).eql(result);
3838
});
39+
40+
it('should find variables that are used as function arguments', function() {
41+
var str = multiline(function() {
42+
/*
43+
.testStyle {
44+
color: rgba($mycolor, $myopacity);
45+
}
46+
*/
47+
}),
48+
result = ['mycolor', 'myopacity'];
49+
expect(parser.findVariables(str)).eql(result);
50+
});
3951
});
4052

4153
describe('LESS syntax', function() {
@@ -67,6 +79,18 @@ describe('Parser', function() {
6779
result = ['mycolor2'];
6880
expect(parser.findVariables(str, 'less')).eql(result);
6981
});
82+
83+
it('should find variables that are used as function arguments', function() {
84+
var str = multiline(function() {
85+
/*
86+
.testStyle {
87+
color: rgba(@mycolor, @myopacity);
88+
}
89+
*/
90+
}),
91+
result = ['mycolor', 'myopacity'];
92+
expect(parser.findVariables(str, 'less')).eql(result);
93+
});
7094
});
7195
});
7296

0 commit comments

Comments
 (0)