Skip to content

Commit 364571b

Browse files
committed
style($shared-utils): lint the code
1 parent bd146ec commit 364571b

14 files changed

+67
-74
lines changed

Diff for: packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ describe('resolveScopePackage', () => {
4646

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

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

@@ -123,11 +123,13 @@ describe('resolvePlugin', () => {
123123

124124
test('relative path', () => {
125125
const resolved = resolvePlugin('./plugin-a')
126+
// eslint-disable-next-line @typescript-eslint/no-var-requires
126127
expect(resolved.entry).toBe(require('./fixtures/plugin-a'))
127128
})
128129

129130
test('aosolute path', () => {
130131
const resolved = resolvePlugin(path.resolve(__dirname, 'fixtures/plugin-a'))
132+
// eslint-disable-next-line @typescript-eslint/no-var-requires
131133
expect(resolved.entry).toBe(require('./fixtures/plugin-a'))
132134
})
133135

Diff for: packages/@vuepress/shared-utils/src/datatypes.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const getType = function (fn: any) {
3131
* ['Function', 'Object'] => 'Function or Object'
3232
* ['Function', 'Object', 'Number'] => 'Function, Object or Number'
3333
*/
34-
type Type = String | Number | Boolean | RegExp | Function | Record<string, any> | Array<any>
34+
type Type = string | number | boolean | RegExp | Function | Record<string, any> | Array<any>
3535

3636
function toNaturalMultiTypesLanguage (types: Type[]) {
3737
const len = types.length
@@ -47,7 +47,7 @@ export function assertTypes (value: any, types: Type[]) {
4747
let valid
4848
let warnMsg
4949
let actualType = toRawType(value)
50-
const expectedTypes = []
50+
const expectedTypes: Type[] = []
5151
if (actualType === 'AsyncFunction') {
5252
actualType = 'Function'
5353
}
@@ -60,9 +60,9 @@ export function assertTypes (value: any, types: Type[]) {
6060
}
6161

6262
if (!valid) {
63-
warnMsg =
64-
`expected a ${chalk.green(toNaturalMultiTypesLanguage(expectedTypes))} ` +
65-
`but got ${chalk.yellow(actualType)}.`
63+
warnMsg
64+
= `expected a ${chalk.green(toNaturalMultiTypesLanguage(expectedTypes))} `
65+
+ `but got ${chalk.yellow(actualType)}.`
6666
}
6767

6868
return { valid, warnMsg }

Diff for: packages/@vuepress/shared-utils/src/extractHeaders.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import deeplyParseHeaders from './deeplyParseHeaders'
1212

1313
const cache = new LRU({ max: 1000 })
1414

15-
export = function (content: string, include = [], md: any) {
15+
export = function (content: string, include: any[] = [], md: any) {
1616
const key = content + include.join(',')
1717
const hit = cache.get(key)
1818
if (hit) {
@@ -23,11 +23,9 @@ export = function (content: string, include = [], md: any) {
2323

2424
const res: any[] = []
2525
tokens.forEach((t: any, i: any) => {
26-
// @ts-ignore
2726
if (t.type === 'heading_open' && include.includes(t.tag)) {
2827
const title = tokens[i + 1].content
29-
// @ts-ignore
30-
const slug = (t.attrs).find(([name]) => name === 'id')[1]
28+
const slug = t.attrs.find(([name]: any[]) => name === 'id')[1]
3129
res.push({
3230
level: parseInt(t.tag.slice(1), 10),
3331
title: deeplyParseHeaders(title),

Diff for: packages/@vuepress/shared-utils/src/getPermalink.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ export = function getPermalink ({
4444

4545
pattern = removeLeadingSlash(pattern)
4646

47-
const link =
48-
localePath +
49-
pattern
47+
const link
48+
= localePath
49+
+ pattern
5050
.replace(/:year/, String(year))
5151
.replace(/:month/, String(month))
5252
.replace(/:i_month/, String(iMonth))

Diff for: packages/@vuepress/shared-utils/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,5 @@ export {
6868
globby,
6969
hash,
7070
escapeHtml,
71-
semver,
71+
semver
7272
}

Diff for: packages/@vuepress/shared-utils/src/logger.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import chalk from 'chalk'
88

99
interface LoggerOptions {
10-
logLevel: number
10+
logLevel: number;
1111
}
1212

1313
class Logger {
@@ -90,7 +90,6 @@ class Logger {
9090
if (this.options.logLevel < 3) {
9191
return
9292
}
93-
// @ts-ignore
9493
console.log(chalk[color](label), ...args)
9594
}
9695

@@ -107,4 +106,3 @@ class Logger {
107106
*/
108107

109108
export = new Logger()
110-

Diff for: packages/@vuepress/shared-utils/src/moduleLoader.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import semver from 'semver'
44
import env from './env'
55

66
function resolveFallback (request: string, options: { paths: string[] }) {
7+
// eslint-disable-next-line @typescript-eslint/no-var-requires
78
const Module = require('module')
89
const isMain = false
910
const fakeParent = new Module('', null)
@@ -24,28 +25,37 @@ function resolveFallback (request: string, options: { paths: string[] }) {
2425

2526
const filename = Module._findPath(request, paths, isMain)
2627
if (!filename) {
27-
const err = new Error(`Cannot find module '${request}'`)
28-
// @ts-ignores
28+
const err: Error & { code?: string } = new Error(`Cannot find module '${request}'`)
2929
err.code = 'MODULE_NOT_FOUND'
3030
throw err
3131
}
3232
return filename
3333
}
3434

35+
function clearRequireCache (id: string, map = new Map()) {
36+
const module = require.cache[id]
37+
if (module) {
38+
map.set(id, true)
39+
// Clear children modules
40+
module.children.forEach((child: any) => {
41+
if (!map.get(child.id)) clearRequireCache(child.id, map)
42+
})
43+
delete require.cache[id]
44+
}
45+
}
46+
3547
const resolve = semver.satisfies(process.version, '>=10.0.0')
3648
? require.resolve
3749
: resolveFallback
3850

3951
export function resolveModule (request: string, context: string): string {
40-
let resolvedPath
41-
4252
if (env.isTest) {
4353
return require.resolve(request)
4454
}
4555

4656
// module.paths is for globally install packages.
4757
const paths = [context || process.cwd(), ...module.paths]
48-
resolvedPath = resolve(request, { paths })
58+
const resolvedPath = resolve(request, { paths })
4959

5060
return resolvedPath
5161
}
@@ -66,15 +76,3 @@ export function clearModule (request: string, context: string) {
6676
clearRequireCache(resolvedPath)
6777
}
6878
}
69-
70-
function clearRequireCache (id: string, map = new Map()) {
71-
const module = require.cache[id]
72-
if (module) {
73-
map.set(id, true)
74-
// Clear children modules
75-
module.children.forEach((child: any) => {
76-
if (!map.get(child.id)) clearRequireCache(child.id, map)
77-
})
78-
delete require.cache[id]
79-
}
80-
}

Diff for: packages/@vuepress/shared-utils/src/moduleResolver.ts

+30-30
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,30 @@ import {
1717
assertTypes
1818
} from './datatypes'
1919

20+
/**
21+
* Parse info of scope package.
22+
*/
23+
2024
const SCOPE_PACKAGE_RE = /^@(.*)\/(.*)/
2125

26+
export interface ScopePackage {
27+
org: string;
28+
name: string;
29+
}
30+
31+
export function resolveScopePackage (name: string) {
32+
if (SCOPE_PACKAGE_RE.test(name)) {
33+
return {
34+
org: RegExp.$1,
35+
name: RegExp.$2
36+
}
37+
}
38+
return {
39+
org: '',
40+
name: ''
41+
}
42+
}
43+
2244
/**
2345
* Common module constructor.
2446
*/
@@ -33,20 +55,20 @@ export class CommonModule {
3355
) {}
3456
}
3557

36-
function getNoopModule(error?: Error) {
58+
function getNoopModule (error?: Error) {
3759
return new CommonModule(null, null, null, null, error)
3860
}
3961

4062
export interface NormalizedModuleRequest {
41-
name: string | null
42-
shortcut: string | null
63+
name: string | null;
64+
shortcut: string | null;
4365
}
4466

4567
/**
4668
* Expose ModuleResolver.
4769
*/
4870

49-
type Type = String | Number | Boolean | RegExp | Function | Object | Record<string, any> | Array<any>
71+
type Type = string | number | boolean | RegExp | Function | Record<string, any> | Record<string, any> | Array<any>
5072

5173
class ModuleResolver {
5274
private nonScopePrefix: string
@@ -162,8 +184,8 @@ class ModuleResolver {
162184
const { shortcut, name } = this.normalizeName(req)
163185
try {
164186
const entry = this.load
165-
? loadModule(<string>name, this.cwd)
166-
: resolveModule(<string>name, this.cwd)
187+
? loadModule(name as string, this.cwd)
188+
: resolveModule(name as string, this.cwd)
167189
return new CommonModule(entry, name, shortcut, true /* fromDep */)
168190
} catch (error) {
169191
return getNoopModule(error)
@@ -185,8 +207,8 @@ class ModuleResolver {
185207
*/
186208

187209
normalizeName (req: string): NormalizedModuleRequest {
188-
let name = null
189-
let shortcut = null
210+
let name: string | null = null
211+
let shortcut: string | null = null
190212

191213
if (req.startsWith('@')) {
192214
const pkg = resolveScopePackage(req)
@@ -237,28 +259,6 @@ class ModuleResolver {
237259
}
238260
}
239261

240-
/**
241-
* Parse info of scope package.
242-
*/
243-
244-
export interface ScopePackage {
245-
org: string;
246-
name: string;
247-
}
248-
249-
export function resolveScopePackage (name: string) {
250-
if (SCOPE_PACKAGE_RE.test(name)) {
251-
return {
252-
org: RegExp.$1,
253-
name: RegExp.$2
254-
}
255-
}
256-
return {
257-
org: '',
258-
name: ''
259-
}
260-
}
261-
262262
export const getMarkdownItResolver = (cwd: string) => new ModuleResolver(
263263
'markdown-it', '', [String, Function], true /* load module */, cwd
264264
)

Diff for: packages/@vuepress/shared-utils/src/parseEmojis.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const emojiData = require('markdown-it-emoji/lib/data/full.json')
1+
import emojiData from 'markdown-it-emoji/lib/data/full.json'
22

33
export default (str: string) => {
44
return String(str).replace(/:(.+?):/g, (placeholder, key) => emojiData[key] || placeholder)
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const matter = require('gray-matter')
2-
const toml = require('toml')
1+
import matter from 'gray-matter'
2+
import toml from 'toml'
33

44
export = function parseFrontmatter (content: string) {
55
return matter(content, {
6+
// eslint-disable-next-line @typescript-eslint/camelcase
67
excerpt_separator: '<!-- more -->',
78
engines: {
8-
toml: toml.parse.bind(toml),
9-
excerpt: false
9+
toml: toml.parse.bind(toml)
1010
}
1111
})
1212
}

Diff for: packages/@vuepress/shared-utils/src/parseVueFrontmatter.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @ts-ignore
21
import { parse as _parse } from '@vue/component-compiler-utils'
32
import parseFrontmatter from './parseFrontmatter'
43

Diff for: packages/@vuepress/shared-utils/src/performance.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os from 'os'
22

33
class Performance {
4-
// @ts-ignore
54
private _totalMemory: number
65
private _startFreeMemory: number
76
private _endFreeMemory: number

Diff for: packages/@vuepress/shared-utils/src/slugify.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// string.js slugify drops non ascii chars so we have to
22
// use a custom implementation here
3-
// @ts-ignore
43
import { remove as removeDiacritics } from 'diacritics'
54

65
// eslint-disable-next-line no-control-regex

Diff for: packages/@vuepress/shared-utils/src/tryChain.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export = function tryChain<T, U> (resolvers: Array<Resolver<T, U>>, arg: T): U |
1313
continue
1414
}
1515
try {
16-
response = (<Provider<T, U>>provider)(arg)
16+
response = (provider as Provider<T, U>)(arg)
1717
return response
1818
} catch (e) {
1919
}

0 commit comments

Comments
 (0)