Skip to content

Commit 9a45221

Browse files
committed
add option to include location data
1 parent 73cdb11 commit 9a45221

13 files changed

+133
-24
lines changed

index.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11

2-
module.exports = function(css){
2+
module.exports = function(css, opts){
3+
4+
var line = 1
5+
, column = 1
6+
, location = opts && opts.loc;
37

48
/**
59
* Parse stylesheet.
@@ -48,10 +52,30 @@ module.exports = function(css){
4852
function match(re) {
4953
var m = re.exec(css);
5054
if (!m) return;
55+
5156
css = css.slice(m[0].length);
57+
updateLocation(m[0]);
5258
return m;
5359
}
5460

61+
/**
62+
* Update line and column number based on the passed `str`.
63+
*/
64+
65+
function updateLocation(str) {
66+
if (!location) return;
67+
var re = /\n/g
68+
, i = 0
69+
, newline;
70+
71+
while((newline = re.exec(str))) {
72+
line++;
73+
column = 0;
74+
i = newline.index;
75+
}
76+
column += str.length-i;
77+
}
78+
5579
/**
5680
* Parse whitespace.
5781
*/
@@ -81,6 +105,7 @@ module.exports = function(css){
81105
while ('*' != css[i] || '/' != css[i + 1]) ++i;
82106
i += 2;
83107
var comment = css.slice(2, i - 2);
108+
updateLocation(comment);
84109
css = css.slice(i);
85110
whitespace();
86111
return { comment: comment };
@@ -334,10 +359,14 @@ module.exports = function(css){
334359
*/
335360

336361
function rule() {
337-
var sel = selector();
362+
var loc = location && { line: line, column: column }
363+
, sel = selector();
364+
338365
if (!sel) return;
339366
comments();
340-
return { selectors: sel, declarations: declarations() };
367+
var ret = { selectors: sel, declarations: declarations() };
368+
if (loc) ret.loc = loc;
369+
return ret;
341370
}
342371

343372
return stylesheet();

test/cases/comment.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
"property": "bar",
2020
"value": "baz"
2121
}
22-
]
22+
],
23+
"loc": {
24+
"line": 12,
25+
"column": 1
26+
}
2327
}
2428
]
2529
}

test/cases/comment.url.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
"property": "bar",
1414
"value": "baz"
1515
}
16-
]
16+
],
17+
"loc": {
18+
"line": 3,
19+
"column": 1
20+
}
1721
}
1822
]
1923
}

test/cases/media.json

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
"property": "color",
1818
"value": "#300"
1919
}
20-
]
20+
],
21+
"loc": {
22+
"line": 2,
23+
"column": 3
24+
}
2125
},
2226
{
2327
"selectors": [
@@ -32,7 +36,11 @@
3236
"property": "margin",
3337
"value": "0 auto"
3438
}
35-
]
39+
],
40+
"loc": {
41+
"line": 6,
42+
"column": 3
43+
}
3644
}
3745
]
3846
},
@@ -52,7 +60,11 @@
5260
"property": "color",
5361
"value": "#000"
5462
}
55-
]
63+
],
64+
"loc": {
65+
"line": 13,
66+
"column": 3
67+
}
5668
},
5769
{
5870
"selectors": [
@@ -67,7 +79,11 @@
6779
"property": "border",
6880
"value": "0.5pt solid #666"
6981
}
70-
]
82+
],
83+
"loc": {
84+
"line": 17,
85+
"column": 3
86+
}
7187
}
7288
]
7389
}

test/cases/media.messed.json

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
"property": "color",
1818
"value": "#300"
1919
}
20-
]
20+
],
21+
"loc": {
22+
"line": 1,
23+
"column": 28
24+
}
2125
},
2226
{
2327
"selectors": [
@@ -32,7 +36,11 @@
3236
"property": "margin",
3337
"value": "0 auto"
3438
}
35-
]
39+
],
40+
"loc": {
41+
"line": 7,
42+
"column": 3
43+
}
3644
}
3745
]
3846
},
@@ -52,7 +60,11 @@
5260
"property": "color",
5361
"value": "#000"
5462
}
55-
]
63+
],
64+
"loc": {
65+
"line": 19,
66+
"column": 15
67+
}
5668
},
5769
{
5870
"selectors": [
@@ -67,7 +79,11 @@
6779
"property": "border",
6880
"value": "0.5pt solid #666"
6981
}
70-
]
82+
],
83+
"loc": {
84+
"line": 23,
85+
"column": 15
86+
}
7187
}
7288
]
7389
}

test/cases/messed-up.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
"property": "foo\n ",
1111
"value": "'bar'"
1212
}
13-
]
13+
],
14+
"loc": {
15+
"line": 1,
16+
"column": 1
17+
}
1418
},
1519
{
1620
"selectors": [
@@ -25,7 +29,11 @@
2529
"property": "bar",
2630
"value": "baz"
2731
}
28-
]
32+
],
33+
"loc": {
34+
"line": 5,
35+
"column": 4
36+
}
2937
},
3038
{
3139
"selectors": [
@@ -40,7 +48,11 @@
4048
"property": "bar\n ",
4149
"value": "baz"
4250
}
43-
]
51+
],
52+
"loc": {
53+
"line": 6,
54+
"column": 4
55+
}
4456
}
4557
]
4658
}

test/cases/no-semi.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
"property": "the-species",
1515
"value": "called \"ferrets\""
1616
}
17-
]
17+
],
18+
"loc": {
19+
"line": 2,
20+
"column": 1
21+
}
1822
}
1923
]
2024
}

test/cases/props.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
"property": "*even",
1919
"value": "'ie crap'"
2020
}
21-
]
21+
],
22+
"loc": {
23+
"line": 2,
24+
"column": 1
25+
}
2226
}
2327
]
2428
}

test/cases/quoted.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
"property": "background",
1111
"value": "url('some;stuff;here') 50% 50% no-repeat"
1212
}
13-
]
13+
],
14+
"loc": {
15+
"line": 1,
16+
"column": 1
17+
}
1418
}
1519
]
1620
}

test/cases/rule.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
"property": "bar",
1111
"value": "'baz'"
1212
}
13-
]
13+
],
14+
"loc": {
15+
"line": 1,
16+
"column": 1
17+
}
1418
}
1519
]
1620
}

test/cases/rules.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
"property": "name",
1111
"value": "'tobi'"
1212
}
13-
]
13+
],
14+
"loc": {
15+
"line": 1,
16+
"column": 1
17+
}
1418
},
1519
{
1620
"selectors": [
@@ -21,7 +25,11 @@
2125
"property": "name",
2226
"value": "'loki'"
2327
}
24-
]
28+
],
29+
"loc": {
30+
"line": 4,
31+
"column": 1
32+
}
2533
}
2634
]
2735
}

test/cases/supports.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
"property": "display",
1818
"value": "flex"
1919
}
20-
]
20+
],
21+
"loc": {
22+
"line": 2,
23+
"column": 3
24+
}
2125
}
2226
]
2327
}

test/css-parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('parse(str)', function(){
1616
it('should parse ' + file, function(){
1717
var css = read(path.join('test', 'cases', file + '.css'), 'utf8');
1818
var json = read(path.join('test', 'cases', file + '.json'), 'utf8');
19-
var ret = parse(css);
19+
var ret = parse(css, { loc: true });
2020
ret = JSON.stringify(ret, null, 2);
2121
ret.should.equal(json);
2222
})

0 commit comments

Comments
 (0)