diff --git a/index.js b/index.js index 3701858..b99b1fa 100644 --- a/index.js +++ b/index.js @@ -117,6 +117,7 @@ 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' @@ -124,8 +125,8 @@ var scssContextParser = (function () { 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]; @@ -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; }; diff --git a/test/test.js b/test/test.js index 66e6e60..94c0d76 100644 --- a/test/test.js +++ b/test/test.js @@ -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(){