Skip to content

fix(deps): update @textlint/ast-node-types and refactor types #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14, 16, 18]
node-version: [18, 20]
steps:
- name: checkout
uses: actions/checkout@v3
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@
"prepare": "git config --local core.hooksPath .githooks"
},
"dependencies": {
"@textlint/ast-node-types": "^13.0.5",
"@textlint/ast-node-types": "^13.4.1",
"rehype-parse": "^6.0.1",
"structured-source": "^4.0.0",
"unified": "^8.4.0"
},
"devDependencies": {
"@textlint/markdown-to-ast": "^13.2.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@textlint/markdown-to-ast": "^13.4.1",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.0",
"cross-env": "^7.0.3",
"lint-staged": "^13.1.0",
"lint-staged": "^15.1.0",
"microbundle": "^0.15.1",
"mocha": "^10.2.0",
"prettier": "^2.8.3",
"sentence-splitter": "^4.1.0",
"prettier": "^3.1.0",
"sentence-splitter": "^5.0.0",
"ts-node": "^10.9.1",
"ts-node-test-register": "^10.0.0",
"typescript": "^4.9.4"
"typescript": "^5.3.2"
},
"email": "[email protected]",
"prettier": {
Expand Down
58 changes: 45 additions & 13 deletions src/StringSource.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TxtNode, TxtParentNode } from "@textlint/ast-node-types";
import type { TxtHtmlNode, TxtNode, TxtNodeLocation, TxtNodeRange } from "@textlint/ast-node-types";
import { SourcePosition, StructuredSource } from "structured-source";
import type { Node as UnistNode } from "unist";
import unified from "unified";
Expand All @@ -9,13 +9,17 @@ import { emptyValue, handleReplacerCommand, maskValue, StringSourceReplacerComma
const isTxtNode = (node: unknown): node is TxtNode => {
return typeof node === "object" && node !== null && "range" in node;
};

const isHtmlNode = (node: StringSourceTxtParentNodeLikeNode | StringSourceTxtTxtNode): node is TxtHtmlNode => {
return node.type === "Html";
};
const htmlProcessor = unified().use(parse, { fragment: true });
const html2hast = (html: string) => {
return htmlProcessor.parse(html);
};

const isParentNode = (node: TxtNode | StringSourceTxtParentNodeLikeNode): node is StringSourceTxtParentNodeLikeNode => {
const isParentNode = (
node: UnistNode | TxtNode | StringSourceTxtParentNodeLikeNode
): node is StringSourceTxtParentNodeLikeNode => {
return "children" in node;
};

Expand Down Expand Up @@ -53,7 +57,28 @@ export type StringSourceOptions = {
emptyValue: typeof emptyValue;
}) => StringSourceReplacerCommand | undefined;
};
export type StringSourceTxtParentNodeLikeNode = TxtParentNode | (Omit<TxtParentNode, "type"> & { type: string });
/**
* TxtTxtNode-like definition
* It is intentionally loose definition to accept sentences-splitter's node and unist node.
*/
export type StringSourceTxtTxtNode = {
type: string;
raw: string;
range: TxtNodeRange;
loc: TxtNodeLocation;
value?: string | null | undefined;
};
/**
* TxtParentNode-like definition
* It is intentionally loose definition to accept sentences-splitter's node and unist node.
*/
export type StringSourceTxtParentNodeLikeNode = {
type: string;
raw: string;
range: TxtNodeRange;
loc: TxtNodeLocation;
children: (StringSourceTxtTxtNode | StringSourceTxtParentNodeLikeNode)[];
};

export class StringSource {
private rootNode: StringSourceTxtParentNodeLikeNode;
Expand Down Expand Up @@ -218,15 +243,19 @@ export class StringSource {
* @private
*/
private _getValue(node: TxtNode | UnistNode): string | undefined {
if (node.value) {
if ("value" in node && typeof node.value === "string") {
return node.value;
} else if (node.alt) {
} else if ("alt" in node && typeof node.alt === "string") {
return node.alt;
} else if (node.title) {
} else if ("title" in node) {
// Ignore link title e.g.) [text](url "title")
// See https://github.com/azu/textlint-rule-sentence-length/issues/6
if (node.type === "Link") {
return;
}
if (typeof node.title !== "string") {
return;
}
return node.title;
} else {
return;
Expand Down Expand Up @@ -330,24 +359,27 @@ export class StringSource {
parent,
options
}: {
node: TxtNode | StringSourceTxtParentNodeLikeNode;
node: StringSourceTxtParentNodeLikeNode | StringSourceTxtTxtNode;
parent?: StringSourceTxtParentNodeLikeNode;
options: StringSourceOptions;
}): void | StringSourceIR {
const isHTML = node.type === "Html";
const currentNode = isHTML ? html2hast(node.value) : node;
const currentNode = isHtmlNode(node) ? html2hast(node.value) : node;
const value = this._valueOf({ node: currentNode, parent: parent, options });
if (value) {
return value;
}
if (!isParentNode(node)) {
if (!isParentNode(currentNode)) {
return;
}
currentNode.children.forEach((childNode: TxtNode) => {
currentNode.children.forEach((childNode) => {
if (!isParentNode(node)) {
return;
}
const tokenMap = this._stringify({ node: childNode, parent: node, options });
const tokenMap = this._stringify({
node: childNode,
parent: node,
options
});
if (tokenMap) {
this._addTokenMap(tokenMap);
}
Expand Down
4 changes: 2 additions & 2 deletions src/replacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ export const handleReplacerCommand = <T extends TxtNode | UnistNode>(
return node;
}
if (command.type === "StringSourceReplacerEmptyValueCommand") {
if (node.value === undefined) {
if (!("value" in node)) {
return node;
}
return {
...node,
value: ""
};
} else if (command.type === "StringSourceReplacerMaskValueCommand") {
if (node.value === undefined) {
if (!("value" in node) || typeof node.value !== "string") {
throw new Error(
`Can not masking. ${node.type} node does not have value property: ` + JSON.stringify(node, null, 4)
);
Expand Down
7 changes: 5 additions & 2 deletions test/integration-sentence-splitter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { split } from "sentence-splitter";
import { split, TxtSentenceNode } from "sentence-splitter";
import { StringSource } from "../src/StringSource";
import assert from "assert";

describe("sentence-splitter", function () {
it("should handle TxtSentenceNode without compile error", function () {
const [sentence1, _whitespace, sentence2] = split("Hello world. This is a test.");
const results = split("Hello world. This is a test.");
const [sentence1, sentence2] = results.filter((r) => {
return r.type === "Sentence";
}) as TxtSentenceNode[];
assert.strictEqual(new StringSource(sentence1).toString(), "Hello world.");
assert.strictEqual(new StringSource(sentence2).toString(), "This is a test.");
});
Expand Down
Loading