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

View File

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

View File

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

View File

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