Skip to content

Commit c9913a5

Browse files
authored
fix: fix a bug that Ast instance doesn't have parent (#3)
* fix: fix a bug that Ast instance doesn't have parent * test: update unit test for node that have parent * fix(test): fix global configure test potential problem
1 parent 8ff8c9a commit c9913a5

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

__tests__/index.spec.ts

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as unified from 'unified';
22
import * as markdown from 'remark-parse';
3-
import { Ast, Plugin } from '../src/';
3+
import { Ast, Plugin } from '../src';
44
import { setGlobalConfig, getGlobalConfig } from '../src/global';
55
import { TestPlugin } from './TestPlugin';
66

77
describe('index', () => {
8-
98
test('exports', () => {
109
expect(Ast).not.toBe(undefined);
1110
expect(Plugin).not.toBe(undefined);
@@ -65,6 +64,7 @@ describe('index', () => {
6564
expect(plugin2.doDelete).toHaveBeenCalledTimes(0);
6665
});
6766

67+
6868
test('get', () => {
6969
const ast = unified()
7070
.use(markdown)
@@ -77,6 +77,7 @@ describe('index', () => {
7777
expect(new Ast(ast).get('aaa.bbb')).toBe(undefined);
7878
});
7979

80+
8081
test('segment', () => {
8182
let md = `\`\`\`js
8283
cont a = 1;
@@ -103,6 +104,7 @@ const b = 2;
103104
expect(ast.get('children.0.children.0').segment()).toBe('Hello **world**!');
104105
});
105106

107+
106108
test('throwError call', () => {
107109
const throwErrFn = jest.fn();
108110
const testErrorPlugin = new TestPlugin({
@@ -121,11 +123,49 @@ const b = 2;
121123
});
122124
});
123125

126+
124127
test('global configure', () => {
125128
expect(getGlobalConfig()).toEqual({ typeKey: 'type', childrenKey: 'children' });
126129

127130
setGlobalConfig({ typeKey: 'type' });
128131

129132
expect(getGlobalConfig()).toEqual({ typeKey: 'type' });
133+
134+
// 上述操作有副作用,会影响下面的 tests, 需要重新复原
135+
setGlobalConfig({ typeKey: 'type', childrenKey: 'children' });
136+
expect(getGlobalConfig()).toEqual({ typeKey: 'type', childrenKey: 'children' });
137+
});
138+
139+
test('should have parent in child ast', () => {
140+
const mock = jest.fn((ast: Ast) => {
141+
expect(ast.parent.node.type).toStrictEqual('paragraph');
142+
});
143+
144+
class MyPlugin extends Plugin {
145+
post(): void {
146+
}
147+
148+
pre(): void {
149+
}
150+
151+
visitor(): any {
152+
return {
153+
strong: mock
154+
};
155+
}
156+
}
157+
158+
const a = unified()
159+
.use(markdown)
160+
.parse('Hello **world**!');
161+
162+
163+
const p = new MyPlugin({
164+
throwError: undefined
165+
});
166+
167+
new Ast(a).traverse([p]);
168+
169+
expect(mock).toBeCalled();
130170
});
131171
});

src/Ast.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { Plugin } from './Plugin';
55

66
export class Ast {
77
node: Unist.Node;
8-
parent: Unist.Node;
8+
parent: Ast;
99
text: string;
1010
skipped: boolean;
1111

12-
constructor(node: Unist.Node, parent?: Unist.Parent, text?: string) {
12+
constructor(node: Unist.Node, parent?: Ast, text?: string) {
1313
this.node = node;
1414
this.parent = parent;
1515
// 文本、代码
@@ -59,7 +59,7 @@ export class Ast {
5959

6060
// 处理子元素
6161
if (Array.isArray(children) && !this.skipped) {
62-
children.forEach(child => new Ast(child, undefined, this.text).process(plugins));
62+
children.forEach(child => new Ast(child, this, this.text).process(plugins));
6363
}
6464
}
6565

0 commit comments

Comments
 (0)