-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
149 additions
and
24 deletions.
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
packages/compiler/__tests__/transforms/__snapshots__/vModel.spec.ts.snap
This file was deleted.
Oops, something went wrong.
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,86 @@ | ||
// todo: uncomment when vue-next#907 is released | ||
test.todo('uncomment when vue-next#907 is released') | ||
import { | ||
baseParse as parse, | ||
transform, | ||
ElementNode, | ||
CompilerOptions, | ||
NodeTypes, | ||
VNodeCall, | ||
transformElement | ||
} from '../../src' | ||
import { transformOn } from '../../src/transforms/vOn' | ||
|
||
function parseWithVOn(template: string, options: CompilerOptions = {}) { | ||
const ast = parse(template) | ||
transform(ast, { | ||
nodeTransforms: [transformElement], | ||
directiveTransforms: { | ||
on: transformOn | ||
}, | ||
...options | ||
}) | ||
return { | ||
root: ast, | ||
node: ast.children[0] as ElementNode | ||
} | ||
} | ||
|
||
describe('compiler: transform v-on', () => { | ||
test('basic', () => { | ||
const { node } = parseWithVOn(`<Label v-on:click="onClick"/>`) | ||
expect((node.codegenNode as VNodeCall).props).toMatchObject({ | ||
properties: [ | ||
{ | ||
key: { | ||
content: `onClick`, | ||
isStatic: true, | ||
loc: { | ||
start: { | ||
line: 1, | ||
column: 13 | ||
}, | ||
end: { | ||
line: 1, | ||
column: 18 | ||
} | ||
} | ||
}, | ||
value: { | ||
content: `onClick`, | ||
isStatic: false, | ||
loc: { | ||
start: { | ||
line: 1, | ||
column: 20 | ||
}, | ||
end: { | ||
line: 1, | ||
column: 27 | ||
} | ||
} | ||
} | ||
} | ||
] | ||
}) | ||
}) | ||
|
||
test('dynamic arg', () => { | ||
const { node } = parseWithVOn(`<Label v-on:[event]="handler"/>`) | ||
expect((node.codegenNode as VNodeCall).props).toMatchObject({ | ||
properties: [ | ||
{ | ||
key: { | ||
type: NodeTypes.COMPOUND_EXPRESSION, | ||
children: [`"on" + (`, { content: `event` }, `)`] | ||
}, | ||
value: { | ||
type: NodeTypes.SIMPLE_EXPRESSION, | ||
content: `handler`, | ||
isStatic: false | ||
} | ||
} | ||
] | ||
}) | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
transformOn as baseTransform, | ||
DirectiveTransform, | ||
createObjectProperty, | ||
createObjectExpression, | ||
createSimpleExpression | ||
} from '@vue/compiler-core' | ||
import { makeMap } from '@vue/shared' | ||
|
||
// todo: simplify - we only support `once` - no need to use makeMap | ||
const isEventOptionModifier = /*#__PURE__*/ makeMap(`once`) | ||
|
||
// todo: remove - no need to loop through modifiers as only `once` is supported | ||
const generateOptionModifiers = (modifiers: string[]) => { | ||
const eventOptionModifiers = [] | ||
|
||
for (let i = 0; i < modifiers.length; i++) { | ||
const modifier = modifiers[i] | ||
|
||
if (isEventOptionModifier(modifier)) { | ||
// eventOptionModifiers: modifiers for addEventListener() options, e.g. .once | ||
eventOptionModifiers.push(modifier) | ||
} | ||
} | ||
|
||
return eventOptionModifiers | ||
} | ||
|
||
export const transformOn: DirectiveTransform = (dir, node, context) => { | ||
return baseTransform(dir, node, context, baseResult => { | ||
const { modifiers } = dir | ||
if (!modifiers.length) return baseResult | ||
|
||
let { key, value: handlerExp } = baseResult.props[0] | ||
const eventOptionModifiers = generateOptionModifiers(modifiers) | ||
|
||
if (eventOptionModifiers.length) { | ||
handlerExp = createObjectExpression([ | ||
createObjectProperty('handler', handlerExp), | ||
createObjectProperty( | ||
'options', | ||
createObjectExpression( | ||
eventOptionModifiers.map(modifier => | ||
createObjectProperty( | ||
modifier, | ||
createSimpleExpression('true', false) | ||
) | ||
) | ||
) | ||
) | ||
]) | ||
} | ||
|
||
return { | ||
props: [createObjectProperty(key, handlerExp)] | ||
} | ||
}) | ||
} |