Skip to content

Commit 02a4dd4

Browse files
authored
Add compile time check for workspace actions (#2685)
* Move workspace location check to action level This allows for type-checking to guard against unintended behaviour. * Improve type safety Prevents specifying invalid workspace locations. * Add comments Also fixed formatiing. * Fix type errors * Restore runtime check For defensive programming. * Fix incorrect typing * Alias type for workspace locations with tools
1 parent e1a3267 commit 02a4dd4

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

src/commons/sagas/WorkspaceSaga.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,9 @@ export function* evalCode(
13021302
// the total number of steps and the breakpoints are updated in the Environment Visualiser slider.
13031303
if (context.executionMethod === 'ec-evaluator' && needUpdateEnv) {
13041304
yield put(actions.updateEnvStepsTotal(context.runtime.envStepsTotal, workspaceLocation));
1305-
yield put(actions.toggleUpdateEnv(false, workspaceLocation));
1305+
// `needUpdateEnv` implies `correctWorkspace`, which satisfies the type constraint.
1306+
// But TS can't infer that yet, so we need a typecast here.
1307+
yield put(actions.toggleUpdateEnv(false, workspaceLocation as any));
13061308
yield put(actions.updateBreakpointSteps(context.runtime.breakpointSteps, workspaceLocation));
13071309
}
13081310
// Stop the home icon from flashing for an error if it is doing so since the evaluation is successful

src/commons/workspace/WorkspaceActions.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import {
6868
UPDATE_SUBMISSIONS_TABLE_FILTERS,
6969
UPDATE_WORKSPACE,
7070
WorkspaceLocation,
71+
WorkspaceLocationsWithTools,
7172
WorkspaceState
7273
} from './WorkspaceTypes';
7374

@@ -385,20 +386,24 @@ export const notifyProgramEvaluated = (
385386
workspaceLocation
386387
});
387388

388-
export const toggleUsingSubst = (usingSubst: boolean, workspaceLocation: WorkspaceLocation) =>
389-
action(TOGGLE_USING_SUBST, { usingSubst, workspaceLocation });
389+
export const toggleUsingSubst = (
390+
usingSubst: boolean,
391+
workspaceLocation: WorkspaceLocationsWithTools
392+
) => action(TOGGLE_USING_SUBST, { usingSubst, workspaceLocation });
390393

391394
export const addHtmlConsoleError = (
392395
errorMsg: string,
393396
workspaceLocation: WorkspaceLocation,
394397
storyEnv?: string
395398
) => action(ADD_HTML_CONSOLE_ERROR, { errorMsg, workspaceLocation, storyEnv });
396399

397-
export const toggleUsingEnv = (usingEnv: boolean, workspaceLocation: WorkspaceLocation) =>
400+
export const toggleUsingEnv = (usingEnv: boolean, workspaceLocation: WorkspaceLocationsWithTools) =>
398401
action(TOGGLE_USING_ENV, { usingEnv, workspaceLocation });
399402

400-
export const toggleUpdateEnv = (updateEnv: boolean, workspaceLocation: WorkspaceLocation) =>
401-
action(TOGGLE_UPDATE_ENV, { updateEnv, workspaceLocation });
403+
export const toggleUpdateEnv = (
404+
updateEnv: boolean,
405+
workspaceLocation: WorkspaceLocationsWithTools
406+
) => action(TOGGLE_UPDATE_ENV, { updateEnv, workspaceLocation });
402407

403408
export const updateEnvSteps = (steps: number, workspaceLocation: WorkspaceLocation) =>
404409
action(UPDATE_ENVSTEPS, { steps, workspaceLocation });

src/commons/workspace/WorkspaceReducer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ export const WorkspaceReducer: Reducer<WorkspaceManagerState> = (
571571
isEditorAutorun: !state[workspaceLocation].isEditorAutorun
572572
}
573573
};
574-
case TOGGLE_USING_SUBST:
574+
case TOGGLE_USING_SUBST: {
575+
const { workspaceLocation } = action.payload;
575576
if (workspaceLocation === 'playground' || workspaceLocation === 'sicp') {
576577
return {
577578
...state,
@@ -583,7 +584,9 @@ export const WorkspaceReducer: Reducer<WorkspaceManagerState> = (
583584
} else {
584585
return state;
585586
}
586-
case TOGGLE_USING_ENV:
587+
}
588+
case TOGGLE_USING_ENV: {
589+
const { workspaceLocation } = action.payload;
587590
if (workspaceLocation === 'playground' || workspaceLocation === 'sicp') {
588591
return {
589592
...state,
@@ -595,7 +598,9 @@ export const WorkspaceReducer: Reducer<WorkspaceManagerState> = (
595598
} else {
596599
return state;
597600
}
598-
case TOGGLE_UPDATE_ENV:
601+
}
602+
case TOGGLE_UPDATE_ENV: {
603+
const { workspaceLocation } = action.payload;
599604
if (workspaceLocation === 'playground' || workspaceLocation === 'sicp') {
600605
return {
601606
...state,
@@ -607,6 +612,7 @@ export const WorkspaceReducer: Reducer<WorkspaceManagerState> = (
607612
} else {
608613
return state;
609614
}
615+
}
610616
case UPDATE_SUBMISSIONS_TABLE_FILTERS:
611617
return {
612618
...state,

src/commons/workspace/WorkspaceTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const UPDATE_BREAKPOINTSTEPS = 'UPDATE_BREAKPOINTSTEPS';
6363
export const CHANGE_SUBLANGUAGE = 'CHANGE_SUBLANGUAGE';
6464

6565
export type WorkspaceLocation = keyof WorkspaceManagerState;
66+
export type WorkspaceLocationsWithTools = Extract<WorkspaceLocation, 'playground' | 'sicp'>;
6667

6768
type AssessmentWorkspaceAttr = {
6869
readonly currentAssessment?: number;

0 commit comments

Comments
 (0)