From 9bc39160c817dad9c40925d7f5fdb6ddbb3fb0b8 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 8 Jan 2023 08:27:48 -0500 Subject: [PATCH 1/6] Fix multiple layout issue --- apps/extension/client/testFixture/machine.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apps/extension/client/testFixture/machine.ts b/apps/extension/client/testFixture/machine.ts index d0f82126..959c5d8e 100644 --- a/apps/extension/client/testFixture/machine.ts +++ b/apps/extension/client/testFixture/machine.ts @@ -8,13 +8,12 @@ createMachine( states: { idle: { on: { - NEXT: { - target: 'active', - cond: 'canGoNext', - }, + NEXT: 'active', }, }, + active: {}, + 'new state 1': {}, }, }, { From d41a449d8129ba9f8f33d16ae6d8c8c50f3f54ed Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 8 Jan 2023 08:28:08 -0500 Subject: [PATCH 2/6] Fix multiple layout issue --- package.json | 3 ++- .../machine-extractor/src/MachineExtractResult.ts | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 9266a5cb..50b6dbfe 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "deps:build": "preconstruct build", "vscode:dev": "yarn turbo run vscode:build:dev --no-cache && cp apps/extension/server/dist/index.js apps/extension/client/dist/server.js", "vscode:prod": "yarn turbo run vscode:build:prod --no-cache && cp apps/extension/server/dist/index.js apps/extension/client/dist/server.js", - "test": "jest" + "test": "jest", + "build:vsix": "cd apps/extension/client && npx vsce package --yarn" }, "preconstruct": { "packages": [ diff --git a/packages/machine-extractor/src/MachineExtractResult.ts b/packages/machine-extractor/src/MachineExtractResult.ts index d8c7d2d3..a17a49b8 100644 --- a/packages/machine-extractor/src/MachineExtractResult.ts +++ b/packages/machine-extractor/src/MachineExtractResult.ts @@ -4,12 +4,12 @@ import * as recast from 'recast'; import { Action, Condition, MachineOptions } from 'xstate'; import { choose } from 'xstate/lib/actions'; import { DeclarationType } from '.'; +import { RecordOfArrays } from './RecordOfArrays'; import { ActionNode, ParsedChooseCondition } from './actions'; import { getMachineNodesFromFile } from './getMachineNodesFromFile'; import { TMachineCallExpression } from './machineCallExpression'; -import { RecordOfArrays } from './RecordOfArrays'; import { StateNodeReturn } from './stateNode'; -import { toMachineConfig, ToMachineConfigOptions } from './toMachineConfig'; +import { ToMachineConfigOptions, toMachineConfig } from './toMachineConfig'; import { TransitionConfigNode } from './transitions'; import { Comment } from './types'; @@ -338,10 +338,10 @@ export class MachineExtractResult { return false; } - return ( - comment.loc!.end.line === - this.machineCallResult.callee.loc!.start.line - 1 - ); + const proximity = + this.machineCallResult.callee.loc!.start.line - comment.loc!.end.line; + + return proximity < 5; }); if (!layoutComment) return undefined; From c7cccf7bb0c1bdf570d1e9f2a2223b075020f340 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 8 Jan 2023 08:29:52 -0500 Subject: [PATCH 3/6] Oops --- apps/extension/client/testFixture/machine.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/extension/client/testFixture/machine.ts b/apps/extension/client/testFixture/machine.ts index 959c5d8e..7248f199 100644 --- a/apps/extension/client/testFixture/machine.ts +++ b/apps/extension/client/testFixture/machine.ts @@ -1,6 +1,5 @@ import { createMachine } from 'xstate'; -/** @xstate-layout N4IgpgJg5mDOIC5gF8A0IB2B7CdGgAoBbAQwGMALASwzAEoA6KiAGzHxAActYqAXKlgwcAHogAsAJnQBPRAA4ArA3EBOdYtUBmAAyr5qxfPHI0IYuWq06HbrwFDRiAIzzZL+Qx3efv3wHZTUyA */ createMachine( { id: '(machine)', @@ -8,12 +7,13 @@ createMachine( states: { idle: { on: { - NEXT: 'active', + NEXT: { + target: 'active', + cond: 'canGoNext', + }, }, }, - active: {}, - 'new state 1': {}, }, }, { From 49daa5c000529adb60eecf3d101ba6b86bc67819 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 8 Jan 2023 08:46:53 -0500 Subject: [PATCH 4/6] Add tests --- .../src/MachineExtractResult.ts | 2 +- .../src/__tests__/layout-comment.test.ts | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/machine-extractor/src/MachineExtractResult.ts b/packages/machine-extractor/src/MachineExtractResult.ts index a17a49b8..55c385ca 100644 --- a/packages/machine-extractor/src/MachineExtractResult.ts +++ b/packages/machine-extractor/src/MachineExtractResult.ts @@ -341,7 +341,7 @@ export class MachineExtractResult { const proximity = this.machineCallResult.callee.loc!.start.line - comment.loc!.end.line; - return proximity < 5; + return Math.abs(proximity) <= 2; }); if (!layoutComment) return undefined; diff --git a/packages/machine-extractor/src/__tests__/layout-comment.test.ts b/packages/machine-extractor/src/__tests__/layout-comment.test.ts index cf51d897..2f77b87a 100644 --- a/packages/machine-extractor/src/__tests__/layout-comment.test.ts +++ b/packages/machine-extractor/src/__tests__/layout-comment.test.ts @@ -11,4 +11,43 @@ describe('Layout comments', () => { `layout-string`, ); }); + + it('Should parse layout comments that appear after the variable declaration', () => { + const result = extractMachinesFromFile(` + const machine = + /** @xstate-layout layout-string */ + createMachine({}); + `); + + expect(result!.machines[0]!.getLayoutComment()?.value).toEqual( + `layout-string`, + ); + }); + + it('Should parse layout comments that are near variable declaration', () => { + const result = extractMachinesFromFile(` + const machine = + /** @xstate-layout layout-string */ + + createMachine({}); + `); + + expect(result!.machines[0]!.getLayoutComment()?.value).toEqual( + `layout-string`, + ); + }); + + it('Should not parse layout comments that are far from machine', () => { + const result = extractMachinesFromFile(` + /** @xstate-layout layout-string */ + + + + + + const machine = createMachine({}); + `); + + expect(result!.machines[0]!.getLayoutComment()?.value).toBeUndefined(); + }); }); From d572bc5702f226558f7e69d7f0dad23536be2a70 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 8 Jan 2023 08:48:56 -0500 Subject: [PATCH 5/6] Add back layout string --- apps/extension/client/testFixture/machine.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/extension/client/testFixture/machine.ts b/apps/extension/client/testFixture/machine.ts index 7248f199..d0f82126 100644 --- a/apps/extension/client/testFixture/machine.ts +++ b/apps/extension/client/testFixture/machine.ts @@ -1,5 +1,6 @@ import { createMachine } from 'xstate'; +/** @xstate-layout N4IgpgJg5mDOIC5gF8A0IB2B7CdGgAoBbAQwGMALASwzAEoA6KiAGzHxAActYqAXKlgwcAHogAsAJnQBPRAA4ArA3EBOdYtUBmAAyr5qxfPHI0IYuWq06HbrwFDRiAIzzZL+Qx3efv3wHZTUyA */ createMachine( { id: '(machine)', From 052e14d4e6844718e75dd0886d96d674ea589ed6 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Sun, 8 Jan 2023 08:49:21 -0500 Subject: [PATCH 6/6] Remove script --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 50b6dbfe..9266a5cb 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,7 @@ "deps:build": "preconstruct build", "vscode:dev": "yarn turbo run vscode:build:dev --no-cache && cp apps/extension/server/dist/index.js apps/extension/client/dist/server.js", "vscode:prod": "yarn turbo run vscode:build:prod --no-cache && cp apps/extension/server/dist/index.js apps/extension/client/dist/server.js", - "test": "jest", - "build:vsix": "cd apps/extension/client && npx vsce package --yarn" + "test": "jest" }, "preconstruct": { "packages": [