From 96d7e76e789630bfa53e9eba72f3dfd5fb269e10 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 21 Nov 2020 19:15:57 +0300 Subject: [PATCH 1/2] Replace line-column with vfile-location vfile-location is dependency free and quite fresh package. https://github.com/vfile/vfile-location line-column has outdated dependencies and was not touched in 5 years. https://github.com/io-monad/line-column --- lib/input.d.ts | 4 +++- lib/input.js | 8 ++++---- lib/parser.js | 7 +------ package.json | 4 ++-- yarn.lock | 5 +++++ 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/input.d.ts b/lib/input.d.ts index f6d603ce4..8d086b26e 100644 --- a/lib/input.d.ts +++ b/lib/input.d.ts @@ -126,5 +126,7 @@ export default class Input { * * @param offset Source offset. */ - fromOffset (offset: number): { line: number; col: number } | null + fromOffset ( + offset: number + ): { offset?: number; line?: number; column?: number } } diff --git a/lib/input.js b/lib/input.js index 4c754d14c..1a81529cc 100644 --- a/lib/input.js +++ b/lib/input.js @@ -3,7 +3,7 @@ let { fileURLToPath, pathToFileURL } = require('url') let { resolve, isAbsolute } = require('path') let { nanoid } = require('nanoid/non-secure') -let lineColumn = require('line-column') +let vfileLocation = require('vfile-location') let terminalHighlight = require('./terminal-highlight') let CssSyntaxError = require('./css-syntax-error') @@ -50,8 +50,8 @@ class Input { } fromOffset (offset) { - let finder = lineColumn(this.css) - this.fromOffset = i => finder.fromIndex(i) + let finder = vfileLocation(this.css) + this.fromOffset = i => finder.toPoint(i) return this.fromOffset(offset) } @@ -60,7 +60,7 @@ class Input { if (!column) { let pos = this.fromOffset(line) line = pos.line - column = pos.col + column = pos.column } let origin = this.origin(line, column) if (origin) { diff --git a/lib/parser.js b/lib/parser.js index 0fcfd50f8..6eeb8ed0d 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -376,12 +376,7 @@ class Parser { // Helpers getPosition (offset) { - let pos = this.input.fromOffset(offset) - return { - offset, - line: pos.line, - column: pos.col - } + return this.input.fromOffset(offset) } init (node, offset) { diff --git a/package.json b/package.json index 4bea315b4..3c1f1a4d1 100644 --- a/package.json +++ b/package.json @@ -39,9 +39,9 @@ "repository": "postcss/postcss", "dependencies": { "colorette": "^1.2.1", - "line-column": "^1.0.2", "nanoid": "^3.1.16", - "source-map": "^0.6.1" + "source-map": "^0.6.1", + "vfile-location": "^3.2.0" }, "devDependencies": { "@logux/eslint-config": "^42.2.2", diff --git a/yarn.lock b/yarn.lock index 46f48175e..c0c65f6a2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11037,6 +11037,11 @@ vfile-location@^2.0.0: resolved "https://registry.yarnpkg.com/vfile-location/-/vfile-location-2.0.6.tgz#8a274f39411b8719ea5728802e10d9e0dff1519e" integrity sha512-sSFdyCP3G6Ka0CEmN83A2YCMKIieHx0EDaj5IDP4g1pa5ZJ4FJDvpO0WODLxo4LUX4oe52gmSCK7Jw4SBghqxA== +vfile-location@^3.2.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/vfile-location/-/vfile-location-3.2.0.tgz#d8e41fbcbd406063669ebf6c33d56ae8721d0f3c" + integrity sha512-aLEIZKv/oxuCDZ8lkJGhuhztf/BW4M+iHdCwglA/eWc+vtuRFJj8EtgceYFX4LRjOhCAAiNHsKGssC6onJ+jbA== + vfile-message@^1.0.0: version "1.1.1" resolved "https://registry.yarnpkg.com/vfile-message/-/vfile-message-1.1.1.tgz#5833ae078a1dfa2d96e9647886cd32993ab313e1" From 3ef06ec6c993c8b447a84478ce0d64cb7960cf26 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 21 Nov 2020 21:05:46 +0300 Subject: [PATCH 2/2] Convert object --- lib/input.d.ts | 4 +--- lib/input.js | 10 ++++++++-- lib/parser.js | 7 ++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/input.d.ts b/lib/input.d.ts index 8d086b26e..f6d603ce4 100644 --- a/lib/input.d.ts +++ b/lib/input.d.ts @@ -126,7 +126,5 @@ export default class Input { * * @param offset Source offset. */ - fromOffset ( - offset: number - ): { offset?: number; line?: number; column?: number } + fromOffset (offset: number): { line: number; col: number } | null } diff --git a/lib/input.js b/lib/input.js index 1a81529cc..0bbfd69b1 100644 --- a/lib/input.js +++ b/lib/input.js @@ -51,7 +51,13 @@ class Input { fromOffset (offset) { let finder = vfileLocation(this.css) - this.fromOffset = i => finder.toPoint(i) + this.fromOffset = i => { + let position = finder.toPoint(i) + return { + line: position.line, + col: position.column + } + } return this.fromOffset(offset) } @@ -60,7 +66,7 @@ class Input { if (!column) { let pos = this.fromOffset(line) line = pos.line - column = pos.column + column = pos.col } let origin = this.origin(line, column) if (origin) { diff --git a/lib/parser.js b/lib/parser.js index 6eeb8ed0d..0fcfd50f8 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -376,7 +376,12 @@ class Parser { // Helpers getPosition (offset) { - return this.input.fromOffset(offset) + let pos = this.input.fromOffset(offset) + return { + offset, + line: pos.line, + column: pos.col + } } init (node, offset) {