fix: fix all biome
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import fsp from 'node:fs/promises';
|
||||
import { type MagicString, type Statement, parseSync } from 'oxc-parser';
|
||||
import { type Node, walk } from 'oxc-walker';
|
||||
import {
|
||||
type ArrowFunctionExpression,
|
||||
// biome-ignore lint/suspicious/noShadowRestrictedNames: <explanation>
|
||||
type Function,
|
||||
type MagicString,
|
||||
type Statement,
|
||||
parseSync,
|
||||
} from 'oxc-parser';
|
||||
import { walk } from 'oxc-walker';
|
||||
|
||||
function sourceTextFromNode(
|
||||
context: { magicString?: MagicString },
|
||||
node: Node
|
||||
node: { start: number; end: number }
|
||||
): string {
|
||||
const magicString = context.magicString;
|
||||
assert(magicString, 'magicString should be defined');
|
||||
@@ -33,53 +40,101 @@ export async function rewriteObservableSubscribeToLastValueFrom(
|
||||
child.type === 'ExpressionStatement' &&
|
||||
child.expression.type === 'CallExpression' &&
|
||||
child.expression.callee.type === 'StaticMemberExpression' &&
|
||||
child.expression.callee.property.name === 'subscribe' &&
|
||||
child.expression.arguments.length === 0
|
||||
child.expression.callee.property.name === 'subscribe'
|
||||
) {
|
||||
const newContent = `await lastValueFrom(${sourceTextFromNode(context, child.expression.callee.object)});`;
|
||||
let next: ArrowFunctionExpression | Function | undefined;
|
||||
let error: ArrowFunctionExpression | Function | undefined;
|
||||
let complete: ArrowFunctionExpression | Function | undefined;
|
||||
|
||||
const newStatements = parseSync('index.ts', newContent).program
|
||||
.body as any[];
|
||||
if (child.expression.arguments[0]?.type === 'ObjectExpression') {
|
||||
const obj = child.expression.arguments[0];
|
||||
for (const prop of obj.properties) {
|
||||
if (
|
||||
prop.type === 'ObjectProperty' &&
|
||||
prop.key.type === 'Identifier' &&
|
||||
(prop.value.type === 'FunctionExpression' ||
|
||||
prop.value.type === 'ArrowFunctionExpression')
|
||||
) {
|
||||
if (prop.key.name === 'next') {
|
||||
next = prop.value;
|
||||
} else if (prop.key.name === 'error') {
|
||||
error = prop.value;
|
||||
} else if (prop.key.name === 'complete') {
|
||||
complete = prop.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
child.expression.arguments.find(
|
||||
(arg) =>
|
||||
arg.type === 'FunctionExpression' ||
|
||||
arg.type === 'ArrowFunctionExpression'
|
||||
)
|
||||
) {
|
||||
const args: Array<
|
||||
Function | ArrowFunctionExpression | undefined
|
||||
> = child.expression.arguments.map((arg) =>
|
||||
arg.type === 'FunctionExpression' ||
|
||||
arg.type === 'ArrowFunctionExpression'
|
||||
? arg
|
||||
: undefined
|
||||
);
|
||||
next = args[0];
|
||||
error = args[1];
|
||||
complete = args[2];
|
||||
}
|
||||
let newContent = `await lastValueFrom(${sourceTextFromNode(context, child.expression.callee.object)});`;
|
||||
|
||||
magicString.remove(child.start, child.end);
|
||||
magicString.appendRight(child.start, newContent);
|
||||
if (next) {
|
||||
const nextParam =
|
||||
next?.params?.items?.[0]?.type === 'FormalParameter'
|
||||
? sourceTextFromNode(context, next.params.items[0])
|
||||
: undefined;
|
||||
|
||||
newChildren.push(...newStatements);
|
||||
} else if (
|
||||
child.type === 'ExpressionStatement' &&
|
||||
child.expression.type === 'CallExpression' &&
|
||||
child.expression.callee.type === 'StaticMemberExpression' &&
|
||||
child.expression.callee.property.name === 'subscribe' &&
|
||||
child.expression.arguments[0]?.type === 'ArrowFunctionExpression' &&
|
||||
child.expression.arguments[0].body.type === 'FunctionBody'
|
||||
) {
|
||||
const awaited =
|
||||
child.expression.arguments[0].params.kind ===
|
||||
'ArrowFormalParameters' &&
|
||||
child.expression.arguments[0].params.items[0]?.type ===
|
||||
'FormalParameter' &&
|
||||
child.expression.arguments[0].params.items[0].pattern.type ===
|
||||
'Identifier'
|
||||
? child.expression.arguments[0].params.items[0].pattern.name
|
||||
: undefined;
|
||||
const newContent =
|
||||
(awaited
|
||||
? `const ${awaited} = await lastValueFrom(${sourceTextFromNode(
|
||||
context,
|
||||
child.expression.callee.object
|
||||
)});\n`
|
||||
: `await lastValueFrom(${sourceTextFromNode(context, child.expression.callee.object)});\n`) +
|
||||
child.expression.arguments[0].body.statements
|
||||
if (nextParam) {
|
||||
newContent = `const ${nextParam} = ${newContent}`;
|
||||
}
|
||||
newContent += (next.body?.statements || [])
|
||||
.map((s) => sourceTextFromNode(context, s))
|
||||
.join(';\n');
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
const newStatements = parseSync('index.ts', newContent).program
|
||||
.body as any[];
|
||||
if (error || complete) {
|
||||
const errorParam =
|
||||
error?.params?.items?.[0]?.type === 'FormalParameter' &&
|
||||
error.params.items[0].pattern.type === 'Identifier'
|
||||
? sourceTextFromNode(context, error.params.items[0])
|
||||
: 'err';
|
||||
const errorParamName =
|
||||
error?.params?.items?.[0]?.type === 'FormalParameter' &&
|
||||
error.params.items[0].pattern.type === 'Identifier'
|
||||
? error.params.items[0].pattern.name
|
||||
: 'err';
|
||||
|
||||
let errorBody = '';
|
||||
if (error) {
|
||||
errorBody += (error.body?.statements || [])
|
||||
.map((s) => sourceTextFromNode(context, s))
|
||||
.join('\n');
|
||||
}
|
||||
if (complete) {
|
||||
const completBody = `if (${errorParamName} instanceof EmptyError) { ${(complete.body?.statements || []).map((s) => sourceTextFromNode(context, s)).join('\n')}}`;
|
||||
if (errorBody) {
|
||||
errorBody = `${completBody} else { ${errorBody} }`;
|
||||
} else {
|
||||
errorBody = completBody;
|
||||
}
|
||||
}
|
||||
|
||||
newContent = `try { ${newContent} } catch (${errorParam}) { ${errorBody} }`;
|
||||
}
|
||||
|
||||
const newNodes = parseSync('index.html', newContent).program.body;
|
||||
|
||||
magicString.remove(child.start, child.end);
|
||||
magicString.appendRight(child.start, newContent);
|
||||
magicString.appendLeft(child.start, newContent);
|
||||
|
||||
newChildren.push(...newStatements);
|
||||
newChildren.push(...newNodes);
|
||||
} else {
|
||||
newChildren.push(child as any);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user