Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Jan 25, 2024
1 parent ba24e79 commit 3614725
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
16 changes: 8 additions & 8 deletions new-packages/ts-project/__tests__/transitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5062,7 +5062,7 @@ test('should extract a sibling transition as not internal by default (direct str
`,
});

const project = await createTestProject(tmpPath, { version: 'v4' });
const project = await createTestProject(tmpPath, { xstateVersion: '4' });
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -5178,7 +5178,7 @@ test('should extract a descendant transition as internal by default (direct stri
`,
});

const project = await createTestProject(tmpPath, { version: 'v4' });
const project = await createTestProject(tmpPath, { xstateVersion: '4' });
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -5292,7 +5292,7 @@ test('should extract a sibling transition as internal by default (direct object
`,
});

const project = await createTestProject(tmpPath, { version: 'v4' });
const project = await createTestProject(tmpPath, { xstateVersion: '4' });
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -5410,7 +5410,7 @@ test('should extract a descendant transition as internal by default (direct obje
`,
});

const project = await createTestProject(tmpPath, { version: 'v4' });
const project = await createTestProject(tmpPath, { xstateVersion: '4' });
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -5524,7 +5524,7 @@ test('should extract an explicit internal transition as internal (v4)', async ()
`,
});

const project = await createTestProject(tmpPath, { version: 'v4' });
const project = await createTestProject(tmpPath, { xstateVersion: '4' });
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -5638,7 +5638,7 @@ test('should extract an explicit non-internal transition as not internal (v4)',
`,
});

const project = await createTestProject(tmpPath, { version: 'v4' });
const project = await createTestProject(tmpPath, { xstateVersion: '4' });
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -5767,7 +5767,7 @@ test('should extract transition with multiple targets as internal when any of it
`,
});

const project = await createTestProject(tmpPath, { version: 'v4' });
const project = await createTestProject(tmpPath, { xstateVersion: '4' });
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -5963,7 +5963,7 @@ test('should extract transition with multiple targets as not internal when none
`,
});

const project = await createTestProject(tmpPath, { version: 'v4' });
const project = await createTestProject(tmpPath, { xstateVersion: '4' });
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
Expand Down
4 changes: 2 additions & 2 deletions new-packages/ts-project/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ export async function createTestProject(
cwd: string,
{
ts = typescript,
version,
xstateVersion: version,
...options
}: Partial<TypeScriptTestProgramOptions & TSProjectOptions> = {},
) {
const program = await createTestProgram(cwd, {
ts: typescript,
...options,
});
return createProject(ts, program, { version });
return createProject(ts, program, { xstateVersion: version });
}

function replaceUniqueIdsRecursively(
Expand Down
6 changes: 3 additions & 3 deletions new-packages/ts-project/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ function extractMachineConfig(
}

export interface TSProjectOptions {
version?: XStateVersion | undefined;
xstateVersion?: XStateVersion | undefined;
}

export function createProject(
ts: typeof import('typescript'),
tsProgram: Program,
{ version = 'v5' }: TSProjectOptions = {},
{ xstateVersion = '5' }: TSProjectOptions = {},
) {
return {
extractMachines(fileName: string) {
Expand All @@ -149,7 +149,7 @@ export function createProject(
return findCreateMachineCalls(ts, sourceFile).map((call) => {
const ctx: ExtractionContext = {
sourceFile,
version,
xstateVersion,
errors: [],
digraph: {
nodes: {},
Expand Down
33 changes: 21 additions & 12 deletions new-packages/ts-project/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,14 @@ function registerActionBlocks(
}
}

function getDefaultInternalValue(
/**
* returns the auto-assigned internal value when no explicit configuration for this property is found
*/
function getImpliedInternalValue(
ctx: ExtractionContext,
targets: string[] | undefined,
) {
if (ctx.version !== 'v4') {
if (ctx.xstateVersion !== '4') {
return true;
}
return targets ? targets.some((t) => t.startsWith('.')) : true;
Expand Down Expand Up @@ -262,7 +265,7 @@ function extractEdgeGroup(
createEdge({
sourceId,
eventTypeData,
internal: getDefaultInternalValue(ctx, [element.text]),
internal: getImpliedInternalValue(ctx, [element.text]),
}),
[element.text],
];
Expand Down Expand Up @@ -375,12 +378,15 @@ function extractEdgeGroup(
return;
}

const value = getJsonValue(ctx, ts, prop.initializer);

if (typeof value === 'boolean') {
edge.data.internal = value;
if (prop.initializer.kind === ts.SyntaxKind.TrueKeyword) {
edge.data.internal = true;
return;
}
if (prop.initializer.kind === ts.SyntaxKind.FalseKeyword) {
edge.data.internal = false;
return;
}

return;
}
case 'reenter':
Expand All @@ -391,12 +397,15 @@ function extractEdgeGroup(
}
seenInternalProp = true;

const value = getJsonValue(ctx, ts, prop.initializer);

if (typeof value === 'boolean') {
edge.data.internal = !value;
if (prop.initializer.kind === ts.SyntaxKind.TrueKeyword) {
edge.data.internal = false;
return;
}
if (prop.initializer.kind === ts.SyntaxKind.FalseKeyword) {
edge.data.internal = true;
return;
}

return;
case 'description': {
edge.data.description = ts.isStringLiteralLike(prop.initializer)
Expand All @@ -412,7 +421,7 @@ function extractEdgeGroup(
});

if (!seenInternalProp) {
edge.data.internal = getDefaultInternalValue(ctx, targets);
edge.data.internal = getImpliedInternalValue(ctx, targets);
}

return [edge, targets];
Expand Down
5 changes: 3 additions & 2 deletions new-packages/ts-project/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { SourceFile } from 'typescript';

// it's acting as a threshold - atm there is no need to know the exact version
export type XStateVersion = 'v4' | 'v5';
// strings are used to allow for future minor versions
export type XStateVersion = '4' | '5';

export interface TreeNode {
uniqueId: string;
Expand All @@ -11,7 +12,7 @@ export interface TreeNode {

export interface ExtractionContext {
sourceFile: SourceFile;
version: XStateVersion;
xstateVersion: XStateVersion;
errors: ExtractionError[];
digraph: Pick<
ExtractorDigraphDef,
Expand Down

0 comments on commit 3614725

Please sign in to comment.