Skip to content

Commit

Permalink
fix: source map generation (following #150) (#161)
Browse files Browse the repository at this point in the history
* fix: source map (following #150)

* lint map.test.js

Co-authored-by: essentia0 <contact@essentia0.me>
  • Loading branch information
essentia0 and essentia0 authored Jul 12, 2021
1 parent 20d1e57 commit b6fd291
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
5 changes: 2 additions & 3 deletions lib/LessParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '//';

Expand Down
2 changes: 1 addition & 1 deletion lib/nodes/inline-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/nodes/interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
46 changes: 46 additions & 0 deletions test/map.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { readFileSync } = require('fs');

const { join } = require('path');

const test = require('ava');
const postcss = require('postcss');

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);
});

0 comments on commit b6fd291

Please sign in to comment.