Skip to content

Commit d7e7334

Browse files
authored
fix unist node handling (#23)
* fix unist node handling remove `unist` package deps * fix
1 parent 34f9728 commit d7e7334

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"scripts": {
2929
"build": "microbundle --external none",
3030
"prepublish": "npm run --if-present build",
31-
"test": "mocha \"test/**/*.{js,ts}\"",
31+
"typecheck": "tsc --noEmit",
32+
"test": "npm run typecheck && mocha \"test/**/*.{js,ts}\"",
3233
"watch": "tsc -p . --watch",
3334
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
3435
"prepare": "git config --local core.hooksPath .githooks"

src/StringSource.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { TxtHtmlNode, TxtNode, TxtNodeLocation, TxtNodeRange } from "@textlint/ast-node-types";
1+
import type { TxtHtmlNode, TxtNode, TxtNodeLocation, TxtNodeRange, TxtParagraphNode } from "@textlint/ast-node-types";
22
import { SourcePosition, StructuredSource } from "structured-source";
3-
import type { Node as UnistNode } from "unist";
43
import unified from "unified";
54
// @ts-expect-error no type definition
65
import parse from "rehype-parse";
76
import { emptyValue, handleReplacerCommand, maskValue, StringSourceReplacerCommand } from "./replacer";
7+
import { UnistNode } from "./UnistNode";
88

99
const isTxtNode = (node: unknown): node is TxtNode => {
1010
return typeof node === "object" && node !== null && "range" in node;
@@ -228,7 +228,7 @@ export class StringSource {
228228
return this.originalSource.indexToPosition(originalIndex);
229229
}
230230

231-
isParagraphNode(node: TxtNode | StringSourceTxtParentNodeLikeNode): boolean {
231+
isParagraphNode(node: TxtNode | StringSourceTxtParentNodeLikeNode): node is TxtParagraphNode {
232232
return node.type === "Paragraph";
233233
}
234234

@@ -311,7 +311,7 @@ export class StringSource {
311311
// <p><strong><Str /></strong></p>
312312
// => container is <strong>
313313
const container = this.isParagraphNode(parent) ? newNode : parent;
314-
const rawValue = container.raw as string | undefined;
314+
const rawValue = "raw" in container ? container.raw : undefined;
315315
if (rawValue === undefined) {
316316
return;
317317
}

src/UnistNode.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export type UnistPoint = {
2+
/**
3+
* Column in a source file (1-indexed integer).
4+
*/
5+
column: number;
6+
7+
/**
8+
* Line in a source file (1-indexed integer).
9+
*/
10+
line: number;
11+
12+
/**
13+
* Character in a source file (0-indexed integer).
14+
*/
15+
offset?: number;
16+
};
17+
18+
export type UnistPosition = {
19+
/**
20+
* Place of the first character of the parsed source region.
21+
*/
22+
start: UnistPoint;
23+
24+
/**
25+
* Place of the first character after the parsed source region.
26+
*/
27+
end: UnistPoint;
28+
29+
/**
30+
* Start column at each index (plus start line) in the source region,
31+
* for elements that span multiple lines.
32+
*/
33+
indent?: number[];
34+
};
35+
36+
export type UnistNode = {
37+
/**
38+
* The variant of a node.
39+
*/
40+
type: string;
41+
42+
/**
43+
* Information from the ecosystem.
44+
*/
45+
data?: unknown | undefined;
46+
/**
47+
* Location of a node in a source document.
48+
* Must not be present if a node is generated.
49+
*/
50+
position?: UnistPosition | undefined;
51+
};

src/replacer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TxtNode } from "@textlint/ast-node-types";
2-
import { Node as UnistNode } from "unist";
2+
import { UnistNode } from "./UnistNode";
33

44
export type StringSourceReplacerMaskValueCommand = {
55
type: "StringSourceReplacerMaskValueCommand";

test/StringSource-txt-test.js renamed to test/StringSource-txt-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"use strict";
33
import assert from "assert";
44
import { StringSource } from "../src/StringSource";
5+
import type { TxtDocumentNode } from "@textlint/ast-node-types";
56

67
describe("StringSource AST", function () {
78
describe("#toString", function () {
89
it("should concat string", function () {
9-
const AST = {
10+
const AST: TxtDocumentNode = {
1011
type: "Document",
1112
raw: "Str",
1213
range: [0, 3],
@@ -56,8 +57,8 @@ describe("StringSource AST", function () {
5657
}
5758
]
5859
};
59-
let source = new StringSource(AST);
60-
var text = source.toString();
60+
const source = new StringSource(AST);
61+
const text = source.toString();
6162
assert.equal(text + "!!", "Str!!");
6263
});
6364
});

0 commit comments

Comments
 (0)