From aa1405de1be13435b4647c7df593dae0efb033f3 Mon Sep 17 00:00:00 2001 From: Romain Menke <11521496+romainmenke@users.noreply.github.com> Date: Thu, 16 Jan 2025 22:32:04 +0100 Subject: [PATCH] Integrate `Input#document` (#36) --- lib/__tests__/parse.test.js | 40 +++++++++++++++++++++++++++++++++++++ lib/parse.js | 8 ++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/__tests__/parse.test.js b/lib/__tests__/parse.test.js index 03728b3..1388bf6 100644 --- a/lib/__tests__/parse.test.js +++ b/lib/__tests__/parse.test.js @@ -40,6 +40,26 @@ describe('no interpolations', () => { line: 1, column: 38, }); + expect(decl.rangeBy({})).toEqual({ + start: { + column: 28, + line: 1, + }, + end: { + column: 39, + line: 1, + }, + }); + expect(decl.rangeBy({ word: 'red' })).toEqual({ + start: { + column: 35, + line: 1, + }, + end: { + column: 38, + line: 1, + }, + }); }); test('two components', () => { @@ -111,6 +131,26 @@ describe('no interpolations', () => { line: 2, column: 45, }); + expect(secondComponent.rangeBy({})).toEqual({ + start: { + column: 28, + line: 2, + }, + end: { + column: 47, + line: 2, + }, + }); + expect(secondComponent.rangeBy({ word: 'blue' })).toEqual({ + start: { + column: 42, + line: 2, + }, + end: { + column: 46, + line: 2, + }, + }); }); test('empty component', () => { diff --git a/lib/parse.js b/lib/parse.js index def43cd..8e69394 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -8,9 +8,13 @@ const { parseJs } = require('./parseJs'); module.exports = function parse(css, opts) { let inputCode = typeof css === 'string' ? css : css.toString(); + let options = opts ?? {}; + + options.document = inputCode; + let document = new postcss.Document({ source: { - input: new postcss.Input(inputCode, opts), + input: new postcss.Input(inputCode, options), start: { offset: 0, line: 1, column: 1 }, }, }); @@ -27,7 +31,7 @@ module.exports = function parse(css, opts) { /** @type {postcss.Root} */ let parsedNode; - let input = new postcss.Input(node.css, opts); + let input = new postcss.Input(node.css, options); let interpolationsRanges = node.interpolationRanges.map((range) => { return {