-
-
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
9 changed files
with
268 additions
and
276 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
Large diffs are not rendered by default.
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,81 @@ | ||
import { | ||
NodeTypes, | ||
ElementNode, | ||
locStub, | ||
Namespaces, | ||
ElementTypes, | ||
VNodeCall | ||
} from '../src' | ||
import { isString, PatchFlags, PatchFlagNames, isArray } from '@vue/shared' | ||
|
||
const leadingBracketRE = /^\[/ | ||
const bracketsRE = /^\[|\]$/g | ||
|
||
// Create a matcher for an object | ||
// where non-static expressions should be wrapped in [] | ||
// e.g. | ||
// - createObjectMatcher({ 'foo': '[bar]' }) matches { foo: bar } | ||
// - createObjectMatcher({ '[foo]': 'bar' }) matches { [foo]: "bar" } | ||
export function createObjectMatcher(obj: Record<string, any>) { | ||
return { | ||
type: NodeTypes.JS_OBJECT_EXPRESSION, | ||
properties: Object.keys(obj).map(key => ({ | ||
type: NodeTypes.JS_PROPERTY, | ||
key: { | ||
type: NodeTypes.SIMPLE_EXPRESSION, | ||
content: key.replace(bracketsRE, ''), | ||
isStatic: !leadingBracketRE.test(key) | ||
}, | ||
value: isString(obj[key]) | ||
? { | ||
type: NodeTypes.SIMPLE_EXPRESSION, | ||
content: obj[key].replace(bracketsRE, ''), | ||
isStatic: !leadingBracketRE.test(obj[key]) | ||
} | ||
: obj[key] | ||
})) | ||
} | ||
} | ||
|
||
export function createElementWithCodegen( | ||
tag: VNodeCall['tag'], | ||
props?: VNodeCall['props'], | ||
children?: VNodeCall['children'], | ||
patchFlag?: VNodeCall['patchFlag'], | ||
dynamicProps?: VNodeCall['dynamicProps'] | ||
): ElementNode { | ||
return { | ||
type: NodeTypes.ELEMENT, | ||
loc: locStub, | ||
ns: Namespaces.HTML, | ||
tag: 'div', | ||
tagType: ElementTypes.ELEMENT, | ||
isSelfClosing: false, | ||
props: [], | ||
children: [], | ||
codegenNode: { | ||
type: NodeTypes.VNODE_CALL, | ||
tag, | ||
props, | ||
children, | ||
patchFlag, | ||
dynamicProps, | ||
directives: undefined, | ||
isBlock: false, | ||
isForBlock: false, | ||
loc: locStub | ||
} | ||
} | ||
} | ||
|
||
export function genFlagText(flag: PatchFlags | PatchFlags[]) { | ||
if (isArray(flag)) { | ||
let f = 0 | ||
flag.forEach(ff => { | ||
f |= ff | ||
}) | ||
return `${f} /* ${flag.map(f => PatchFlagNames[f]).join(', ')} */` | ||
} else { | ||
return `${flag} /* ${PatchFlagNames[flag]} */` | ||
} | ||
} |
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,72 @@ | ||
// todo: uncomment when vue-next#907 is released | ||
test.todo('uncomment when vue-next#907 is released') | ||
// import { | ||
// baseParse as parse, | ||
// transform, | ||
// PlainElementNode, | ||
// CompilerOptions, | ||
// transformElement | ||
// } from '@vue/compiler-core' | ||
// import { transformVText } from '../../src/transforms/vText' | ||
// import { | ||
// createObjectMatcher, | ||
// genFlagText | ||
// } from '../testUtils' | ||
// import { PatchFlags } from '@vue/shared' | ||
// import { DOMErrorCodes } from '../../src/errors' | ||
// | ||
// function transformWithVText(template: string, options: CompilerOptions = {}) { | ||
// const ast = parse(template) | ||
// transform(ast, { | ||
// nodeTransforms: [transformElement], | ||
// directiveTransforms: { | ||
// text: transformVText | ||
// }, | ||
// ...options | ||
// }) | ||
// return ast | ||
// } | ||
// | ||
// describe('compiler: v-text transform', () => { | ||
// it('should convert v-text to text', () => { | ||
// const ast = transformWithVText(`<Label v-text="test"/>`) | ||
// expect((ast.children[0] as PlainElementNode).codegenNode).toMatchObject({ | ||
// tag: `_component_Label`, | ||
// props: createObjectMatcher({ | ||
// text: `[test]` | ||
// }), | ||
// children: undefined, | ||
// patchFlag: genFlagText(PatchFlags.PROPS), | ||
// dynamicProps: `["text"]` | ||
// }) | ||
// }) | ||
// | ||
// it('should raise error and ignore children when v-text is present', () => { | ||
// const onError = jest.fn() | ||
// const ast = transformWithVText(`<Label v-text="test">hello</Label>`, { | ||
// onError | ||
// }) | ||
// expect(onError.mock.calls).toMatchObject([ | ||
// [{ code: DOMErrorCodes.X_V_TEXT_WITH_CHILDREN }] | ||
// ]) | ||
// expect((ast.children[0] as PlainElementNode).codegenNode).toMatchObject({ | ||
// tag: `_component_Label`, | ||
// props: createObjectMatcher({ | ||
// text: `[test]` | ||
// }), | ||
// children: undefined, // <-- children should have been removed | ||
// patchFlag: genFlagText(PatchFlags.PROPS), | ||
// dynamicProps: `["text"]` | ||
// }) | ||
// }) | ||
// | ||
// it('should raise error if has no expression', () => { | ||
// const onError = jest.fn() | ||
// transformWithVText(`<Label v-text></Label>`, { | ||
// onError | ||
// }) | ||
// expect(onError.mock.calls).toMatchObject([ | ||
// [{ code: DOMErrorCodes.X_V_TEXT_NO_EXPRESSION }] | ||
// ]) | ||
// }) | ||
// }) |
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,31 @@ | ||
import { | ||
DirectiveTransform, | ||
createObjectProperty, | ||
createSimpleExpression | ||
} from '@vue/compiler-core' | ||
import { createDOMCompilerError, DOMErrorCodes } from '../errors' | ||
|
||
export const transformVText: DirectiveTransform = (dir, node, context) => { | ||
const { exp, loc } = dir | ||
if (!exp) { | ||
context.onError( | ||
createDOMCompilerError(DOMErrorCodes.X_V_TEXT_NO_EXPRESSION, loc) | ||
) | ||
} | ||
if (node.children.length) { | ||
context.onError( | ||
createDOMCompilerError(DOMErrorCodes.X_V_TEXT_WITH_CHILDREN, loc) | ||
) | ||
node.children.length = 0 | ||
} | ||
return { | ||
props: [ | ||
createObjectProperty( | ||
// todo: check if we should implement via a generic textContent | ||
// and handle in runtime module? | ||
createSimpleExpression(`text`, true, loc), | ||
exp || createSimpleExpression('', true) | ||
) | ||
] | ||
} | ||
} |
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