From d7e7334af4959a147c06b19c9a0c2f96a1b64e5c Mon Sep 17 00:00:00 2001 From: azu Date: Sat, 25 Nov 2023 14:04:13 +0900 Subject: [PATCH] fix unist node handling (#23) * fix unist node handling remove `unist` package deps * fix --- package.json | 3 +- src/StringSource.ts | 8 +-- src/UnistNode.ts | 51 +++++++++++++++++++ src/replacer.ts | 2 +- ...e-txt-test.js => StringSource-txt-test.ts} | 7 +-- 5 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 src/UnistNode.ts rename test/{StringSource-txt-test.js => StringSource-txt-test.ts} (90%) diff --git a/package.json b/package.json index 2e10cae..85e0ed2 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "scripts": { "build": "microbundle --external none", "prepublish": "npm run --if-present build", - "test": "mocha \"test/**/*.{js,ts}\"", + "typecheck": "tsc --noEmit", + "test": "npm run typecheck && mocha \"test/**/*.{js,ts}\"", "watch": "tsc -p . --watch", "format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"", "prepare": "git config --local core.hooksPath .githooks" diff --git a/src/StringSource.ts b/src/StringSource.ts index 8d4f5b7..9bbc611 100644 --- a/src/StringSource.ts +++ b/src/StringSource.ts @@ -1,10 +1,10 @@ -import type { TxtHtmlNode, TxtNode, TxtNodeLocation, TxtNodeRange } from "@textlint/ast-node-types"; +import type { TxtHtmlNode, TxtNode, TxtNodeLocation, TxtNodeRange, TxtParagraphNode } from "@textlint/ast-node-types"; import { SourcePosition, StructuredSource } from "structured-source"; -import type { Node as UnistNode } from "unist"; import unified from "unified"; // @ts-expect-error no type definition import parse from "rehype-parse"; import { emptyValue, handleReplacerCommand, maskValue, StringSourceReplacerCommand } from "./replacer"; +import { UnistNode } from "./UnistNode"; const isTxtNode = (node: unknown): node is TxtNode => { return typeof node === "object" && node !== null && "range" in node; @@ -228,7 +228,7 @@ export class StringSource { return this.originalSource.indexToPosition(originalIndex); } - isParagraphNode(node: TxtNode | StringSourceTxtParentNodeLikeNode): boolean { + isParagraphNode(node: TxtNode | StringSourceTxtParentNodeLikeNode): node is TxtParagraphNode { return node.type === "Paragraph"; } @@ -311,7 +311,7 @@ export class StringSource { //

// => container is const container = this.isParagraphNode(parent) ? newNode : parent; - const rawValue = container.raw as string | undefined; + const rawValue = "raw" in container ? container.raw : undefined; if (rawValue === undefined) { return; } diff --git a/src/UnistNode.ts b/src/UnistNode.ts new file mode 100644 index 0000000..cd201ae --- /dev/null +++ b/src/UnistNode.ts @@ -0,0 +1,51 @@ +export type UnistPoint = { + /** + * Column in a source file (1-indexed integer). + */ + column: number; + + /** + * Line in a source file (1-indexed integer). + */ + line: number; + + /** + * Character in a source file (0-indexed integer). + */ + offset?: number; +}; + +export type UnistPosition = { + /** + * Place of the first character of the parsed source region. + */ + start: UnistPoint; + + /** + * Place of the first character after the parsed source region. + */ + end: UnistPoint; + + /** + * Start column at each index (plus start line) in the source region, + * for elements that span multiple lines. + */ + indent?: number[]; +}; + +export type UnistNode = { + /** + * The variant of a node. + */ + type: string; + + /** + * Information from the ecosystem. + */ + data?: unknown | undefined; + /** + * Location of a node in a source document. + * Must not be present if a node is generated. + */ + position?: UnistPosition | undefined; +}; diff --git a/src/replacer.ts b/src/replacer.ts index 5350c89..7ebd815 100644 --- a/src/replacer.ts +++ b/src/replacer.ts @@ -1,5 +1,5 @@ import { TxtNode } from "@textlint/ast-node-types"; -import { Node as UnistNode } from "unist"; +import { UnistNode } from "./UnistNode"; export type StringSourceReplacerMaskValueCommand = { type: "StringSourceReplacerMaskValueCommand"; diff --git a/test/StringSource-txt-test.js b/test/StringSource-txt-test.ts similarity index 90% rename from test/StringSource-txt-test.js rename to test/StringSource-txt-test.ts index e8e1cca..5a366c1 100644 --- a/test/StringSource-txt-test.js +++ b/test/StringSource-txt-test.ts @@ -2,11 +2,12 @@ "use strict"; import assert from "assert"; import { StringSource } from "../src/StringSource"; +import type { TxtDocumentNode } from "@textlint/ast-node-types"; describe("StringSource AST", function () { describe("#toString", function () { it("should concat string", function () { - const AST = { + const AST: TxtDocumentNode = { type: "Document", raw: "Str", range: [0, 3], @@ -56,8 +57,8 @@ describe("StringSource AST", function () { } ] }; - let source = new StringSource(AST); - var text = source.toString(); + const source = new StringSource(AST); + const text = source.toString(); assert.equal(text + "!!", "Str!!"); }); });