From 422b5bfe8363795fabc9600c2b98205847d7013d Mon Sep 17 00:00:00 2001 From: Gusted Date: Wed, 21 Jul 2021 16:33:08 +0200 Subject: [PATCH] Return correct column on `missed semicolon` I've added some test-cases to ensure it works. Also added a comment about `why` we it need to get token[3]+1 And ultimately it resolves https://github.com/postcss/postcss/issues/1617 Regards, Gusted --- lib/parser.js | 8 +++++++- test/parse.test.ts | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/parser.js b/lib/parser.js index 2b88483b1..4144cc92d 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -552,7 +552,13 @@ class Parser { if (founded === 2) break } } - throw this.input.error('Missed semicolon', token[2]) + // If the token is a word, e.g. `!important`, `red` or any other valid property's value. + // Then we need to return the colon after that word token. [3] is the "end" colon of that word. + // And because we need it after that one we do +1 to get the next one. + throw this.input.error( + 'Missed semicolon', + token[0] === 'word' ? token[3] + 1 : token[2] + ) } } diff --git a/test/parse.test.ts b/test/parse.test.ts index 6ec282f72..c2455f723 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -203,3 +203,23 @@ it('suggests postcss-less for Less sources', () => { parse('.@{my-selector} { }', { from: 'app.less' }) }).toThrow(/postcss-less/) }) + +it('should give the correct column of missed semicolon with !important', () => { + let error + try { + parse('a { \n color: red !important\n background-color: black;\n}') + } catch (e) { + error = e + } + expect(error.message).toMatch(/2:26: Missed semicolon/) +}) + +it('should give the correct column of missed semicolon without !important', () => { + let error + try { + parse('a { \n color: red\n background-color: black;\n}') + } catch (e) { + error = e + } + expect(error.message).toMatch(/2:15: Missed semicolon/) +})