From a46e072d4e3bdf43465d8579247ded1572a44d08 Mon Sep 17 00:00:00 2001 From: Roman Dvornov Date: Fri, 6 May 2016 17:49:45 +0300 Subject: [PATCH] add offset to parse error details --- lib/parser/index.js | 7 ++++- test/fixture/parse-errors.json | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/parser/index.js b/lib/parser/index.js index 9f322257..149a7e97 100644 --- a/lib/parser/index.js +++ b/lib/parser/index.js @@ -44,15 +44,19 @@ var blockMode = { function parseError(message) { var error = new Error(message); + var offset = 0; var line = 1; var column = 1; var lines; if (scanner.token !== null) { + offset = scanner.token.offset; line = scanner.token.line; column = scanner.token.column; } else if (scanner.prevToken !== null) { - lines = scanner.prevToken.value.trimRight().split(/\n|\r\n?|\f/); + lines = scanner.prevToken.value.trimRight(); + offset = scanner.prevToken.offset + lines.length; + lines = lines.split(/\n|\r\n?|\f/); line = scanner.prevToken.line + lines.length - 1; column = lines.length > 1 ? lines[lines.length - 1].length + 1 @@ -61,6 +65,7 @@ function parseError(message) { error.name = 'CssSyntaxError'; error.parseError = { + offset: offset, line: line, column: column }; diff --git a/test/fixture/parse-errors.json b/test/fixture/parse-errors.json index 46360923..a8c51c4d 100644 --- a/test/fixture/parse-errors.json +++ b/test/fixture/parse-errors.json @@ -2,6 +2,7 @@ "css": "100 {}", "error": "Identifier is expected", "position": { + "offset": 3, "line": 1, "column": 4 } @@ -9,6 +10,7 @@ "css": ". {}", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -16,6 +18,7 @@ "css": ".{}", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -23,6 +26,7 @@ "css": ".", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -30,6 +34,7 @@ "css": ".1px {}", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -37,6 +42,7 @@ "css": "# {}", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -44,6 +50,7 @@ "css": "#{}", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -51,6 +58,7 @@ "css": "#", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -58,6 +66,7 @@ "css": "a { color }", "error": "Colon is expected", "position": { + "offset": 10, "line": 1, "column": 11 } @@ -65,6 +74,7 @@ "css": "a { color: #}", "error": "Number or identifier is expected", "position": { + "offset": 12, "line": 1, "column": 13 } @@ -72,6 +82,7 @@ "css": "a { color: # }", "error": "Number or identifier is expected", "position": { + "offset": 12, "line": 1, "column": 13 } @@ -79,6 +90,7 @@ "css": ": {}", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -86,6 +98,7 @@ "css": ":{}", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -93,6 +106,7 @@ "css": ":", "error": "Colon or identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -100,6 +114,7 @@ "css": "::", "error": "Identifier is expected", "position": { + "offset": 2, "line": 1, "column": 3 } @@ -107,6 +122,7 @@ "css": ": ", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -114,6 +130,7 @@ "css": ":pseudo({}", "error": "Unexpected input", "position": { + "offset": 8, "line": 1, "column": 9 } @@ -121,6 +138,7 @@ "css": ":pseudo(--test) {}", "error": "Identifier is expected", "position": { + "offset": 9, "line": 1, "column": 10 } @@ -128,6 +146,7 @@ "css": ":pseudo(var(--test)) {}", "error": "Identifier is expected", "position": { + "offset": 13, "line": 1, "column": 14 } @@ -135,6 +154,7 @@ "css": ":nth-child(xxx)", "error": "Unexpected identifier", "position": { + "offset": 11, "line": 1, "column": 12 } @@ -142,6 +162,7 @@ "css": ":nth-child(--test) {}", "error": "Unexpected identifier", "position": { + "offset": 13, "line": 1, "column": 14 } @@ -149,6 +170,7 @@ "css": ":nth-child(var(--test)) {}", "error": "Unexpected identifier", "position": { + "offset": 11, "line": 1, "column": 12 } @@ -156,6 +178,7 @@ "css": ":not(.a{)", "error": "Unexpected input", "position": { + "offset": 7, "line": 1, "column": 8 } @@ -163,6 +186,7 @@ "css": ":not(,.b)", "error": "Simple selector is expected", "position": { + "offset": 5, "line": 1, "column": 6 } @@ -170,6 +194,7 @@ "css": ":not(.a,)", "error": "Simple selector is expected", "position": { + "offset": 8, "line": 1, "column": 9 } @@ -177,6 +202,7 @@ "css": ":not(.a,,)", "error": "Simple selector is expected", "position": { + "offset": 8, "line": 1, "column": 9 } @@ -184,6 +210,7 @@ "css": ":not(.a,.b{)", "error": "Unexpected input", "position": { + "offset": 10, "line": 1, "column": 11 } @@ -191,6 +218,7 @@ "css": ":not(--test) {}", "error": "Identifier is expected", "position": { + "offset": 6, "line": 1, "column": 7 } @@ -198,6 +226,7 @@ "css": ":not(var(--test)) {}", "error": "Unexpected input", "position": { + "offset": 8, "line": 1, "column": 9 } @@ -205,6 +234,7 @@ "css": "[a+=1]{}", "error": "Attribute selector (=, ~=, ^=, $=, *=, |=) is expected", "position": { + "offset": 2, "line": 1, "column": 3 } @@ -212,6 +242,7 @@ "css": "[var(--test)=1]{}", "error": "Attribute selector (=, ~=, ^=, $=, *=, |=) is expected", "position": { + "offset": 4, "line": 1, "column": 5 } @@ -219,6 +250,7 @@ "css": "[--test=1]{}", "error": "Identifier is expected", "position": { + "offset": 2, "line": 1, "column": 3 } @@ -226,6 +258,7 @@ "css": "[--test:=1]{}", "error": "Identifier is expected", "position": { + "offset": 2, "line": 1, "column": 3 } @@ -233,6 +266,7 @@ "css": "[:--test=1]{}", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -240,6 +274,7 @@ "css": "[foo:--test=1]{}", "error": "Identifier is expected", "position": { + "offset": 6, "line": 1, "column": 7 } @@ -247,6 +282,7 @@ "css": "[", "error": "Unexpected end of input", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -254,6 +290,7 @@ "css": "]", "error": "Unexpected input", "position": { + "offset": 0, "line": 1, "column": 1 } @@ -261,6 +298,7 @@ "css": ")", "error": "Unexpected input", "position": { + "offset": 0, "line": 1, "column": 1 } @@ -268,6 +306,7 @@ "css": "}", "error": "Unexpected right curly brace", "position": { + "offset": 0, "line": 1, "column": 1 } @@ -275,6 +314,7 @@ "css": "/foo/", "error": "Identifier `deep` is expected", "position": { + "offset": 1, "line": 1, "column": 2 } @@ -282,6 +322,7 @@ "css": ".a { value: 1 !impo-tant}", "error": "Identifier `important` is expected", "position": { + "offset": 15, "line": 1, "column": 16 } @@ -289,6 +330,7 @@ "css": ".a {", "error": "RightCurlyBracket is expected", "position": { + "offset": 4, "line": 1, "column": 5 } @@ -296,6 +338,7 @@ "css": ".a { v: . }", "error": "Unexpected input", "position": { + "offset": 8, "line": 1, "column": 9 } @@ -303,6 +346,7 @@ "css": ".a {\n \n", "error": "RightCurlyBracket is expected", "position": { + "offset": 4, "line": 1, "column": 5 } @@ -310,6 +354,7 @@ "css": ".a", "error": "LeftCurlyBracket is expected", "position": { + "offset": 2, "line": 1, "column": 3 } @@ -317,6 +362,7 @@ "css": ".a \n ", "error": "LeftCurlyBracket is expected", "position": { + "offset": 2, "line": 1, "column": 3 } @@ -324,6 +370,7 @@ "css": " \n \n.a \n ", "error": "LeftCurlyBracket is expected", "position": { + "offset": 9, "line": 3, "column": 3 } @@ -331,6 +378,7 @@ "css": ".foo { var(--side): 20px }", "error": "Colon is expected", "position": { + "offset": 10, "line": 1, "column": 11 } @@ -338,6 +386,7 @@ "css": "--test { padding: 20px }", "error": "Identifier is expected", "position": { + "offset": 1, "line": 1, "column": 2 }