Skip to content

Commit e599c05

Browse files
authored
refactor($shared-utils): migrate to typescript (vuejs#1086)
1 parent e963731 commit e599c05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+729
-462
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages/@vuepress/shared-utils/lib

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ node_modules
44
.temp
55
TODOs.md
66
vuepress
7+
packages/@vuepress/shared-utils/lib/
8+
types

package.json

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
"scripts": {
1111
"bootstrap": "lerna bootstrap",
1212
"boot": "node scripts/bootstrap.js",
13-
"dev": "yarn workspace docs dev",
14-
"build": "yarn workspace docs build",
13+
"dev": "yarn tsc && yarn workspace docs dev",
14+
"build": "yarn tsc && yarn workspace docs build",
1515
"show-help": "yarn workspace docs show-help",
1616
"dev:blog": "yarn workspace blog dev",
1717
"build:blog": "yarn workspace blog build",
1818
"register-vuepress": "lerna exec --scope vuepress -- yarn link",
1919
"lint": "eslint --fix packages/**/*.js packages/**/*.vue packages/**/bin/*",
2020
"release": "yarn --pure-lockfile && node scripts/release.js",
2121
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
22-
"test": "node scripts/test.js"
22+
"test": "node scripts/test.js",
23+
"tsc": "lerna run tsc"
2324
},
2425
"gitHooks": {
2526
"pre-commit": "lint-staged"
@@ -39,6 +40,14 @@
3940
"lint-staged": "^7.0.4",
4041
"minimist": "^1.2.0",
4142
"yorkie": "^1.0.3",
42-
"inquirer": "^6.2.0"
43+
"inquirer": "^6.2.0",
44+
"typescript": "^3.2.2",
45+
"@types/node": "^10.12.12",
46+
"@types/lru-cache": "^4.1.1",
47+
"@types/fs-extra": "^5.0.4",
48+
"@types/semver": "^5.5.0",
49+
"@types/hash-sum": "^1.0.0",
50+
"@types/globby": "^8.0.0",
51+
"@types/escape-html": "^0.0.20"
4352
}
4453
}

packages/@vuepress/core/lib/prepare/Page.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const {
1717
getPermalink,
1818
extractHeaders,
1919
parseFrontmatter,
20-
parseVueFrontmatter
20+
parseVueFrontmatter: { parse: parseVueFrontmatter }
2121
} = require('@vuepress/shared-utils')
2222

2323
/**

packages/@vuepress/shared-utils/__tests__/deeplyParseHeaders.spec.js

-87
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import deeplyParseHeaders from '../src/deeplyParseHeaders'
2+
3+
test('deeplyParseHeaders', () => {
4+
const asserts: Record<string, string> = {
5+
// Remove tail html
6+
'# `H1` <Comp></Comp>': '# H1',
7+
'# *H1* <Comp/>': '# H1',
8+
9+
// Reserve code-wrapped tail html
10+
'# `H1` `<Comp></Comp>`': '# H1 <Comp></Comp>',
11+
'# *H1* `<Comp/>`': '# H1 <Comp/>',
12+
13+
// Remove leading html
14+
'# <Comp></Comp> `H1`': '# H1',
15+
'# <Comp/> *H1*': '# H1',
16+
17+
// Reserve code-wrapped leading html
18+
'# `<Comp></Comp>` `H1`': '# <Comp></Comp> H1',
19+
'# `<Comp/>` *H1*': '# <Comp/> H1',
20+
21+
// Remove middle html
22+
'# `H1` <Comp></Comp> `H2`': '# H1 H2',
23+
'# `H1` <Comp/> `H2`': '# H1 H2',
24+
25+
// Reserve middle html
26+
'# `H1` `<Comp></Comp>` `H2`': '# H1 <Comp></Comp> H2',
27+
'# `H1` `<Comp/>` `H2`': '# H1 <Comp/> H2'
28+
}
29+
30+
Object.keys(asserts).forEach(input => {
31+
expect(deeplyParseHeaders(input)).toBe(asserts[input])
32+
})
33+
})

packages/@vuepress/shared-utils/__tests__/fileToPath.spec.js renamed to packages/@vuepress/shared-utils/__tests__/fileToPath.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import fileToPath from '../lib/fileToPath'
1+
import fileToPath from '../src/fileToPath'
22

33
test('fileToPath', () => {
4-
const asserts = {
4+
const asserts: Record<string, string> = {
55
'README.md': '/',
66
'foo/README.md': '/foo/',
77
'foo.md': '/foo.html',

packages/@vuepress/shared-utils/__tests__/isIndexFile.spec.js renamed to packages/@vuepress/shared-utils/__tests__/isIndexFile.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import isIndexFile from '../lib/isIndexFile'
1+
import { isIndexFile } from '../src/isIndexFile'
22

33
test('isIndexFile', () => {
44
[

packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.js renamed to packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import {
1111
getThemeResolver,
1212
getPluginResolver,
1313
resolveScopePackage
14-
} from '../lib/moduleResolver'
14+
} from '../src/moduleResolver'
1515
import hash from 'hash-sum'
1616

1717
const MOCK_RELATIVE = '../../../../__mocks__'
1818

19-
function loadMockModule (name) {
19+
function loadMockModule (name: string) {
2020
return require(`${MOCK_RELATIVE}/${name}`)
2121
}
2222

23-
function resolveMockModule (name) {
23+
function resolveMockModule (name: string) {
2424
return path.resolve(__dirname, `${MOCK_RELATIVE}/${name}`)
2525
}
2626

@@ -46,14 +46,14 @@ describe('resolveScopePackage', () => {
4646

4747
test('incorrect format', () => {
4848
const pkg2 = resolveScopePackage('vuepress/plugin-a')
49-
expect(pkg2).toBe(null)
49+
expect(pkg2).toEqual({ "name": "", "org": "" })
5050

5151
const pkg3 = resolveScopePackage('vuepress-plugin-a')
52-
expect(pkg3).toBe(null)
52+
expect(pkg3).toEqual({ "name": "", "org": "" })
5353
})
5454
})
5555

56-
const getBaseAsserts = (type) => [
56+
const getBaseAsserts = (type: string) => [
5757
{ input: 'a', output: ['a', `vuepress-${type}-a`] },
5858
{ input: `vuepress-${type}-a`, output: ['a', `vuepress-${type}-a`] },
5959
{ input: '@vuepress/a', output: ['@vuepress/a', `@vuepress/${type}-a`] },

packages/@vuepress/shared-utils/__tests__/parseHeaders.spec.js renamed to packages/@vuepress/shared-utils/__tests__/parseHeaders.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import parseHeaders from '../lib/parseHeaders'
1+
import parseHeaders from '../src/parseHeaders'
22

33
describe('parseHeaders', () => {
44
test('should unescape html', () => {
@@ -7,7 +7,7 @@ describe('parseHeaders', () => {
77
})
88

99
test('should remove markdown tokens correctly', () => {
10-
const asserts = {
10+
const asserts: Record<string, string> = {
1111
// #238
1212
'[vue](vuejs.org)': 'vue',
1313
'`vue`': 'vue',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import removeNonCodeWrappedHTML from '../src/removeNonCodeWrappedHTML'
2+
3+
test('removeNonCodeWrappedHTML', () => {
4+
const asserts: Record<string, string> = {
5+
// Remove tail html
6+
'# H1 <Comp></Comp>': '# H1 ',
7+
'# H1<Comp></Comp>': '# H1',
8+
'# H1 <Comp a="b"></Comp>': '# H1 ',
9+
'# H1<Comp a="b"></Comp>': '# H1',
10+
'# H1 <Comp/>': '# H1 ',
11+
'# H1<Comp/>': '# H1',
12+
'# H1 <Comp a="b"/>': '# H1 ',
13+
'# H1<Comp a="b"/>': '# H1',
14+
15+
// Reserve code-wrapped tail html
16+
'# H1 `<Comp></Comp>`': '# H1 `<Comp></Comp>`',
17+
'# H1 `<Comp a="b"></Comp>`': '# H1 `<Comp a="b"></Comp>`',
18+
'# H1 `<Comp/>`': '# H1 `<Comp/>`',
19+
'# H1 `<Comp a="b"/>`': '# H1 `<Comp a="b"/>`',
20+
21+
// Remove leading html
22+
'# <Comp></Comp> H1': '# H1',
23+
'# <Comp></Comp>H1': '# H1',
24+
'# <Comp a="b"></Comp> H1': '# H1',
25+
'# <Comp a="b"></Comp>H1': '# H1',
26+
'# <Comp/> H1': '# H1',
27+
'# <Comp/>H1': '# H1',
28+
'# <Comp a="b"/> H1': '# H1',
29+
'# <Comp a="b"/>H1': '# H1',
30+
31+
// Reserve code-wrapped leading html
32+
'# `<Comp></Comp>` H1': '# `<Comp></Comp>` H1',
33+
'# `<Comp a="b"></Comp>` H1': '# `<Comp a="b"></Comp>` H1',
34+
'# `<Comp/>` H1': '# `<Comp/>` H1',
35+
'# `<Comp a="b"/>` H1': '# `<Comp a="b"/>` H1',
36+
37+
// Remove middle html
38+
'# H1 <Comp></Comp> H2': '# H1 H2',
39+
'# H1 <Comp a="b"></Comp> H2': '# H1 H2',
40+
'# H1 <Comp/> H2': '# H1 H2',
41+
'# H1 <Comp a="b"/> H2': '# H1 H2',
42+
43+
// Reserve code-wrapped middle html
44+
'# H1 `<Comp></Comp>` H2': '# H1 `<Comp></Comp>` H2',
45+
'# H1 `<Comp a="b"></Comp>` H2': '# H1 `<Comp a="b"></Comp>` H2',
46+
'# H1 `<Comp/>` H2': '# H1 `<Comp/>` H2',
47+
'# H1 `<Comp a="b"/>` H2': '# H1 `<Comp a="b"/>` H2'
48+
}
49+
50+
Object.keys(asserts).forEach(input => {
51+
expect(removeNonCodeWrappedHTML(input)).toBe(asserts[input])
52+
})
53+
})

packages/@vuepress/shared-utils/__tests__/unescapeHtml.spec.js renamed to packages/@vuepress/shared-utils/__tests__/unescapeHtml.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unescapeHtml from '../lib/unescapeHtml'
1+
import unescapeHtml from '../src/unescapeHtml'
22

33
test('should unescape html', () => {
44
const input = '&lt;div&gt;'

packages/@vuepress/shared-utils/index.js

-39
This file was deleted.

packages/@vuepress/shared-utils/lib/compose.js

-10
This file was deleted.

0 commit comments

Comments
 (0)