-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(svelte-template-compiler): first template ref implementation
- Loading branch information
Showing
10 changed files
with
237 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/svelte-template-compiler/src/transforms/__snapshots__/templateRef.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`native element > expected 1`] = ` | ||
"import { setRef as _setRef, template as _template } from 'vue/vapor'; | ||
const t0 = _template("<canvas></canvas>") | ||
export function render(_ctx) { | ||
const n0 = t0() | ||
_setRef(n0, "el") | ||
return n0 | ||
}" | ||
`; | ||
|
||
exports[`native element > received 1`] = ` | ||
"import { setRef as _setRef, template as _template } from 'vue/vapor'; | ||
const t0 = _template("<canvas></canvas>") | ||
export function render(_ctx) { | ||
const n0 = t0() | ||
_setRef(n0, "el") | ||
return n0 | ||
}" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 68 additions & 3 deletions
71
packages/svelte-template-compiler/src/transforms/templateRef.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,71 @@ | ||
import { NodeTypes } from '@vue-vapor/compiler-dom' | ||
import { compile as vaporCompile } from '@vue-vapor/compiler-vapor' | ||
import { expect, test } from 'vitest' | ||
import { IRNodeTypes } from '../ir/index.ts' | ||
import { makeCompile } from './_utils.ts' | ||
import { transformVBind } from './bind.ts' | ||
import { transformChildren } from './children.ts' | ||
import { transformComment } from './comment.ts' | ||
import { transformElement } from './element.ts' | ||
import { transformVModel } from './model.ts' | ||
import { transformVOn } from './on.ts' | ||
import { transformTemplateRef } from './templateRef.ts' | ||
import { transformText } from './text.ts' | ||
|
||
test.todo('basic', () => { | ||
const source = '<div bind:this={foo} />' | ||
expect(source).toBe('todo') | ||
const compileWithTransformRef = makeCompile({ | ||
prefixIdentifiers: false, | ||
nodeTransforms: [ | ||
transformElement, | ||
transformChildren, | ||
transformText, | ||
transformComment, | ||
transformTemplateRef | ||
], | ||
directiveTransforms: { | ||
bind: transformVBind, | ||
on: transformVOn, | ||
model: transformVModel | ||
} | ||
}) | ||
|
||
test('native element', () => { | ||
const source1 = '<canvas bind:this={el} />' | ||
const source2 = '<canvas ref="el" />' | ||
const { code, ir } = compileWithTransformRef(source1) | ||
const expectedResult = vaporCompile(source2) | ||
|
||
expect(code).toMatchSnapshot('received') | ||
expect(expectedResult.code).toMatchSnapshot('expected') | ||
|
||
expect(code).contains(`_setRef(n0, "el")`) | ||
|
||
expect(ir.template).toEqual(['<canvas></canvas>']) | ||
expect(ir.block.operation).toMatchObject([ | ||
{ | ||
type: IRNodeTypes.SET_TEMPLATE_REF, | ||
element: 0, | ||
effect: false, | ||
refFor: false, | ||
value: { | ||
type: NodeTypes.SIMPLE_EXPRESSION, | ||
content: 'el', | ||
isStatic: true, | ||
loc: { | ||
// TODO: we need to align svelte AST source location | ||
source: 'el' | ||
} | ||
} | ||
} | ||
]) | ||
}) | ||
|
||
test.todo('component', () => { | ||
const source1 = '<MyComp bind:this={instance} />' | ||
const source2 = '<MyComp ref="instance" />' | ||
|
||
const { code, ir: _ } = compileWithTransformRef(source1) | ||
const expectedResult = vaporCompile(source2) | ||
|
||
expect(code).toMatchSnapshot('received') | ||
expect(expectedResult.code).toMatchSnapshot('expected') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters