Skip to content

Commit

Permalink
fix unist node handling (#23)
Browse files Browse the repository at this point in the history
* fix unist node handling

remove `unist` package deps

* fix
  • Loading branch information
azu authored Nov 25, 2023
1 parent 34f9728 commit d7e7334
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions src/StringSource.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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";
}

Expand Down Expand Up @@ -311,7 +311,7 @@ export class StringSource {
// <p><strong><Str /></strong></p>
// => container is <strong>
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;
}
Expand Down
51 changes: 51 additions & 0 deletions src/UnistNode.ts
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 1 addition & 1 deletion src/replacer.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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!!");
});
});
Expand Down

0 comments on commit d7e7334

Please sign in to comment.