Skip to content

Commit 8e087eb

Browse files
committed
chore: resolved some lint error by unit test
1 parent d628a40 commit 8e087eb

File tree

6 files changed

+48
-40
lines changed

6 files changed

+48
-40
lines changed

packages/core/hmr/__test__/hmr.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ mockModuleNode.add({ id: 'foo.vue' })
1616
const mockFileToModulesMap = new Map()
1717
mockFileToModulesMap.set('../D/test', mockModuleNode)
1818

19-
let hmrModule = null
19+
let hmrModule: any = null
2020
const mockServer = {
21-
reloadModule: (m) => {
21+
reloadModule: (m: any) => {
2222
hmrModule = m
2323
},
2424
moduleGraph: {

packages/core/inject/__test__/inject-cssvars.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('inject-cssvars', () => {
5151

5252
describe('createCSSVarsObjCode', () => {
5353
test('should return empty string if vbindVariableList is empty', () => {
54-
const vbindVariableList = []
54+
const vbindVariableList: any[] = []
5555
const result = createCSSVarsObjCode(vbindVariableList, false)
5656
expect(result).toBe('')
5757
})

packages/core/inject/inject-cssvars.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,19 @@ export function injectUseCssVarsSetup(
6565
parserRes: IParseSFCRes,
6666
) {
6767
let resMgcStr = mgcStr
68-
if (!hasUseCssVars) {
69-
const start = parserRes.setupBodyNode.start + 1
70-
resMgcStr = resMgcStr.prependLeft(start, useCssVars)
71-
resMgcStr = resMgcStr.prependLeft(0, importer)
72-
} else {
73-
const start = parserRes.useCSSVarsNode.start + 1
74-
resMgcStr = resMgcStr.prependLeft(start, useCssVars)
68+
if (parserRes) {
69+
if (!hasUseCssVars
70+
&& parserRes.setupBodyNode
71+
&& parserRes.setupBodyNode.start) {
72+
const start = parserRes.setupBodyNode.start + 1
73+
resMgcStr = resMgcStr.prependLeft(start, useCssVars)
74+
resMgcStr = resMgcStr.prependLeft(0, importer)
75+
} else if (hasUseCssVars
76+
&& parserRes.useCSSVarsNode
77+
&& parserRes.useCSSVarsNode.start) {
78+
const start = parserRes.useCSSVarsNode.start + 1
79+
resMgcStr = resMgcStr.prependLeft(start, useCssVars)
80+
}
7581
}
7682

7783
return resMgcStr
@@ -96,10 +102,13 @@ export function injectUseCssVarsOption(
96102
const _sfc_main = __default__
97103
function _sfc_render`)
98104
resMgcStr = resMgcStr.prependLeft(0, importer)
99-
} else {
105+
} else if (hasUseCssVars
106+
&& parserRes.useCSSVarsNode
107+
&& parserRes.useCSSVarsNode.start) {
100108
const start = parserRes.useCSSVarsNode.start + 1
101109
resMgcStr = resMgcStr.prependLeft(start, useCssVars)
102110
}
111+
103112
return resMgcStr
104113
}
105114

packages/core/parser/__test__/parser-compiled-sfc.spec.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
reSetVar,
1111
setVar,
1212
} from '../parser-compiled-sfc'
13-
import type { ImportSpecifier } from '@babel/types'
13+
import type { CallExpression, Identifier, ImportSpecifier, ObjectExpression } from '@babel/types'
1414
describe('parseSetupBody', () => {
1515
let node: any
1616

@@ -82,7 +82,7 @@ describe('parseHasCSSVars', () => {
8282
const code = 'const a = 1'
8383
const ast = parse(code)
8484

85-
const node = ast.program.body[0].declarations[0].init
85+
const node = (ast as any).program.body[0].declarations[0].init
8686

8787
parseHasCSSVars(node, null!)
8888

@@ -102,47 +102,47 @@ describe('parseHasCSSVars', () => {
102102
})
103103

104104
describe('parseUseCSSVars function', () => {
105-
let node: Node & { scopeIds?: Set<string> }
106-
let parent: Node & { scopeIds?: Set<string> }
105+
let node: Node & { scopeIds?: Set<string> } & Identifier & CallExpression & ObjectExpression
106+
let parent: Node & { scopeIds?: Set<string> } & Identifier & CallExpression & ObjectExpression
107107

108108
beforeEach(() => {
109109
reSetVar()
110-
node = {} as Node & { scopeIds?: Set<string> }
111-
parent = {} as Node & { scopeIds?: Set<string> }
110+
node = {} as Node & { scopeIds?: Set<string> } & Identifier & CallExpression & ObjectExpression
111+
parent = {} as Node & { scopeIds?: Set<string> } & Identifier & CallExpression & ObjectExpression
112112
})
113113

114114
test('sets isUseCSSVarsEnter to true when called with _useCssVars identifier and a CallExpression parent', () => {
115-
node.type = 'Identifier';
116-
(node as any).name = '_useCssVars'
117-
parent.type = 'CallExpression'
115+
(node as Identifier).type = 'Identifier';
116+
(node as Identifier).name = '_useCssVars';
117+
(parent as CallExpression).type = 'CallExpression'
118118

119-
parseUseCSSVars(node, parent as any)
119+
parseUseCSSVars(node as any, parent as any)
120120

121121
expect(getVar().isUseCSSVarsEnter).toBe(true)
122122
})
123123

124124
test('does not set isUseCSSVarsEnter to true when called with non-_useCssVars identifier and a CallExpression parent', () => {
125-
node.type = 'Identifier';
126-
(node as any).name = 'foo'
127-
parent.type = 'CallExpression'
125+
(node as Identifier).type = 'Identifier';
126+
(node as Identifier).name = 'foo';
127+
(parent as CallExpression).type = 'CallExpression'
128128

129129
parseUseCSSVars(node, parent as any)
130130

131131
expect(getVar().isUseCSSVarsEnter).toBe(false)
132132
})
133133

134134
test('does not set isUseCSSVarsEnter to true when called with _useCssVars identifier and a non-CallExpression parent', () => {
135-
node.type = 'Identifier';
136-
(node as any).name = '_useCssVars'
137-
parent.type = 'Identifier'
135+
(node as Identifier).type = 'Identifier';
136+
(node as Identifier).name = '_useCssVars';
137+
(parent as Identifier).type = 'Identifier'
138138

139139
parseUseCSSVars(node, parent as any)
140140

141141
expect(getVar().isUseCSSVarsEnter).toBe(false)
142142
})
143143

144144
test('does not set isUseCSSVarsEnter to true when called with _useCssVars identifier and no parent', () => {
145-
node.type = 'Identifier';
145+
(node as Identifier).type = 'Identifier';
146146
(node as any).name = '_useCssVars'
147147

148148
parseUseCSSVars(node, undefined!)
@@ -151,26 +151,25 @@ describe('parseUseCSSVars function', () => {
151151
})
152152

153153
test('sets useCSSVarsNode to the node when isUseCSSVarsEnter is true and node is an ObjectExpression', () => {
154-
setVar('isUseCSSVarsEnter', true)
155-
node.type = 'ObjectExpression'
156-
154+
setVar('isUseCSSVarsEnter', true);
155+
(node as ObjectExpression).type = 'ObjectExpression'
157156
parseUseCSSVars(node, parent as any)
158157

159158
expect(getVar().useCSSVarsNode).toBe(node)
160159
})
161160

162161
test('does not set useCSSVarsNode when isUseCSSVarsEnter is true and node is not an ObjectExpression', () => {
163-
setVar('isUseCSSVarsEnter', true)
164-
node.type = 'Identifier'
162+
setVar('isUseCSSVarsEnter', true);
163+
(node as Identifier).type = 'Identifier'
165164

166165
parseUseCSSVars(node, parent as any)
167166

168167
expect(getVar().useCSSVarsNode).not.toBe(node)
169168
})
170169

171170
test('does not set useCSSVarsNode when isUseCSSVarsEnter is false and node is an ObjectExpression', () => {
172-
setVar('isUseCSSVarsEnter', false)
173-
node.type = 'ObjectExpression'
171+
setVar('isUseCSSVarsEnter', false);
172+
(node as ObjectExpression).type = 'ObjectExpression'
174173

175174
parseUseCSSVars(node, parent as any)
176175

packages/core/parser/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './parser-variable'
22
export * from './parser-import'
33
export * from './parser-vbindm'
44
export { parserCompiledSfc } from './parser-compiled-sfc'
5+
export type { IParseSFCRes } from './parser-compiled-sfc'

packages/core/parser/parser-compiled-sfc.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parse as babelParse } from '@babel/parser'
22
import { walk } from 'estree-walker-ts'
33
import type {
44
BlockStatement,
5-
CallExpression, ExpressionStatement,
5+
CallExpression,
66
Identifier,
77
ImportSpecifier,
88
ObjectExpression,
@@ -35,7 +35,7 @@ export function parseHasCSSVars(
3535
if ((node as Identifier).type === 'Identifier'
3636
&& (node as Identifier).name === 'useCssVars'
3737
&& parent
38-
&& ((parent.type as ImportSpecifier) === 'ImportSpecifier'))
38+
&& (((parent as ImportSpecifier).type) === 'ImportSpecifier'))
3939
hasCSSVars = true
4040
}
4141

@@ -47,7 +47,7 @@ export function parseUseCSSVars(
4747
if ((node as Identifier).type === 'Identifier'
4848
&& (node as Identifier).name === '_useCssVars'
4949
&& parent
50-
&& ((parent.type as CallExpression) === 'CallExpression'))
50+
&& (((parent as CallExpression).type) === 'CallExpression'))
5151
isUseCSSVarsEnter = true
5252

5353
if (isUseCSSVarsEnter && (node as ObjectExpression).type === 'ObjectExpression') {
@@ -56,7 +56,6 @@ export function parseUseCSSVars(
5656
}
5757
}
5858

59-
// TODO: unit test
6059
export function parserCompiledSfc(code: string) {
6160
reSetVar()
6261
const ast = babelParse(code, {
@@ -100,7 +99,7 @@ export function getVar() {
10099
}
101100

102101
// test only
103-
export function setVar(k, v) {
102+
export function setVar(k: string, v: any) {
104103
if (k === 'isSetupEnter')
105104
isSetupEnter = v
106105

0 commit comments

Comments
 (0)