Skip to content

Commit

Permalink
refactor(markdown-to-ast): Convert tests to TypeScript (#766)
Browse files Browse the repository at this point in the history
* refactor(markdown-to-ast): Convert tests to TypeScript

* chore: use ignore

we need to use strip comments
  • Loading branch information
azu committed May 12, 2021
1 parent a79a7b1 commit ac95d63
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 54 deletions.
11 changes: 5 additions & 6 deletions packages/@textlint/markdown-to-ast/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// LICENSE : MIT
"use strict";
import { SyntaxMap } from "./mapping/markdown-syntax-map";
import { ASTNodeTypes, TxtNode } from "@textlint/ast-node-types";
import traverse from "traverse";
import StructuredSource from "structured-source";
import debug0 from "debug";
// @ts-ignore
// @ts-ignore: no types
import unified from "unified";
// @ts-ignore
// @ts-ignore: no types
import remarkParse from "remark-parse";
// @ts-ignore
// @ts-ignore: no types
import frontmatter from "remark-frontmatter";

const debug = debug0("@textlint/markdown-to-ast");
const remark = unified().use(remarkParse).use(frontmatter, ["yaml"]);

Expand Down Expand Up @@ -56,5 +55,5 @@ export function parse<T extends TxtNode>(text: string): T {
}
}
});
return ast;
return ast as T;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// LICENSE : MIT
"use strict";
const assert = require("assert");
const parse = require("../src/index").parse;
const Syntax = require("../src/index").Syntax;
const inspect = (obj) => JSON.stringify(obj, null, 4);
const traverse = require("traverse");
function findFirstTypedNode(node, type, value) {
let result = null;
import { TxtNode } from "@textlint/ast-node-types";
import { parse, Syntax } from "../src/index";
import assert from "assert";
import traverse from "traverse";

const inspect = (obj: Record<string, unknown>) => JSON.stringify(obj, null, 4);

function findFirstTypedNode(node: TxtNode, type: string, value?: string): TxtNode {
let result: TxtNode | null = null;
traverse(node).forEach(function (x) {
// eslint-disable-next-line no-invalid-this
if (this.notLeaf) {
Expand All @@ -21,18 +23,18 @@ function findFirstTypedNode(node, type, value) {
});
if (result == null) {
/* eslint-disable no-console */
console.log(`Not Found type:${type}`);
console.log(inspect(node));
throw new Error(`Not Found type:${type}`);
/* eslint-enable no-console */
}
return result;
}

function shouldHaveImplementTxtNode(node, rawValue) {
function shouldHaveImplementTxtNode(node: TxtNode, rawValue: string) {
const lines = rawValue.split("\n");
const lastLine = lines[lines.length - 1];
assert.equal(node.raw, rawValue);
assert.deepEqual(node.loc, {
assert.strictEqual(node.raw, rawValue);
assert.deepStrictEqual(node.loc, {
start: {
line: 1,
column: 0
Expand All @@ -42,12 +44,13 @@ function shouldHaveImplementTxtNode(node, rawValue) {
column: lastLine.length
}
});
assert.deepEqual(node.range, [0, rawValue.length]);
assert.deepStrictEqual(node.range, [0, rawValue.length]);
}
function shouldHaveImplementInlineTxtNode(node, text, allText) {
assert.equal(node.raw, text);

function shouldHaveImplementInlineTxtNode(node: TxtNode, text: string, allText: string) {
assert.strictEqual(node.raw, text);
const startColumn = allText.indexOf(text);
assert.deepEqual(node.loc, {
assert.deepStrictEqual(node.loc, {
start: {
line: 1,
column: startColumn
Expand All @@ -57,8 +60,9 @@ function shouldHaveImplementInlineTxtNode(node, text, allText) {
column: startColumn + text.length
}
});
assert.deepEqual(node.range, [startColumn, startColumn + text.length]);
assert.deepStrictEqual(node.range, [startColumn, startColumn + text.length]);
}

/*
NOTE:
`line` start with 1
Expand All @@ -69,18 +73,18 @@ describe("markdown-parser", function () {
context("Node type is Document", function () {
it("should has implemented TxtNode", function () {
const RootDocument = parse("");
assert.equal(RootDocument.type, Syntax.Document);
assert.equal(RootDocument.raw, "");
assert.deepEqual(RootDocument.loc, { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } });
assert.deepEqual(RootDocument.range, [0, 0]);
assert.strictEqual(RootDocument.type, Syntax.Document);
assert.strictEqual(RootDocument.raw, "");
assert.deepStrictEqual(RootDocument.loc, { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } });
assert.deepStrictEqual(RootDocument.range, [0, 0]);
});
it("should has range and loc on whole text", function () {
const text = "# Header\n\n" + "- list\n\n" + "text Str.";
const lines = text.split("\n");
const RootDocument = parse(text);
assert.equal(RootDocument.type, Syntax.Document);
assert.equal(RootDocument.raw, text);
assert.deepEqual(RootDocument.loc, {
assert.strictEqual(RootDocument.type, Syntax.Document);
assert.strictEqual(RootDocument.raw, text);
assert.deepStrictEqual(RootDocument.loc, {
start: {
line: 1,
column: 0
Expand All @@ -90,15 +94,15 @@ describe("markdown-parser", function () {
column: lines[lines.length - 1].length
}
});
assert.deepEqual(RootDocument.range, [0, text.length]);
assert.deepStrictEqual(RootDocument.range, [0, text.length]);
});
it("should has range and loc on whole text", function () {
const text = "# Header\n" + "\n" + "text";
const lines = text.split("\n");
const RootDocument = parse(text);
assert.equal(RootDocument.type, Syntax.Document);
assert.equal(RootDocument.raw, text);
assert.deepEqual(RootDocument.loc, {
assert.strictEqual(RootDocument.type, Syntax.Document);
assert.strictEqual(RootDocument.raw, text);
assert.deepStrictEqual(RootDocument.loc, {
start: {
line: 1,
column: 0
Expand All @@ -108,15 +112,15 @@ describe("markdown-parser", function () {
column: lines[lines.length - 1].length
}
});
const slicedText = text.slice(RootDocument.range);
assert.deepEqual(slicedText, text);
const slicedText = text.slice(RootDocument.range[0], RootDocument.range[1]);
assert.deepStrictEqual(slicedText, text);
});
});
/*
Paragraph > Str
*/
context("Node type is Paragraph", function () {
let AST, rawValue;
let AST: TxtNode, rawValue: string;
beforeEach(function () {
rawValue = "string";
AST = parse(rawValue);
Expand All @@ -143,7 +147,7 @@ describe("markdown-parser", function () {
* =====
**/
context("SetextHeader", function () {
let AST, text, header;
let AST: TxtNode, text: string, header: string;
beforeEach(function () {
text = "string";
header = `${text}\n======`;
Expand All @@ -166,7 +170,7 @@ describe("markdown-parser", function () {
* # text
* */
context("ATXHeader", function () {
let AST, text, header;
let AST: TxtNode, text: string, header: string;
beforeEach(function () {
text = "string";
header = `# ${text}`;
Expand All @@ -187,7 +191,7 @@ describe("markdown-parser", function () {
});
});
context("Node type is Link", function () {
let AST, rawValue, labelText;
let AST: TxtNode, rawValue: string, labelText: string;
beforeEach(function () {
labelText = "text";
rawValue = `[${labelText}](http://example.com)`;
Expand All @@ -214,7 +218,7 @@ describe("markdown-parser", function () {
});
context("Node type is ListItem", function () {
it("should same the bullet_char", function () {
let node, AST;
let node: TxtNode, AST: TxtNode;
AST = parse("- item");
node = findFirstTypedNode(AST, Syntax.ListItem);
assert(/^-/.test(node.raw));
Expand All @@ -226,7 +230,7 @@ describe("markdown-parser", function () {
const AST = parse("- item\n" + " - item2"); // second line should has offset
const node = findFirstTypedNode(AST, Syntax.ListItem, " - item2");
assert(node);
assert.equal(node.raw, " - item2");
assert.strictEqual(node.raw, " - item2");
});
it("should has implemented TxtNode", function () {
const text = "text",
Expand All @@ -249,15 +253,15 @@ describe("markdown-parser", function () {
> BlockQuote
*/
context("Node type is BlockQuote", function () {
let AST, rawValue, text;
let AST: TxtNode, rawValue: string, text: string;
beforeEach(function () {
text = "text";
rawValue = `> ${text}`;
AST = parse(rawValue);
});
it("should has implemented TxtNode", function () {
const node = findFirstTypedNode(AST, Syntax.BlockQuote);
assert.deepEqual(node.range, [0, rawValue.length]);
assert.deepStrictEqual(node.range, [0, rawValue.length]);
});
});
/*
Expand All @@ -267,7 +271,7 @@ describe("markdown-parser", function () {
*/
context("Node type is CodeBlock", function () {
context("IndentCodeBlock", function () {
let AST, rawValue, code;
let AST: TxtNode, rawValue: string, code: string;
beforeEach(function () {
code = "var code;";
rawValue = `${" \n" + " "}${code}\n\n`;
Expand All @@ -277,11 +281,11 @@ describe("markdown-parser", function () {
const node = findFirstTypedNode(AST, Syntax.CodeBlock);
assert(node.raw.indexOf(code) !== -1);
const slicedCode = rawValue.slice(node.range[0], node.range[1]);
assert.equal(slicedCode.trim(), code);
assert.strictEqual(slicedCode.trim(), code);
});
});
context("FencedCode", function () {
let AST, rawValue, code;
let AST: TxtNode, rawValue: string, code: string;
beforeEach(function () {
code = "var code;";
rawValue = `\`\`\`\n${code}\n\`\`\``;
Expand All @@ -290,17 +294,17 @@ describe("markdown-parser", function () {
it("should has implemented TxtNode", function () {
const node = findFirstTypedNode(AST, Syntax.CodeBlock);
const codeBlockRaw = rawValue;
assert.equal(node.raw, codeBlockRaw);
assert.strictEqual(node.raw, codeBlockRaw);
const slicedCode = rawValue.slice(node.range[0], node.range[1]);
assert.equal(slicedCode, codeBlockRaw);
assert.strictEqual(slicedCode, codeBlockRaw);
});
});
});
/*
`code`
*/
context("Node type is Code", function () {
let AST, rawValue;
let AST: TxtNode, rawValue: string;
beforeEach(function () {
rawValue = "`code`";
AST = parse(rawValue);
Expand All @@ -314,7 +318,7 @@ describe("markdown-parser", function () {
__Strong__
*/
context("Node type is Strong", function () {
let AST, rawValue, text;
let AST: TxtNode, rawValue: string, text: string;
beforeEach(function () {
text = "text";
rawValue = `__${text}__`;
Expand All @@ -336,7 +340,7 @@ describe("markdown-parser", function () {
![text](http://example.com/a.png)
*/
context("Node type is Image", function () {
let AST, rawValue, labelText;
let AST: TxtNode, rawValue: string, labelText: string;
beforeEach(function () {
labelText = "text";
rawValue = `![${labelText}](http://example.com/a.png)`;
Expand All @@ -351,7 +355,7 @@ describe("markdown-parser", function () {
*text*
*/
context("Node type is Emphasis", function () {
let AST, rawValue, text;
let AST: TxtNode, rawValue: string, text: string;
beforeEach(function () {
text = "text";
rawValue = `*${text}*`;
Expand All @@ -372,7 +376,7 @@ describe("markdown-parser", function () {
----
*/
context("Node type is HorizontalRule", function () {
let AST, rawValue;
let AST: TxtNode, rawValue: string;
beforeEach(function () {
rawValue = "----";
AST = parse(rawValue);
Expand All @@ -386,7 +390,7 @@ describe("markdown-parser", function () {
<html>
*/
context("Node type is Html", function () {
let AST, rawValue;
let AST: TxtNode, rawValue: string;
beforeEach(function () {
rawValue = "<p>text</p>";
AST = parse(rawValue);
Expand Down

0 comments on commit ac95d63

Please sign in to comment.