Skip to content

Commit

Permalink
feat: add transform function to annotate a function to be transform…
Browse files Browse the repository at this point in the history
…ed by the vite plugin
  • Loading branch information
paoloricciuti committed Oct 31, 2024
1 parent c32c994 commit 81c1bba
Show file tree
Hide file tree
Showing 13 changed files with 716 additions and 25 deletions.
3 changes: 2 additions & 1 deletion packages/svelte/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ node_modules
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
coverage
coverage
src/lib/tests/expected-transforms/**/_actual.js
1 change: 1 addition & 0 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
},
"dependencies": {
"acorn": "^8.11.3",
"esm-env": "^1.1.4",
"esrap": "^1.2.2",
"zimmerframe": "^1.1.2"
},
Expand Down
10 changes: 6 additions & 4 deletions packages/svelte/src/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ export class CancelationError extends Error {
}
}

export type TaskFunction<TArgs = unknown, TReturn = unknown> = (
args: TArgs,
utils: SheepdogUtils,
) => Promise<TReturn> | AsyncGenerator<unknown, TReturn, unknown>;

export function createTask<TArgs = unknown, TReturn = unknown, TModifier = object>(
adapter: TaskAdapter<TReturn, TModifier>,
gen_or_fun: (
args: TArgs,
utils: SheepdogUtils,
) => Promise<TReturn> | AsyncGenerator<unknown, TReturn, unknown>,
gen_or_fun: TaskFunction<TArgs, TReturn>,
options?: TaskOptions,
) {
const handler_factory = handlers[options?.kind ?? 'default'];
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Reexport your entry components here
import { didCancel, timeout } from './utils';
import { didCancel, timeout, transform } from './utils';
import { task, CancelationError } from './task.js';
export type { Task, SheepdogUtils, TaskInstance } from './task.js';

export { task, CancelationError, didCancel, timeout };
export { task, CancelationError, didCancel, timeout, transform };
7 changes: 2 additions & 5 deletions packages/svelte/src/lib/task.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { onDestroy } from 'svelte';
import { writable } from 'svelte/store';
import type { HandlerType, HandlersMap, SheepdogUtils, TaskOptions } from './core';
import type { HandlerType, HandlersMap, SheepdogUtils, TaskOptions, TaskFunction } from './core';
import { CancelationError, createTask, handlers } from './core';
import type { ReadableWithGet, WritableWithGet } from './internal/helpers';
import { writable_with_get } from './internal/helpers';
Expand All @@ -22,10 +22,7 @@ export type TaskInstance<TReturn = undefined> = {
};

function _task<TArgs = unknown, TReturn = undefined>(
gen_or_fun: (
args: TArgs,
utils: SheepdogUtils,
) => Promise<TReturn> | AsyncGenerator<unknown, TReturn, unknown>,
gen_or_fun: TaskFunction<TArgs, TReturn>,
options?: TaskOptions,
) {
const { subscribe, ...result } = writable({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { transform as transform_renamed } from '@sheepdog/svelte/utils';

/**
* @param {()=>any} fn
*/
function transform(fn) {
return fn;
}

/**
* @param {number} val
*/
function fn(val) {
return val;
}

/**
* @type {Record<string,()=>void>}
*/
const fns = {};

/**
*
* @param {TemplateStringsArray} quasi
* @param {string} strings
*/
function str(quasi, strings) {}

export const task_fn = transform_renamed(async () => {
const assign = await Promise.resolve();
let assignment = null;
assignment = await Promise.resolve();
const array = [await Promise.resolve()];
const sum = (await Promise.resolve(1)) + 2;
const sum2 = 2 + (await Promise.resolve(1));
fns[await Promise.resolve(0)]();
const [array_destructure = await Promise.resolve()] = [undefined];
const function_call = fn(await Promise.resolve(2));
const conditional1 = (await Promise.resolve(true)) ? 1 : 2;
const conditional2 = true ? await Promise.resolve(1) : 2;
const conditional3 = false ? 1 : await Promise.resolve(2);
const member = { property: null };
member[await Promise.resolve('property')] = await Promise.resolve(null);
const logical1 = (await Promise.resolve(null)) ?? 3;
const logical2 = null ?? (await Promise.resolve(null));
const logical3 = null || (await Promise.resolve(null));
const logical4 = (await Promise.resolve(null)) || null;
const logical5 = (await Promise.resolve(null)) && null;
const logical6 = true && (await Promise.resolve(null));
const object_expression1 = { awaited: await Promise.resolve(2) };
const object_expression2 = { awaited: { nested: await Promise.resolve(2) } };
const object_expression3 = { awaited: { nested: [await Promise.resolve(2)] } };
const tagged_template = str`something ${await Promise.resolve('')}`;
const template_literal = `something ${await Promise.resolve('')}`;
const unary = !(await Promise.resolve(true));
});

const task_fn_no_export = transform_renamed(async () => {
const assign = await Promise.resolve();
let assignment = null;
assignment = await Promise.resolve();
const array = [await Promise.resolve()];
const sum = (await Promise.resolve(1)) + 2;
const sum2 = 2 + (await Promise.resolve(1));
fns[await Promise.resolve(0)]();
const [array_destructure = await Promise.resolve()] = [undefined];
const function_call = fn(await Promise.resolve(2));
const conditional1 = (await Promise.resolve(true)) ? 1 : 2;
const conditional2 = true ? await Promise.resolve(1) : 2;
const conditional3 = false ? 1 : await Promise.resolve(2);
const member = { property: null };
member[await Promise.resolve('property')] = await Promise.resolve(null);
const logical1 = (await Promise.resolve(null)) ?? 3;
const logical2 = null ?? (await Promise.resolve(null));
const logical3 = null || (await Promise.resolve(null));
const logical4 = (await Promise.resolve(null)) || null;
const logical5 = (await Promise.resolve(null)) && null;
const logical6 = true && (await Promise.resolve(null));
const object_expression1 = { awaited: await Promise.resolve(2) };
const object_expression2 = { awaited: { nested: await Promise.resolve(2) } };
const object_expression3 = { awaited: { nested: [await Promise.resolve(2)] } };
const tagged_template = str`something ${await Promise.resolve('')}`;
const template_literal = `something ${await Promise.resolve('')}`;
const unary = !(await Promise.resolve(true));
});

async function with_no_transform() {
const assign = await Promise.resolve();
let assignment = null;
assignment = await Promise.resolve();
const array = [await Promise.resolve()];
const sum = (await Promise.resolve(1)) + 2;
const sum2 = 2 + (await Promise.resolve(1));
fns[await Promise.resolve(0)]();
const [array_destructure = await Promise.resolve()] = [undefined];
const function_call = fn(await Promise.resolve(2));
const conditional1 = (await Promise.resolve(true)) ? 1 : 2;
const conditional2 = true ? await Promise.resolve(1) : 2;
const conditional3 = false ? 1 : await Promise.resolve(2);
const member = { property: null };
member[await Promise.resolve('property')] = await Promise.resolve(null);
const logical1 = (await Promise.resolve(null)) ?? 3;
const logical2 = null ?? (await Promise.resolve(null));
const logical3 = null || (await Promise.resolve(null));
const logical4 = (await Promise.resolve(null)) || null;
const logical5 = (await Promise.resolve(null)) && null;
const logical6 = true && (await Promise.resolve(null));
const object_expression1 = { awaited: await Promise.resolve(2) };
const object_expression2 = { awaited: { nested: await Promise.resolve(2) } };
const object_expression3 = { awaited: { nested: [await Promise.resolve(2)] } };
const tagged_template = str`something ${await Promise.resolve('')}`;
const template_literal = `something ${await Promise.resolve('')}`;
const unary = !(await Promise.resolve(true));
}

const different_transform = transform(async () => {
const assign = await Promise.resolve();
let assignment = null;
assignment = await Promise.resolve();
const array = [await Promise.resolve()];
const sum = (await Promise.resolve(1)) + 2;
const sum2 = 2 + (await Promise.resolve(1));
fns[await Promise.resolve(0)]();
const [array_destructure = await Promise.resolve()] = [undefined];
const function_call = fn(await Promise.resolve(2));
const conditional1 = (await Promise.resolve(true)) ? 1 : 2;
const conditional2 = true ? await Promise.resolve(1) : 2;
const conditional3 = false ? 1 : await Promise.resolve(2);
const member = { property: null };
member[await Promise.resolve('property')] = await Promise.resolve(null);
const logical1 = (await Promise.resolve(null)) ?? 3;
const logical2 = null ?? (await Promise.resolve(null));
const logical3 = null || (await Promise.resolve(null));
const logical4 = (await Promise.resolve(null)) || null;
const logical5 = (await Promise.resolve(null)) && null;
const logical6 = true && (await Promise.resolve(null));
const object_expression1 = { awaited: await Promise.resolve(2) };
const object_expression2 = { awaited: { nested: await Promise.resolve(2) } };
const object_expression3 = { awaited: { nested: [await Promise.resolve(2)] } };
const tagged_template = str`something ${await Promise.resolve('')}`;
const template_literal = `something ${await Promise.resolve('')}`;
const unary = !(await Promise.resolve(true));
});
Loading

0 comments on commit 81c1bba

Please sign in to comment.