Skip to content

Commit

Permalink
Update transform-comment.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Oct 25, 2024
1 parent c5ea69e commit b2e52b2
Showing 1 changed file with 179 additions and 180 deletions.
359 changes: 179 additions & 180 deletions packages/core/babel/transform-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,195 +31,194 @@ const CALLBACK_LABEL: Record<string, CallbackLabel> = {
'@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<State> = {
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);
}

0 comments on commit b2e52b2

Please sign in to comment.