From 004e1e00289dcf305410b760380433dd2c741029 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 9 Apr 2025 08:12:22 +0800 Subject: [PATCH] Replace `lines-and-columns` with `index-to-position` (#17325) --- package.json | 2 +- scripts/lint-changelog.js | 5 ++--- tests/config/utils/visualize-range.js | 14 +++++--------- yarn.lock | 17 +++++------------ 4 files changed, 13 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 1ccd5af71..284c9e35a 100644 --- a/package.json +++ b/package.json @@ -132,13 +132,13 @@ "eslint-plugin-unicorn": "58.0.0", "esm-utils": "4.3.0", "globals": "16.0.0", + "index-to-position": "1.1.0", "jest": "30.0.0-alpha.7", "jest-light-runner": "0.6.0", "jest-snapshot-serializer-ansi": "2.2.1", "jest-snapshot-serializer-raw": "2.0.0", "jest-watch-typeahead": "2.2.2", "knip": "5.47.0", - "lines-and-columns": "2.0.4", "magic-string": "0.30.17", "nano-spawn": "0.2.0", "node-style-text": "0.0.7", diff --git a/scripts/lint-changelog.js b/scripts/lint-changelog.js index 895e6c3dc..a558d149e 100644 --- a/scripts/lint-changelog.js +++ b/scripts/lint-changelog.js @@ -1,7 +1,7 @@ #!/usr/bin/env node import fs from "node:fs"; -import { LinesAndColumns } from "lines-and-columns"; +import indexToPosition from "index-to-position"; import { outdent } from "outdent"; import { CHANGELOG_CATEGORIES } from "./utils/changelog-categories.js"; @@ -142,9 +142,8 @@ for (const category of CHANGELOG_CATEGORIES) { function getCommentDescription(content, comment) { const start = content.indexOf(comment); const end = start + comment.length; - const linesAndColumns = new LinesAndColumns(content); const [startLine, endLine] = [start, end].map( - (index) => linesAndColumns.locationForIndex(index).line + 1, + (index) => indexToPosition(content, index, { oneBased: true }).line, ); if (startLine === endLine) { diff --git a/tests/config/utils/visualize-range.js b/tests/config/utils/visualize-range.js index 222bc7a6f..220404a1a 100644 --- a/tests/config/utils/visualize-range.js +++ b/tests/config/utils/visualize-range.js @@ -1,5 +1,5 @@ import { codeFrameColumns } from "@babel/code-frame"; -import { LinesAndColumns } from "lines-and-columns"; +import indexToPosition from "index-to-position"; const codeFrameColumnsOptions = { linesAbove: Number.POSITIVE_INFINITY, linesBelow: Number.POSITIVE_INFINITY, @@ -9,15 +9,11 @@ const locationForRange = (text, rangeStart, rangeEnd) => { if (rangeStart > rangeEnd) { [rangeStart, rangeEnd] = [rangeEnd, rangeStart]; } - const lines = new LinesAndColumns(text); - const start = lines.locationForIndex(rangeStart); - const end = lines.locationForIndex(rangeEnd); - start.line += 1; - start.column += 1; - end.line += 1; - if (start.line === end.line) { - end.column += 1; + const start = indexToPosition(text, rangeStart, { oneBased: true }); + const end = indexToPosition(text, rangeEnd, { oneBased: true }); + if (start.line !== end.line) { + end.column -= 1; } return { diff --git a/yarn.lock b/yarn.lock index a8149c659..79ffd6061 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4798,10 +4798,10 @@ __metadata: languageName: node linkType: hard -"index-to-position@npm:^1.0.0": - version: 1.0.0 - resolution: "index-to-position@npm:1.0.0" - checksum: 10/4a24b8f0728da8ec6ffea85556acf809a71317d5291d31148db7c2dececa10432abf3c87a932481a2e326af66221bcf77d8dc1f0abfd11537e7f3cc21432a525 +"index-to-position@npm:1.1.0, index-to-position@npm:^1.0.0": + version: 1.1.0 + resolution: "index-to-position@npm:1.1.0" + checksum: 10/16703893c732a025786098fe77cb7e83109afe4b72633dd6feea1595c54f8406623fa7a0a2263a8e08adee7f639fbb1c4731982cd30b4bc30d787bf803f5f3d8 languageName: node linkType: hard @@ -5959,13 +5959,6 @@ __metadata: languageName: node linkType: hard -"lines-and-columns@npm:2.0.4": - version: 2.0.4 - resolution: "lines-and-columns@npm:2.0.4" - checksum: 10/81ac2f943f5428a46bd4ea2561c74ba674a107d8e6cc70cd317d16892a36ff3ba0dc6e599aca8b6f8668d26c85288394c6edf7a40e985ca843acab3701b80d4c - languageName: node - linkType: hard - "lines-and-columns@npm:^1.1.6": version: 1.2.4 resolution: "lines-and-columns@npm:1.2.4" @@ -6911,6 +6904,7 @@ __metadata: html-ua-styles: "npm:0.0.8" ignore: "npm:7.0.3" import-meta-resolve: "npm:4.1.0" + index-to-position: "npm:1.1.0" is-es5-identifier-name: "npm:1.0.0" iterate-directory-up: "npm:1.1.1" jest: "npm:30.0.0-alpha.7" @@ -6923,7 +6917,6 @@ __metadata: json5: "npm:2.2.3" knip: "npm:5.47.0" leven: "npm:4.0.0" - lines-and-columns: "npm:2.0.4" linguist-languages: "npm:7.29.0" magic-string: "npm:0.30.17" meriyah: "npm:6.0.6"