add inline spread for record updates

This commit is contained in:
mdgriffith 2020-08-07 08:38:42 -04:00
parent 3802dca61f
commit e6a34feffd
2 changed files with 22 additions and 3 deletions

View File

@ -22,12 +22,29 @@ const _Utils_update = (oldRecord, updatedFields) => (Object.assign({}, oldRecord
}
`;
export const objectUpdate = (
kind: ObjectUpdate
): ts.TransformerFactory<ts.SourceFile> => {
switch (kind) {
case ObjectUpdate.UseSpreadForUpdateAndOriginalRecord:
return createReplaceUtilsUpdateWithObjectSpread(kind);
case ObjectUpdate.UseSpreadOnlyToMakeACopy:
return createReplaceUtilsUpdateWithObjectSpread(kind);
case ObjectUpdate.UseAssign:
return createReplaceUtilsUpdateWithObjectSpread(kind);
case ObjectUpdate.InlineAssign:
return inlineObjectAssign();
case ObjectUpdate.InlineSpread:
return inlineObjectSpread();
}
};
const extractBody = (sourceText: string): ts.Node => {
const source = ts.createSourceFile('bla', sourceText, ts.ScriptTarget.ES2018);
return source.statements[0];
};
export const createReplaceUtilsUpdateWithObjectSpread = (
const createReplaceUtilsUpdateWithObjectSpread = (
kind: ObjectUpdate
): ts.TransformerFactory<ts.SourceFile> => context => {
return sourceFile => {
@ -55,7 +72,7 @@ export const createReplaceUtilsUpdateWithObjectSpread = (
};
const OBJECT_UPDATE = '_Utils_update';
export const inlineObjectAssign = (): ts.TransformerFactory<ts.SourceFile> => context => {
const inlineObjectAssign = (): ts.TransformerFactory<ts.SourceFile> => context => {
return sourceFile => {
const visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
// detects function f(..){..}
@ -78,7 +95,7 @@ export const inlineObjectAssign = (): ts.TransformerFactory<ts.SourceFile> => co
};
};
export const inlineObjectSpread = (): ts.TransformerFactory<ts.SourceFile> => context => {
const inlineObjectSpread = (): ts.TransformerFactory<ts.SourceFile> => context => {
return sourceFile => {
const visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
// detects function f(..){..}

View File

@ -27,4 +27,6 @@ export enum ObjectUpdate {
UseSpreadForUpdateAndOriginalRecord = 'for_both',
UseSpreadOnlyToMakeACopy = 'for_copy',
UseAssign = 'use_assign',
InlineAssign = 'inline_assign',
InlineSpread = 'inline_spread',
}