Skip to content

Commit 8ff8c9a

Browse files
authored
refactor: refactor project by typescript (#2)
* chore: init typescript develop environment * refactor: add types for ast-plugin * test: add test case for throwErrFn * chore: remove .npmignore, use 'files' instead * ci: add test github workflows * chore: update some configurations * feat: expose type definitions * chore(release): publish v1.0.0 * chore: add useful command for npm scripts
1 parent 5922aac commit 8ff8c9a

18 files changed

+196
-109
lines changed

.babelrc

-8
This file was deleted.

.github/workflows/build.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: build
2+
3+
on: [ pull_request ]
4+
5+
jobs:
6+
unit-test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
11+
- name: Install modules
12+
run: yarn install
13+
14+
- name: Run tests
15+
run: yarn test

.gitignore

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
lib
2-
coverage
1+
# IDE configurations
2+
.idea
3+
.vscode
4+
5+
# Dependency directories
36
node_modules
7+
8+
# coverage files
9+
coverage
10+
11+
# lock files
12+
package-lock.json
13+
yarn.lock
14+
15+
# build files
16+
esm
17+
lib

.npmignore

-11
This file was deleted.

.prettierrc.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"tabWidth": 2,
3+
"semi": true,
4+
"singleQuote": true,
5+
"jsxBracketSameLine": true
6+
}

.travis.yml

-8
This file was deleted.

README.md

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
# ast-plugin
1+
# @lint-md/ast-plugin
22

33
> The simplest abstract syntax tree walker.
44
5+
[![Build Status](https://github.com/lint-md/ast-plugin/actions/workflows/build.yml/badge.svg)](https://github.com/lint-md/ast-plugin/actions/workflows/build.yml)
56

67
## Install
78

89
> npm i --save ast-plugin
910
10-
11-
1211
## Usage
1312

14-
- Traverse Ast plugins
13+
- Traverse Ast plugins
1514

1615
```js
1716
import { Ast, Plugin } from 'ast-plugin';
@@ -22,14 +21,15 @@ new Ast(ast).traverse([
2221
]);
2322
```
2423

25-
- Write an ast plugin
24+
- Write an ast plugin
2625

2726
```js
2827
import { Ast, Plugin } from 'ast-plugin';
2928

3029
class TestPlugin extends Plugin {
3130

32-
pre = () => {};
31+
pre = () => {
32+
};
3333

3434
visitor = () => {
3535
return {
@@ -41,18 +41,14 @@ class TestPlugin extends Plugin {
4141
};
4242
};
4343

44-
post = () => {};
44+
post = () => {
45+
};
4546
}
4647
```
4748

48-
49-
5049
## Used by
5150

52-
- [lint-md](https://github.com/hustcc/lint-md): Cli tool to lint your markdown file for Chinese.
53-
54-
55-
51+
- [lint-md](https://github.com/lint-md/lint-md) Cli tool to lint your markdown file for Chinese.
5652

5753
## License
5854

__tests__/TestPlugin.js __tests__/TestPlugin.ts

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export class TestPlugin extends Plugin {
1313
return {
1414
text: ast => {
1515
this.do(ast);
16+
this.cfg.throwError({
17+
name: 'Hello, I threw an exception!'
18+
});
1619
},
1720
heading: ast => {
1821
this.doHeading(ast);

__tests__/index.spec.js __tests__/index.spec.ts

+52-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
1-
import unified from 'unified';
2-
import markdown from 'remark-parse';
3-
1+
import * as unified from 'unified';
2+
import * as markdown from 'remark-parse';
43
import { Ast, Plugin } from '../src/';
54
import { setGlobalConfig, getGlobalConfig } from '../src/global';
65
import { TestPlugin } from './TestPlugin';
76

8-
const plugin1 = new TestPlugin('p1');
9-
const plugin2 = new TestPlugin('p2');
10-
117
describe('index', () => {
8+
129
test('exports', () => {
1310
expect(Ast).not.toBe(undefined);
1411
expect(Plugin).not.toBe(undefined);
1512
});
1613

1714
test('Ast', () => {
18-
expect(plugin1.cfg).toBe('p1');
19-
expect(plugin2.cfg).toBe('p2');
15+
let throwErrFn = jest.fn();
16+
const plugin1 = new TestPlugin({
17+
throwError: throwErrFn,
18+
config: {
19+
name: 'p1'
20+
}
21+
});
22+
23+
const plugin2 = new TestPlugin({
24+
throwError: throwErrFn,
25+
config: {
26+
name: 'p2'
27+
}
28+
});
29+
expect(plugin1.cfg).toStrictEqual({
30+
'config': {
31+
'name': 'p1'
32+
},
33+
'throwError': throwErrFn
34+
});
35+
expect(plugin2.cfg).toStrictEqual({
36+
'config': {
37+
'name': 'p2'
38+
},
39+
'throwError': throwErrFn
40+
});
2041

2142
const ast = unified()
2243
.use(markdown)
2344
.parse('> Hello **world**!\n\n# ~~hustcc~~');
2445

2546
const plugins = [
2647
plugin1,
27-
plugin2,
48+
plugin2
2849
];
2950

3051
new Ast(ast).traverse(plugins);
@@ -62,26 +83,44 @@ cont a = 1;
6283
const b = 2;
6384
\`\`\``;
6485

65-
let ast = unified()
86+
let mdAst = unified()
6687
.use(markdown)
6788
.parse(md);
6889

69-
ast = new Ast(ast, undefined, md);
90+
let ast = new Ast(mdAst, undefined, md);
7091

7192
const code = ast.get('children.0');
7293

7394
expect(code.segment()).toBe(md);
7495

7596
md = '> Hello **world**!';
76-
ast = unified()
97+
mdAst = unified()
7798
.use(markdown)
7899
.parse(md);
79100

80-
ast = new Ast(ast, undefined, md);
101+
ast = new Ast(mdAst, undefined, md);
81102

82103
expect(ast.get('children.0.children.0').segment()).toBe('Hello **world**!');
83104
});
84105

106+
test('throwError call', () => {
107+
const throwErrFn = jest.fn();
108+
const testErrorPlugin = new TestPlugin({
109+
throwError: throwErrFn
110+
});
111+
const md = 'bad content that break some rules...';
112+
let mdAst = unified()
113+
.use(markdown)
114+
.parse(md);
115+
116+
const ast = new Ast(mdAst, undefined, md);
117+
ast.traverse([testErrorPlugin, testErrorPlugin]);
118+
expect(throwErrFn).toBeCalledTimes(2);
119+
expect(throwErrFn).toBeCalledWith({
120+
'name': 'Hello, I threw an exception!'
121+
});
122+
});
123+
85124
test('global configure', () => {
86125
expect(getGlobalConfig()).toEqual({ typeKey: 'type', childrenKey: 'children' });
87126

package.json

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
{
2-
"name": "ast-plugin",
3-
"version": "0.0.7",
2+
"name": "@lint-md/ast-plugin",
3+
"version": "1.0.0",
44
"description": "The simplest abstract syntax tree walker.",
55
"main": "lib/index.js",
6+
"module": "esm/index.js",
67
"scripts": {
78
"test": "jest --no-cache",
8-
"coveralls": "cat ./coverage/lcov.info | coveralls",
9-
"build": "rimraf ./lib && babel src -d lib"
9+
"lib:cjs": "tsc -p tsconfig.json --target ES5 --module commonjs --outDir lib",
10+
"lib:esm": "tsc -p tsconfig.json --target ES5 --module ESNext --outDir esm",
11+
"clean": "rimraf lib esm",
12+
"build": "run-p lib:*"
1013
},
1114
"repository": {
1215
"type": "git",
13-
"url": "git+https://github.com/hustcc/ast-plugin.git"
16+
"url": "git+https://github.com/lint-md/ast-plugin.git"
1417
},
1518
"keywords": [
1619
"ast",
@@ -19,31 +22,32 @@
1922
],
2023
"author": "hustcc",
2124
"license": "MIT",
25+
"files": [
26+
"lib",
27+
"esm",
28+
"src"
29+
],
2230
"bugs": {
23-
"url": "https://github.com/hustcc/ast-plugin/issues"
31+
"url": "https://github.com/lint-md/ast-plugin/issues"
2432
},
25-
"homepage": "https://github.com/hustcc/ast-plugin#readme",
33+
"homepage": "https://github.com/lint-md/ast-plugin#readme",
2634
"devDependencies": {
27-
"@babel/cli": "^7.1.5",
28-
"@babel/core": "^7.1.5",
29-
"@babel/plugin-proposal-class-properties": "^7.1.0",
30-
"@babel/plugin-transform-spread": "^7.0.0",
31-
"@babel/preset-env": "^7.1.5",
32-
"babel-core": "^7.0.0-bridge.0",
33-
"babel-jest": "^23.6.0",
34-
"babel-plugin-version": "^0.2.3",
35-
"coveralls": "^3.0.2",
36-
"jest": "^23.6.0",
35+
"@types/jest": "^26.0.20",
36+
"jest": "^26.6.3",
37+
"npm-run-all": "^4.1.5",
3738
"remark-parse": "^6.0.2",
38-
"rimraf": "^2.6.2",
39+
"rimraf": "^3.0.2",
40+
"ts-jest": "^26.5.3",
41+
"typescript": "^4.2.3",
3942
"unified": "^7.0.1"
4043
},
4144
"jest": {
45+
"preset": "ts-jest",
4246
"collectCoverage": true,
4347
"testEnvironment": "node",
44-
"testRegex": "(/__tests__/.*\\.(test|spec))\\.jsx?$",
48+
"testRegex": "(/__tests__/.*\\.(test|spec))\\.tsx?$",
4549
"collectCoverageFrom": [
46-
"src/**/*.{js,jsx}"
50+
"src/**/*.{ts,tsx}"
4751
]
4852
}
4953
}

0 commit comments

Comments
 (0)