From bd63521666079916423689182d99708b5f7cab42 Mon Sep 17 00:00:00 2001 From: Nate Wiebe Date: Mon, 3 Jun 2019 17:00:22 -0400 Subject: [PATCH 1/2] Add tests for adding backslashes in names --- .../functionNameWithBackslash.test.scss | 3 ++ .../fixtures/mixinNameWithBackslash.test.scss | 3 ++ .../placeholderNameWithBackslash.test.scss | 3 ++ .../variableNameWithBackslash.test.scss | 1 + test/test.js | 37 +++++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 test/fixtures/functionNameWithBackslash.test.scss create mode 100644 test/fixtures/mixinNameWithBackslash.test.scss create mode 100644 test/fixtures/placeholderNameWithBackslash.test.scss create mode 100644 test/fixtures/variableNameWithBackslash.test.scss diff --git a/test/fixtures/functionNameWithBackslash.test.scss b/test/fixtures/functionNameWithBackslash.test.scss new file mode 100644 index 0000000..e50b5fa --- /dev/null +++ b/test/fixtures/functionNameWithBackslash.test.scss @@ -0,0 +1,3 @@ +@function function\with\backslash(){ + $some : "code"; +} \ No newline at end of file diff --git a/test/fixtures/mixinNameWithBackslash.test.scss b/test/fixtures/mixinNameWithBackslash.test.scss new file mode 100644 index 0000000..8dceb9f --- /dev/null +++ b/test/fixtures/mixinNameWithBackslash.test.scss @@ -0,0 +1,3 @@ +@mixin mixin\with\backslash(){ + $some : "code"; +} \ No newline at end of file diff --git a/test/fixtures/placeholderNameWithBackslash.test.scss b/test/fixtures/placeholderNameWithBackslash.test.scss new file mode 100644 index 0000000..21e4ba0 --- /dev/null +++ b/test/fixtures/placeholderNameWithBackslash.test.scss @@ -0,0 +1,3 @@ +%placeholder\with\backslash { + $some : "code"; +} \ No newline at end of file diff --git a/test/fixtures/variableNameWithBackslash.test.scss b/test/fixtures/variableNameWithBackslash.test.scss new file mode 100644 index 0000000..4b52ac7 --- /dev/null +++ b/test/fixtures/variableNameWithBackslash.test.scss @@ -0,0 +1 @@ +$variable\with\backslash : 'value'; \ No newline at end of file diff --git a/test/test.js b/test/test.js index b78edf2..facd195 100644 --- a/test/test.js +++ b/test/test.js @@ -40,6 +40,15 @@ describe('scss-comment-parser', function () { code: '\n $some : "code";\n' }) }) + + it('should work for a name that contains a backslash', function () { + var context = parser.contextParser(getContent('placeholderNameWithBackslash.test.scss')) + assert.deepEqual(context, { + type: 'placeholder', + name: 'placeholder\\with\\backslash', + code: '\n $some : "code";\n' + }) + }) }) describe('mixin', function () { @@ -58,6 +67,15 @@ describe('scss-comment-parser', function () { var context = parser.contextParser(getContent('mixinLinebreaks.test.scss')) assert.deepEqual(context, expected) }) + + it('should work for a name that contains a backslash', function () { + var context = parser.contextParser(getContent('mixinNameWithBackslash.test.scss')) + assert.deepEqual(context, { + type: 'mixin', + name: 'mixin\\with\\backslash', + code: '\n $some : "code";\n' + }) + }) }) describe('function', function () { @@ -76,6 +94,15 @@ describe('scss-comment-parser', function () { var context = parser.contextParser(getContent('functionLinebreaks.test.scss')) assert.deepEqual(context, expected) }) + + it('should work for a name that contains a backslash', function () { + var context = parser.contextParser(getContent('functionNameWithBackslash.test.scss')) + assert.deepEqual(context, { + type: 'function', + name: 'function\\with\\backslash', + code: '\n $some : "code";\n' + }) + }) }) describe('variable', function () { @@ -145,6 +172,16 @@ describe('scss-comment-parser', function () { scope: 'private' }) }) + + it('should work for a name that contains a backslash', function () { + var context = parser.contextParser(getContent('variableNameWithBackslash.test.scss')) + assert.deepEqual(context, { + type: 'variable', + name: 'variable\\with\\backslash', + value: '\'value\'', + scope: 'private' + }) + }) }) describe('css rule', function () { From 4bb0fc2d64bf6d79f8fda2623bd05a6a36fbd20e Mon Sep 17 00:00:00 2001 From: Nate Wiebe Date: Mon, 3 Jun 2019 17:00:36 -0400 Subject: [PATCH 2/2] Update the regex to allow for backslashes in names --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 50bc711..fd1b5d9 100644 --- a/index.js +++ b/index.js @@ -117,7 +117,7 @@ var addCodeToContext = function (context, ctxCode, match) { * SCSS Context Parser */ var scssContextParser = (function () { - var ctxRegEx = /^(@|%|\$)([\w-_]+)*(?:\s+([\w-_]+)|[\s\S]*?:([\s\S]*?)(?:\s!(\w+))?;[ \t]*?(?=\/\/|\n|$))?/ + var ctxRegEx = /^(@|%|\$)([\w-_\\]+)*(?:\s+([\w-_\\]+)|[\s\S]*?:([\s\S]*?)(?:\s!(\w+))?;[ \t]*?(?=\/\/|\n|$))?/ var parser = function (ctxCode, lineNumberFor) { var match = ctxRegEx.exec(ctxCode.trim()) var startIndex, endIndex