Skip to content

Commit

Permalink
Add line numbers for CSS blocks, too.
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Dec 17, 2015
1 parent ef9520a commit 09ba95b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
27 changes: 15 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,16 @@ var scssContextParser = (function () {
var ctxRegEx = /^(@|%|\$)([\w-_]+)*(?:\s+([\w-_]+)|[\s\S]*?\:([\s\S]*?)(?:\s!(\w+))?\;)?/;
var parser = function (ctxCode, lineNumberFor) {
var match = ctxRegEx.exec(ctxCode.trim());
var startIndex, endIndex;

var context = {
type : 'unknown'
};

if (match) {
var wsOffset = Math.min(ctxCode.match(/\s*/).length - 1, 0);
var startIndex = wsOffset + match.index;
var endIndex = startIndex + match[0].length;
startIndex = wsOffset + match.index;
endIndex = startIndex + match[0].length;

if (match[1] === '@' && (match[2] === 'function' || match[2] === 'mixin')) {
context.type = match[2];
Expand All @@ -141,21 +142,23 @@ var scssContextParser = (function () {
context.value = match[4].trim();
context.scope = match[5] || 'private';
}
if (lineNumberFor !== undefined) {
context.line = {
start : lineNumberFor(startIndex) + 1,
end : lineNumberFor(endIndex) + 1
};
}
} else {
var codeStart = ctxCode.indexOf('{');
if (codeStart > 0) {
startIndex = ctxCode.indexOf('{');
endIndex = ctxCode.length - 1;
if (startIndex > 0) {
context.type = 'css';
context.name = ctxCode.slice(0, codeStart).trim();
context.value = extractCode(ctxCode, codeStart).trim();
context.name = ctxCode.slice(0, startIndex).trim();
context.value = extractCode(ctxCode, startIndex).trim();
}
}

if (lineNumberFor !== undefined && startIndex !== undefined) {
context.line = {
start : lineNumberFor(startIndex) + 1,
end : lineNumberFor(endIndex) + 1
};
}

return context;
};

Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ describe('ScssCommentParser', function () {
value: 'font-weight: bold;\n color: red;\n .bar {\n color: blue;\n }'
});
});

it('should add line numbers if lineNumberFor provided', function(){
var lineNumberFor = function (index) { return index; }
var context = parser.contextParser(getContent('rule.test.scss'), lineNumberFor);
assert.deepEqual(context, {
type: 'css',
name: '.foo',
value: 'font-weight: bold;\n color: red;\n .bar {\n color: blue;\n }',
line: { start: 6, end: 74 }
});
});
});

describe('unknown', function(){
Expand Down

0 comments on commit 09ba95b

Please sign in to comment.