From 5e4dd428de41e7ddc013b1ab764a92a03c9d282d Mon Sep 17 00:00:00 2001 From: essentia0 Date: Wed, 7 Jul 2021 12:01:03 +0200 Subject: [PATCH 1/2] fix: source map (following #150) --- lib/LessParser.js | 5 ++-- lib/nodes/inline-comment.js | 2 +- lib/nodes/interpolation.js | 4 +--- test/map.test.js | 47 +++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 test/map.test.js diff --git a/lib/LessParser.js b/lib/LessParser.js index 60af9c0..f8597e3 100644 --- a/lib/LessParser.js +++ b/lib/LessParser.js @@ -67,9 +67,8 @@ module.exports = class LessParser extends Parser { const node = new Comment(); const text = token[1].slice(2); - this.init(node, token[2], token[3]); - - node.source.end = { line: token[4], column: token[5] }; + this.init(node, token[2]); + node.source.end = this.getPosition(token[3] || token[2]); node.inline = true; node.raws.begin = '//'; diff --git a/lib/nodes/inline-comment.js b/lib/nodes/inline-comment.js index bb79142..0adb70b 100644 --- a/lib/nodes/inline-comment.js +++ b/lib/nodes/inline-comment.js @@ -33,7 +33,7 @@ module.exports = { token = this.tokenizer.nextToken({ ignoreUnclosed: true }); } - const newToken = ['comment', bits.join(''), first[2], first[3], last[2], last[3]]; + const newToken = ['comment', bits.join(''), first[2], last[2]]; this.inlineComment(newToken); // Replace tokenizer to retokenize the rest of the string diff --git a/lib/nodes/interpolation.js b/lib/nodes/interpolation.js index b3fd4ad..0126a37 100644 --- a/lib/nodes/interpolation.js +++ b/lib/nodes/interpolation.js @@ -22,9 +22,7 @@ module.exports = { const words = tokens.map((tokn) => tokn[1]); [first] = tokens; const last = tokens.pop(); - const start = [first[2], first[3]]; - const end = [last[4] || last[2], last[5] || last[3]]; - const newToken = ['word', words.join('')].concat(start, end); + const newToken = ['word', words.join(''), first[2], last[2]]; this.tokenizer.back(token); this.tokenizer.back(newToken); diff --git a/test/map.test.js b/test/map.test.js new file mode 100644 index 0000000..aef8de1 --- /dev/null +++ b/test/map.test.js @@ -0,0 +1,47 @@ +const test = require("ava"); +const postcss = require("postcss"); +const { readFileSync } = require("fs"); +const { join } = require("path"); + +const syntax = require("../lib"); + +const { parser } = syntax; + +// silence the rediculously verbose "You did not set any plugins, parser, or +// stringifier" warnings in PostCSS. +console.warn = () => {}; // eslint-disable-line no-console + +test("should parse LESS integration syntax and generate a source map", async t => { + const less = readFileSync( + join(__dirname, "./integration/ext.cx.dashboard.less"), + "utf-8" + ); + const result = await postcss().process(less, { + syntax, + parser, + map: { inline: false, annotation: false, sourcesContent: true } + }); + + t.truthy(result); + t.is(result.css, less); + t.is(result.content, less); + t.truthy(result.map); +}); + +test("should parse LESS inline comment syntax and generate a source map", async t => { + const less = ` + a { + //background-color: red; + } + `; + const result = await postcss().process(less, { + syntax, + parser, + map: { inline: false, annotation: false, sourcesContent: true } + }); + + t.truthy(result); + t.is(result.css, less); + t.is(result.content, less); + t.truthy(result.map); +}); From 9aa87a6553f393ade131c8640335e33bbb6a9b3f Mon Sep 17 00:00:00 2001 From: essentia0 Date: Wed, 7 Jul 2021 14:52:24 +0200 Subject: [PATCH 2/2] lint map.test.js --- test/map.test.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/map.test.js b/test/map.test.js index aef8de1..0037768 100644 --- a/test/map.test.js +++ b/test/map.test.js @@ -1,9 +1,11 @@ -const test = require("ava"); -const postcss = require("postcss"); -const { readFileSync } = require("fs"); -const { join } = require("path"); +const { readFileSync } = require('fs'); -const syntax = require("../lib"); +const { join } = require('path'); + +const test = require('ava'); +const postcss = require('postcss'); + +const syntax = require('../lib'); const { parser } = syntax; @@ -11,11 +13,8 @@ const { parser } = syntax; // stringifier" warnings in PostCSS. console.warn = () => {}; // eslint-disable-line no-console -test("should parse LESS integration syntax and generate a source map", async t => { - const less = readFileSync( - join(__dirname, "./integration/ext.cx.dashboard.less"), - "utf-8" - ); +test('should parse LESS integration syntax and generate a source map', async (t) => { + const less = readFileSync(join(__dirname, './integration/ext.cx.dashboard.less'), 'utf-8'); const result = await postcss().process(less, { syntax, parser, @@ -28,7 +27,7 @@ test("should parse LESS integration syntax and generate a source map", async t = t.truthy(result.map); }); -test("should parse LESS inline comment syntax and generate a source map", async t => { +test('should parse LESS inline comment syntax and generate a source map', async (t) => { const less = ` a { //background-color: red;