From b2e52b253181f266fd0e7bbdaaed58a56a6b62c6 Mon Sep 17 00:00:00 2001 From: "Alexis H. Munsayac" Date: Fri, 25 Oct 2024 13:43:20 +0800 Subject: [PATCH] Update transform-comment.ts --- packages/core/babel/transform-comment.ts | 359 +++++++++++------------ 1 file changed, 179 insertions(+), 180 deletions(-) diff --git a/packages/core/babel/transform-comment.ts b/packages/core/babel/transform-comment.ts index 7af9cc1..629e264 100644 --- a/packages/core/babel/transform-comment.ts +++ b/packages/core/babel/transform-comment.ts @@ -31,195 +31,194 @@ const CALLBACK_LABEL: Record = { '@transition': ['startTransition', 'solid-js', false], }; -export default function transformComments( +function getVariableLabelPreference( state: State, - path: babel.NodePath, -): void { - path.traverse({ - VariableDeclaration(p) { - const comments = p.node.leadingComments; - if (comments) { - let preference: string | undefined; - for (let i = 0, len = comments.length; i < len; i++) { - const comment = comments[i]; - const value: string = comment.value.trim(); - if ( - value in VARIABLE_LABEL && - !state.opts.disabled?.pragma?.[value] - ) { - preference = value; - comment.value = ''; - } - } - if (preference) { - const { declarations } = p.node; - let declarators: t.VariableDeclarator[] = []; - for (let i = 0, len = declarations.length; i < len; i++) { - const declarator = declarations[i]; - switch (preference as keyof typeof VARIABLE_LABEL) { - case '@signal': - if (t.isIdentifier(declarator.id)) { - declarators.push( - signalVariable( - state, - p, - declarator.id, - declarator.init ?? t.identifier('undefined'), - ), - ); - } - break; - case '@memo': - if (t.isIdentifier(declarator.id)) { - declarators.push( - memoVariable( - state, - p, - declarator.id, - declarator.init ?? t.identifier('undefined'), - ), - ); - } - break; - case '@deferred': - if (t.isIdentifier(declarator.id)) { - declarators.push( - deferredVariable( - state, - p, - declarator.id, - declarator.init ?? t.identifier('undefined'), - ), - ); - } - break; - case '@destructure': - if ( - (t.isObjectPattern(declarator.id) || - t.isArrayPattern(declarator.id)) && - declarator.init - ) { - declarators = [ - ...declarators, - ...destructureVariable( - state, - p, - declarator.init, - declarator.id, - ), - ]; - } - break; - case '@children': - if (t.isIdentifier(declarator.id)) { - declarators.push( - accessorVariable( - p, - declarator.id, - getImportIdentifier(state, p, 'children', 'solid-js'), - [ - t.arrowFunctionExpression( - [], - declarator.init ?? t.identifier('undefined'), - ), - ], - ), - ); - } - break; - default: - break; - } - } + comments: t.Comment[], +): string | undefined { + let preference: string | undefined; + for (let i = 0, len = comments.length; i < len; i++) { + const comment = comments[i]; + const value: string = comment.value.trim(); + if (value in VARIABLE_LABEL && !state.opts.disabled?.pragma?.[value]) { + preference = value; + comment.value = ''; + } + } + return preference; +} - p.replaceWith(t.variableDeclaration('const', declarators)); - } - } - }, - BlockStatement(p) { - if (!t.isBlockStatement(p.parent)) { - return; +function getCallbackLabelPreference(state: State, comments: t.Comment[]) { + let preference: string | undefined; + let nameOption: string | undefined; + for (let i = 0, len = comments.length; i < len; i++) { + const comment = comments[i]; + const value: string = comment.value.trim(); + if (/^@\w+( .*)?$/.test(value)) { + const [tag, ...debugName] = value.split(' '); + if (tag in CALLBACK_LABEL && !state.opts.disabled?.pragma?.[value]) { + preference = tag; + nameOption = debugName.join(' '); + comment.value = ''; } - const comments = p.node.leadingComments; - if (comments) { - let preference: string | undefined; - let nameOption: string | undefined; - for (let i = 0, len = comments.length; i < len; i++) { - const comment = comments[i]; - const value: string = comment.value.trim(); - if (/^@\w+( .*)?$/.test(value)) { - const [tag, ...debugName] = value.split(' '); - if ( - tag in CALLBACK_LABEL && - !state.opts.disabled?.pragma?.[value] - ) { - preference = tag; - nameOption = debugName.join(' '); - comment.value = ''; - } + } + } + return [preference, nameOption]; +} + +const COMMENT_TRAVERSE: babel.Visitor = { + VariableDeclaration(path, state) { + const comments = path.node.leadingComments; + if (comments) { + const preference = getVariableLabelPreference(state, comments); + if (preference) { + const { declarations } = path.node; + let declarators: t.VariableDeclarator[] = []; + for (let i = 0, len = declarations.length; i < len; i++) { + const declarator = declarations[i]; + switch (preference as keyof typeof VARIABLE_LABEL) { + case '@signal': + if (t.isIdentifier(declarator.id)) { + declarators.push( + signalVariable( + state, + path, + declarator.id, + declarator.init ?? t.identifier('undefined'), + ), + ); + } + break; + case '@memo': + if (t.isIdentifier(declarator.id)) { + declarators.push( + memoVariable( + state, + path, + declarator.id, + declarator.init ?? t.identifier('undefined'), + ), + ); + } + break; + case '@deferred': + if (t.isIdentifier(declarator.id)) { + declarators.push( + deferredVariable( + state, + path, + declarator.id, + declarator.init ?? t.identifier('undefined'), + ), + ); + } + break; + case '@destructure': + if ( + (t.isObjectPattern(declarator.id) || + t.isArrayPattern(declarator.id)) && + declarator.init + ) { + declarators = [ + ...declarators, + ...destructureVariable( + state, + path, + declarator.init, + declarator.id, + ), + ]; + } + break; + case '@children': + if (t.isIdentifier(declarator.id)) { + declarators.push( + accessorVariable( + path, + declarator.id, + getImportIdentifier(state, path, 'children', 'solid-js'), + [ + t.arrowFunctionExpression( + [], + declarator.init ?? t.identifier('undefined'), + ), + ], + ), + ); + } + break; + default: + break; } } - if (preference) { - const [name, source, named] = CALLBACK_LABEL[preference]; - const callback = t.arrowFunctionExpression([], p.node); - const args: t.Expression[] = [callback]; - if (named && nameOption) { - args.push( - t.identifier('undefined'), - t.objectExpression([ - t.objectProperty( - t.identifier('name'), - t.stringLiteral(nameOption), - ), - ]), - ); - } - p.replaceWith( - t.callExpression(getImportIdentifier(state, p, name, source), args), + + path.replaceWith(t.variableDeclaration('const', declarators)); + } + } + }, + BlockStatement(p, state) { + if (!t.isBlockStatement(p.parent)) { + return; + } + const comments = p.node.leadingComments; + if (comments) { + const [preference, nameOption] = getCallbackLabelPreference( + state, + comments, + ); + if (preference) { + const [name, source, named] = CALLBACK_LABEL[preference]; + const callback = t.arrowFunctionExpression([], p.node); + const args: t.Expression[] = [callback]; + if (named && nameOption) { + args.push( + t.identifier('undefined'), + t.objectExpression([ + t.objectProperty( + t.identifier('name'), + t.stringLiteral(nameOption), + ), + ]), ); } + p.replaceWith( + t.callExpression(getImportIdentifier(state, p, name, source), args), + ); } - }, - ExpressionStatement(p) { - const comments = p.node.leadingComments; - if (comments) { - let preference: string | undefined; - let nameOption: string | undefined; - for (let i = 0, len = comments.length; i < len; i++) { - const comment = comments[i]; - const value: string = comment.value.trim(); - if (/^@\w+( .*)?$/.test(value)) { - const [tag, ...debugName] = value.split(' '); - if ( - tag in CALLBACK_LABEL && - !state.opts.disabled?.pragma?.[value] - ) { - preference = tag; - nameOption = debugName.join(' '); - comment.value = ''; - } - } - } - if (preference) { - const [name, source, named] = CALLBACK_LABEL[preference]; - const callback = t.arrowFunctionExpression([], p.node.expression); - const args: t.Expression[] = [callback]; - if (named && nameOption) { - args.push( - t.identifier('undefined'), - t.objectExpression([ - t.objectProperty( - t.identifier('name'), - t.stringLiteral(nameOption), - ), - ]), - ); - } - p.replaceWith( - t.callExpression(getImportIdentifier(state, p, name, source), args), + } + }, + ExpressionStatement(p, state) { + const comments = p.node.leadingComments; + if (comments) { + const [preference, nameOption] = getCallbackLabelPreference( + state, + comments, + ); + if (preference) { + const [name, source, named] = CALLBACK_LABEL[preference]; + const callback = t.arrowFunctionExpression([], p.node.expression); + const args: t.Expression[] = [callback]; + if (named && nameOption) { + args.push( + t.identifier('undefined'), + t.objectExpression([ + t.objectProperty( + t.identifier('name'), + t.stringLiteral(nameOption), + ), + ]), ); } + p.replaceWith( + t.callExpression(getImportIdentifier(state, p, name, source), args), + ); } - }, - }); + } + }, +}; + +export default function transformComments( + state: State, + path: babel.NodePath, +): void { + path.traverse(COMMENT_TRAVERSE, state); }