Skip to content

Commit eed3b21

Browse files
committed
chore: updated unit test
1 parent c0d7c6d commit eed3b21

File tree

4 files changed

+174
-14
lines changed

4 files changed

+174
-14
lines changed

Diff for: packages/core/inject/__test__/inject-cssvars.spec.ts

+161-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import {
44
createCSSVarsObjCode,
55
createUCVCOptionUnHas,
66
createUCVCSetupUnHas,
7-
createUseCssVarsCode, injectCSSVars,
7+
createUseCssVarsCode,
8+
injectCSSVars,
89
injectCSSVarsOnServer,
910
injectUseCssVarsOption,
1011
injectUseCssVarsSetup,
1112
} from '../inject-cssvars'
1213
import { parserCompiledSfc } from '../../parser'
14+
import { CSSVarsBindingTypes } from '../../parser/utils'
1315

1416
describe('inject-cssvars', () => {
1517
describe('createUCVCOptionUnHas', () => {
@@ -370,5 +372,163 @@ describe('inject-cssvars', () => {
370372
mgcStr)
371373
expect(res2.mgcStr.toString()).toContain(mockContent)
372374
})
375+
376+
test('bindingTypes', () => {
377+
const mockContent = `
378+
const _sfc_main = /* @__PURE__ */ _defineComponent({
379+
__name: "App",
380+
props: {
381+
pcolor: {
382+
type: String
383+
}
384+
},
385+
setup(__props, { expose }) {
386+
const color = ref('red')
387+
let foo = ref('foo')
388+
const color2 = reactive({ color: 'red' })
389+
let foo2 = reactive({ foo: 'foo' })
390+
const head2 = new Map()
391+
const color3 = 'red'
392+
const foo3 = { foo: 'foo' }
393+
const bar3 = 100
394+
const head3 = [1]
395+
396+
let color4 = 'red'
397+
let foo4 = { foo: 'foo' }
398+
let bar4 = 100
399+
let head4 = [1]
400+
401+
const fn = () => {}
402+
const fn2 = function (){}
403+
function fn3(){}
404+
let fn4 = () => {}
405+
let fn5 = function (){}
406+
const fn6 = () => {}
407+
const fn7 = fn()
408+
let fn8 = () => {}
409+
let fn9 = fn4()
410+
const a = {foo: 'foo', bar:'bar', head:{heads: 'hh'}}
411+
const {foo: fooAlias, bar, head: {heads: hh} } = a
412+
return {
413+
color,
414+
foo,
415+
color2,
416+
foo2,
417+
head2,
418+
color3,
419+
foo3,
420+
bar3,
421+
head3,
422+
color4,
423+
foo4,
424+
bar4,
425+
head4,
426+
fn1,
427+
fn2,
428+
fn3,
429+
fn4,
430+
fn5,
431+
fn6,
432+
fn7,
433+
fn8,
434+
fn9,
435+
a,
436+
fooAlias,
437+
bar,
438+
hh,
439+
}
440+
}
441+
})
442+
`
443+
const parseRes = parserCompiledSfc(mockContent)
444+
const mgcStr = new MagicString(mockContent)
445+
const res = injectCSSVars(
446+
[
447+
{ value: 'color', has: true, isRef: true },
448+
{ value: 'foo', has: true, isRef: true },
449+
{ value: 'color2', has: true, isRef: false },
450+
{ value: 'foo2', has: true, isRef: false },
451+
{ value: 'head2', has: true, isRef: false },
452+
{ value: 'color3', has: true, isRef: false },
453+
{ value: 'foo3', has: true, isRef: false },
454+
{ value: 'bar3', has: true, isRef: false },
455+
{ value: 'head3', has: true, isRef: false },
456+
{ value: 'color4', has: true, isRef: false },
457+
{ value: 'foo4', has: true, isRef: false },
458+
{ value: 'bar4', has: true, isRef: false },
459+
{ value: 'head4', has: true, isRef: false },
460+
{ value: 'fn1', has: true, isRef: false },
461+
{ value: 'fn2', has: true, isRef: false },
462+
{ value: 'fn3', has: true, isRef: false },
463+
{ value: 'fn4', has: true, isRef: false },
464+
{ value: 'fn5', has: true, isRef: false },
465+
{ value: 'fn6', has: true, isRef: false },
466+
{ value: 'fn7', has: true, isRef: false },
467+
{ value: 'fn8', has: true, isRef: false },
468+
{ value: 'fn9', has: true, isRef: false },
469+
{ value: 'a', has: true, isRef: false },
470+
{ value: 'fooAlias', has: true, isRef: false },
471+
{ value: 'bar', has: true, isRef: false },
472+
{ value: 'hh', has: true, isRef: false },
473+
] as any,
474+
true,
475+
parseRes,
476+
mgcStr,
477+
{
478+
color: CSSVarsBindingTypes.SETUP_REF,
479+
foo: CSSVarsBindingTypes.SETUP_LET,
480+
color2: CSSVarsBindingTypes.SETUP_REACTIVE_CONST,
481+
foo2: CSSVarsBindingTypes.SETUP_LET,
482+
head2: CSSVarsBindingTypes.SETUP_MAYBE_REF,
483+
color3: CSSVarsBindingTypes.LITERAL_CONST,
484+
foo3: CSSVarsBindingTypes.SETUP_CONST,
485+
bar3: CSSVarsBindingTypes.LITERAL_CONST,
486+
head3: CSSVarsBindingTypes.SETUP_CONST,
487+
color4: CSSVarsBindingTypes.SETUP_LET,
488+
foo4: CSSVarsBindingTypes.SETUP_LET,
489+
bar4: CSSVarsBindingTypes.SETUP_LET,
490+
head4: CSSVarsBindingTypes.SETUP_LET,
491+
fn: CSSVarsBindingTypes.SETUP_CONST,
492+
fn2: CSSVarsBindingTypes.SETUP_CONST,
493+
fn3: CSSVarsBindingTypes.SETUP_CONST,
494+
fn4: CSSVarsBindingTypes.SETUP_LET,
495+
fn5: CSSVarsBindingTypes.SETUP_LET,
496+
fn6: CSSVarsBindingTypes.SETUP_CONST,
497+
fn7: CSSVarsBindingTypes.SETUP_MAYBE_REF,
498+
fn8: CSSVarsBindingTypes.SETUP_LET,
499+
fn9: CSSVarsBindingTypes.SETUP_LET,
500+
a: CSSVarsBindingTypes.SETUP_CONST,
501+
fooAlias:CSSVarsBindingTypes.SETUP_MAYBE_REF,
502+
bar: CSSVarsBindingTypes.SETUP_MAYBE_REF,
503+
hh: CSSVarsBindingTypes.SETUP_MAYBE_REF,
504+
})
505+
expect(res.mgcStr.toString()).toContain(' "75f73e04": _unref(hh),\n' +
506+
' "0104392a": _unref(bar),\n' +
507+
' "2bbe40ae": _unref(fooAlias),\n' +
508+
' "770ddb39": a,\n' +
509+
' "33bad66e": _unref(fn9),\n' +
510+
' "33d70570": _unref(fn8),\n' +
511+
' "33f33472": _unref(fn7),\n' +
512+
' "340f6374": fn6,\n' +
513+
' "342b9276": _unref(fn5),\n' +
514+
' "3447c178": _unref(fn4),\n' +
515+
' "3463f07a": fn3,\n' +
516+
' "34801f7c": fn2,\n' +
517+
' "349c4e7e": _ctx.fn1,\n' +
518+
' "e29ebaa8": _unref(head4),\n' +
519+
' "268cecf6": _unref(bar4),\n' +
520+
' "6bb6f0b2": _unref(foo4),\n' +
521+
' "6f79c2b5": _unref(color4),\n' +
522+
' "e2bae9aa": head3,\n' +
523+
' "26a91bf8": bar3,\n' +
524+
' "6ba8d931": foo3,\n' +
525+
' "6f6bab34": color3,\n' +
526+
' "e2d718ac": _unref(head2),\n' +
527+
' "6b9ac1b0": _unref(foo2),\n' +
528+
' "6f5d93b3": color2,\n' +
529+
' "2a5f3ac4": _unref(foo),\n' +
530+
' "1439c43b": color.value,')
531+
expect(res.mgcStr.toString()).toContain('_useCssVars')
532+
})
373533
})
374534
})

Diff for: packages/core/parser/__test__/parser-script-binding.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ describe('parse sfc to set bindings type', () => {
5555
color: 'literal-const',
5656
foo: 'setup-const',
5757
bar: 'literal-const',
58-
head: "setup-const",
58+
head: 'setup-const',
5959
color1: 'setup-let',
6060
foo1: 'setup-let',
6161
bar1: 'setup-let',
6262
head1: 'setup-let',
63-
head2: "setup-maybe-ref",
63+
head2: 'setup-maybe-ref',
6464
})
6565
})
6666

@@ -115,9 +115,9 @@ describe('parse sfc to set bindings type', () => {
115115
const { descriptor } = parse(mockSFC)
116116
const bindings = analyzeScriptBindings(descriptor)
117117
expect(bindings).toMatchObject({
118-
"fooAlias": "setup-maybe-ref",
119-
"bar": "setup-maybe-ref",
120-
"hh": "setup-maybe-ref"
118+
fooAlias: 'setup-maybe-ref',
119+
bar: 'setup-maybe-ref',
120+
hh: 'setup-maybe-ref',
121121
})
122122
})
123123

Diff for: packages/core/parser/ast-grep-rules.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export const astGrepRules = {
134134
has:
135135
{
136136
kind: 'new_expression',
137-
field: 'value'
137+
field: 'value',
138138
},
139139
},
140140
OBJ_PATTERN_VAL: {

Diff for: packages/core/parser/utils.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const CSSVarsBindingTypes = {
77
*/
88
DATA: 'data' as VueBindingTypes.DATA,
99
/**
10-
* declared as a prop
10+
* declared as a prop
1111
*/
1212
PROPS: 'props' as VueBindingTypes.PROPS,
1313
/**
@@ -16,33 +16,33 @@ export const CSSVarsBindingTypes = {
1616
*/
1717
PROPS_ALIASED: 'props-aliased' as VueBindingTypes.PROPS_ALIASED,
1818
/**
19-
* a let binding (may or may not be a ref)
19+
* a let binding (may or may not be a ref)
2020
*/
2121
SETUP_LET: 'setup-let' as VueBindingTypes.SETUP_LET,
2222
/**
2323
* a const binding that can never be a ref.
2424
* these bindings don't need `unref()` calls when processed in inlined
25-
* template expressions.
25+
* template expressions.
2626
*/
2727
SETUP_CONST: 'setup-const' as VueBindingTypes.SETUP_CONST,
2828
/**
29-
* a const binding that does not need `unref()`, but may be mutated.
29+
* a const binding that does not need `unref()`, but may be mutated.
3030
*/
3131
SETUP_REACTIVE_CONST: 'setup-reactive-const' as VueBindingTypes.SETUP_REACTIVE_CONST,
3232
/**
33-
* a const binding that may be a ref.
33+
* a const binding that may be a ref.
3434
*/
3535
SETUP_MAYBE_REF: 'setup-maybe-ref' as VueBindingTypes.SETUP_MAYBE_REF,
3636
/**
37-
* bindings that are guaranteed to be refs
37+
* bindings that are guaranteed to be refs
3838
*/
3939
SETUP_REF: 'setup-ref' as VueBindingTypes.SETUP_REF,
4040
/**
4141
* declared by other options, e.g. computed, inject
4242
*/
4343
OPTIONS: 'options' as VueBindingTypes.OPTIONS,
4444
/**
45-
* a literal constant, e.g. 'foo', 1, true
45+
* a literal constant, e.g. 'foo', 1, true
4646
*/
4747
LITERAL_CONST: 'literal-const' as VueBindingTypes.LITERAL_CONST,
4848
} as const

0 commit comments

Comments
 (0)