style: optimize code

This commit is contained in:
Amour1688 2020-10-19 14:50:25 +08:00
parent f50b9e37fd
commit 1fdcfd0ce2
4 changed files with 11 additions and 12 deletions

View File

@ -125,7 +125,7 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
const isComponent = checkIsComponent(path.get('openingElement'));
const props = path.get('openingElement').get('attributes');
const directives: t.ArrayExpression[] = [];
const dynamicPropNames = new Set();
const dynamicPropNames = new Set<string>();
let slots: Slots = null;
let patchFlag = 0;
@ -225,8 +225,7 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
// must be v-model and is a component
properties.push(t.objectProperty(
arg || t.stringLiteral('modelValue'),
// @ts-ignore
value,
value as any,
));
dynamicPropNames.add(propName);
@ -263,8 +262,7 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
t.stringLiteral(`onUpdate:${propName}`),
t.arrowFunctionExpression(
[t.identifier('$event')],
// @ts-ignore
t.assignmentExpression('=', value, t.identifier('$event')),
t.assignmentExpression('=', value as any, t.identifier('$event')),
),
));

View File

@ -27,7 +27,7 @@ const getType = (path: NodePath<t.JSXOpeningElement>) => {
const parseModifiers = (value: t.Expression) => {
let modifiers: string[] = [];
if (t.isArrayExpression(value)) {
modifiers = (value as t.ArrayExpression).elements
modifiers = value.elements
.map((el) => (t.isStringLiteral(el) ? el.value : '')).filter(Boolean);
}
return modifiers;
@ -61,7 +61,7 @@ const parseDirectives = (args: {
|| (directiveName === 'model' && !isComponent);
if (t.isArrayExpression(value)) {
const { elements } = value as t.ArrayExpression;
const { elements } = value;
const [first, second, third] = elements;
if (t.isStringLiteral(second)) {
arg = second;
@ -86,7 +86,7 @@ const parseDirectives = (args: {
!!modifiersSet.size && t.objectExpression(
[...modifiersSet].map(
(modifier) => t.objectProperty(
t.identifier(modifier as string),
t.identifier(modifier),
t.booleanLiteral(true),
),
),

View File

@ -39,7 +39,7 @@ const getChildren = (
const expression = transformJSXExpressionContainer(path);
if (t.isIdentifier(expression)) {
const { name } = expression as t.Identifier;
const { name } = expression;
const { referencePaths = [] } = path.scope.getBinding(name) || {};
referencePaths.forEach((referencePath) => {
walksScope(referencePath, name, SlotFlags.DYNAMIC);
@ -109,7 +109,7 @@ const transformJSXElement = (
!!patchFlag && optimize && t.numericLiteral(patchFlag),
!!dynamicPropNames.size && optimize
&& t.arrayExpression(
[...dynamicPropNames.keys()].map((name) => t.stringLiteral(name as string)),
[...dynamicPropNames.keys()].map((name) => t.stringLiteral(name)),
),
].filter(Boolean as any));

View File

@ -187,8 +187,9 @@ const transformJSXText = (path: NodePath<t.JSXText>): t.StringLiteral | null =>
*/
const transformJSXExpressionContainer = (
path: NodePath<t.JSXExpressionContainer>,
): (t.Expression
) => path.get('expression').node as t.Expression;
): (
t.Expression
) => path.get('expression').node as t.Expression;
/**
* Transform JSXSpreadChild