Skip to content

Commit 033f8ec

Browse files
authored
Merge pull request #41 from chibicode/remove-immer
Remove immer
2 parents 4f4fa57 + 50c54b1 commit 033f8ec

File tree

5 files changed

+685
-605
lines changed

5 files changed

+685
-605
lines changed

package.json

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
"dependencies": {
44
"@emotion/core": "^10.0.10",
55
"@emotion/styled": "^10.0.10",
6-
"@material-ui/core": "^3.9.2",
6+
"@material-ui/core": "^3.9.3",
77
"@zeit/next-css": "^1.0.1",
88
"@zeit/next-typescript": "^1.1.1",
99
"color": "^3.1.0",
10-
"immer": "^2.1.4",
1110
"lodash": "^4.17.11",
12-
"luxon": "^1.12.0",
11+
"luxon": "^1.12.1",
1312
"modern-normalize": "^0.5.0",
14-
"next": "8.0.3",
13+
"next": "8.0.4",
1514
"nprogress": "^0.2.0",
16-
"react": "^16.8.5",
17-
"react-dom": "^16.8.5",
15+
"react": "^16.8.6",
16+
"react-dom": "^16.8.6",
1817
"smoothscroll-polyfill": "^0.4.4",
19-
"twemoji": "^11.3.0"
18+
"twemoji": "^12.0.0"
2019
},
2120
"scripts": {
2221
"jp": "SHOW_HIDDEN_CARDS_ON_QUIZ=true DEV_LOCALE=jp next -p 3001",
@@ -43,32 +42,32 @@
4342
"@types/color": "^3.0.0",
4443
"@types/jest": "^24.0.11",
4544
"@types/lodash": "^4.14.123",
46-
"@types/luxon": "^1.11.1",
45+
"@types/luxon": "^1.12.0",
4746
"@types/material-ui": "^0.21.6",
4847
"@types/next": "^8.0.3",
4948
"@types/nprogress": "^0.0.29",
50-
"@types/react": "^16.8.10",
49+
"@types/react": "^16.8.12",
5150
"@types/react-dom": "^16.8.3",
5251
"@types/smoothscroll-polyfill": "^0.3.1",
53-
"@typescript-eslint/eslint-plugin": "^1.5.0",
54-
"@typescript-eslint/parser": "^1.5.0",
52+
"@typescript-eslint/eslint-plugin": "^1.6.0",
53+
"@typescript-eslint/parser": "^1.6.0",
5554
"babel-eslint": "^10.0.1",
5655
"babel-plugin-emotion": "^10.0.9",
5756
"chokidar": "^2.1.5",
5857
"concurrently": "^4.1.0",
59-
"eslint": "^5.15.3",
58+
"eslint": "^5.16.0",
6059
"eslint-config-prettier": "^4.1.0",
6160
"eslint-config-react-app": "^3.0.8",
62-
"eslint-plugin-flowtype": "^3.4.2",
61+
"eslint-plugin-flowtype": "^3.5.1",
6362
"eslint-plugin-import": "^2.16.0",
6463
"eslint-plugin-jsx-a11y": "^6.2.1",
6564
"eslint-plugin-prettier": "^3.0.1",
6665
"eslint-plugin-react": "^7.12.4",
6766
"eslint-plugin-react-hooks": "^1.6.0",
6867
"glob": "^7.1.3",
69-
"jest": "^24.5.0",
68+
"jest": "^24.7.1",
7069
"prettier": "^1.16.4",
71-
"ts-jest": "^24.0.0",
72-
"typescript": "^3.3.4000"
70+
"ts-jest": "^24.0.2",
71+
"typescript": "^3.4.2"
7372
}
7473
}

src/lib/yc/replaceCallParentKey.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { isCall, isVariable } from 'src/lib/yc/expressionTypeGuards'
2+
import {
3+
CallExpression,
4+
Expression,
5+
FunctionExpression,
6+
VariableExpression,
7+
StepChild,
8+
ExecutableCall
9+
} from 'src/types/yc/ExpressionTypes'
10+
11+
export default function replaceCallParentKey(
12+
expression: VariableExpression,
13+
target: CallExpression,
14+
key: 'func' | 'arg',
15+
replaceWith: ExecutableCall | StepChild<'default'>
16+
): VariableExpression
17+
export default function replaceCallParentKey(
18+
expression: FunctionExpression,
19+
target: CallExpression,
20+
key: 'func' | 'arg',
21+
replaceWith: ExecutableCall | StepChild<'default'>
22+
): FunctionExpression
23+
export default function replaceCallParentKey(
24+
expression: CallExpression,
25+
target: CallExpression,
26+
key: 'func' | 'arg',
27+
replaceWith: ExecutableCall | StepChild<'default'>
28+
): CallExpression
29+
export default function replaceCallParentKey(
30+
expression: VariableExpression | FunctionExpression,
31+
target: CallExpression,
32+
key: 'func' | 'arg',
33+
replaceWith: ExecutableCall | StepChild<'default'>
34+
): VariableExpression | FunctionExpression
35+
export default function replaceCallParentKey(
36+
expression: Expression,
37+
target: CallExpression,
38+
key: 'func' | 'arg',
39+
replaceWith: ExecutableCall | StepChild<'default'>
40+
): Expression
41+
export default function replaceCallParentKey(
42+
expression: Expression,
43+
target: CallExpression,
44+
key: 'func' | 'arg',
45+
replaceWith: ExecutableCall | StepChild<'default'>
46+
): Expression {
47+
if (isVariable(expression)) {
48+
return expression
49+
} else if (isCall(expression)) {
50+
if (expression === target) {
51+
return {
52+
...expression,
53+
[key]: replaceWith
54+
}
55+
} else {
56+
return {
57+
...expression,
58+
arg: replaceCallParentKey(expression.arg, target, key, replaceWith),
59+
func: replaceCallParentKey(expression.func, target, key, replaceWith)
60+
}
61+
}
62+
} else {
63+
return {
64+
...expression,
65+
body: replaceCallParentKey(expression.body, target, key, replaceWith)
66+
}
67+
}
68+
}

src/lib/yc/replaceFuncParentKey.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { isCall, isVariable } from 'src/lib/yc/expressionTypeGuards'
2+
import {
3+
CallExpression,
4+
Expression,
5+
FunctionExpression,
6+
VariableExpression,
7+
StepChild,
8+
ExecutableCall
9+
} from 'src/types/yc/ExpressionTypes'
10+
11+
export default function replaceFuncParentKey(
12+
expression: VariableExpression,
13+
target: FunctionExpression,
14+
replaceWith: ExecutableCall | StepChild<'default'>
15+
): VariableExpression
16+
export default function replaceFuncParentKey(
17+
expression: FunctionExpression,
18+
target: FunctionExpression,
19+
replaceWith: ExecutableCall | StepChild<'default'>
20+
): FunctionExpression
21+
export default function replaceFuncParentKey(
22+
expression: CallExpression,
23+
target: FunctionExpression,
24+
replaceWith: ExecutableCall | StepChild<'default'>
25+
): CallExpression
26+
export default function replaceFuncParentKey(
27+
expression: VariableExpression | FunctionExpression,
28+
target: FunctionExpression,
29+
replaceWith: ExecutableCall | StepChild<'default'>
30+
): VariableExpression | FunctionExpression
31+
export default function replaceFuncParentKey(
32+
expression: Expression,
33+
target: FunctionExpression,
34+
replaceWith: ExecutableCall | StepChild<'default'>
35+
): Expression
36+
export default function replaceFuncParentKey(
37+
expression: Expression,
38+
target: FunctionExpression,
39+
replaceWith: ExecutableCall | StepChild<'default'>
40+
): Expression {
41+
if (isVariable(expression)) {
42+
return expression
43+
} else if (isCall(expression)) {
44+
return {
45+
...expression,
46+
arg: replaceFuncParentKey(expression.arg, target, replaceWith),
47+
func: replaceFuncParentKey(expression.func, target, replaceWith)
48+
}
49+
} else {
50+
if (expression === target) {
51+
return {
52+
...expression,
53+
body: replaceWith
54+
}
55+
} else {
56+
return {
57+
...expression,
58+
body: replaceFuncParentKey(expression.body, target, replaceWith)
59+
}
60+
}
61+
}
62+
}

src/lib/yc/stepExpressionContainer.ts

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import produce, { Draft } from 'immer'
21
import conflictingVariableNames from 'src/lib/yc/conflictingVariableNames'
32
import { isContainerWithState } from 'src/lib/yc/expressionContainerGuards'
43
import { StepOptions } from 'src/lib/yc/ExpressionContainerManager'
54
import findNextCallExpressionAndParent from 'src/lib/yc/findNextCallExpressionAndParent'
65
import hasUnboundVariables from 'src/lib/yc/hasUnboundVariables'
76
import prioritizeExpressionContainer from 'src/lib/yc/prioritizeExpressionContainer'
87
import resetExpressionContainer from 'src/lib/yc/resetExpressionContainer'
8+
import replaceCallParentKey from 'src/lib/yc/replaceCallParentKey'
9+
import replaceFuncParentKey from 'src/lib/yc/replaceFuncParentKey'
910
import {
1011
removeCrossed,
1112
stepToActive,
@@ -48,7 +49,7 @@ const stepExpressionContainerReset = (
4849
}
4950

5051
const step = (
51-
e: Draft<ExecutableCall>,
52+
e: ExecutableCall,
5253
{ showAllShowSteps }: StepOptions,
5354
matchExists?: boolean
5455
): {
@@ -170,33 +171,36 @@ const step = (
170171
}
171172
}
172173

173-
const recipe = (stepOptions: StepOptions) => (
174-
draftContainer: Draft<
175-
| ContainerWithState<'ready'>
176-
| ContainerWithState<'stepped'>
177-
| ContainerWithState<'needsReset'>
178-
>
179-
): ContainerWithState<'needsReset'> | ContainerWithState<'stepped'> | void => {
174+
const runStep = (
175+
e: ContainerWithState<'ready'> | ContainerWithState<'stepped'>,
176+
stepOptions: StepOptions
177+
):
178+
| ContainerWithState<'needsReset'>
179+
| ContainerWithState<'stepped'>
180+
| ContainerWithState<'ready'> => {
180181
const {
181182
expression,
182183
callParent,
183184
funcParent,
184185
callParentKey
185186
} = findNextCallExpressionAndParent<
186-
Draft<ExecutableCall>,
187-
Draft<CallExpression>,
188-
Draft<FunctionExpression>
189-
>(draftContainer.expression)
187+
ExecutableCall,
188+
CallExpression,
189+
FunctionExpression
190+
>(e.expression)
190191
if (!expression) {
191192
// Special case - already done to begin with
192-
draftContainer.containerState = 'needsReset'
193-
return
193+
return {
194+
...e,
195+
containerState: 'needsReset'
196+
}
194197
}
198+
195199
const {
196200
nextExpression,
197201
matchExists,
198202
previouslyChangedExpressionState
199-
} = step(expression, stepOptions, draftContainer.matchExists)
203+
} = step(expression, stepOptions, e.matchExists)
200204

201205
if (!callParent && !callParentKey && !funcParent) {
202206
const newContainer = {
@@ -209,22 +213,42 @@ const recipe = (stepOptions: StepOptions) => (
209213
: { ...newContainer, containerState: 'stepped' }
210214
}
211215

216+
let newExpression: typeof e.expression
217+
212218
if (callParent && callParentKey) {
213-
callParent[callParentKey] = nextExpression
219+
newExpression = replaceCallParentKey(
220+
e.expression,
221+
callParent,
222+
callParentKey,
223+
nextExpression
224+
)
214225
} else if (funcParent) {
215-
funcParent.body = nextExpression
226+
newExpression = replaceFuncParentKey(
227+
e.expression,
228+
funcParent,
229+
nextExpression
230+
)
216231
} else {
217232
throw new Error()
218233
}
219234

220235
if (previouslyChangedExpressionState === 'default') {
221-
draftContainer.containerState = 'needsReset'
236+
return {
237+
...e,
238+
expression: newExpression,
239+
containerState: 'needsReset',
240+
matchExists,
241+
previouslyChangedExpressionState
242+
}
222243
} else {
223-
draftContainer.containerState = 'stepped'
244+
return {
245+
...e,
246+
expression: newExpression,
247+
containerState: 'stepped',
248+
matchExists,
249+
previouslyChangedExpressionState
250+
}
224251
}
225-
226-
draftContainer.matchExists = matchExists
227-
draftContainer.previouslyChangedExpressionState = previouslyChangedExpressionState
228252
}
229253

230254
export default function stepExpressionContainer(
@@ -234,11 +258,7 @@ export default function stepExpressionContainer(
234258
| ContainerWithState<'done'>
235259
| ContainerWithState<'stepped'>
236260
| ContainerWithState<'ready'> {
237-
const result = produce<
238-
| ContainerWithState<'needsReset'>
239-
| ContainerWithState<'stepped'>
240-
| ContainerWithState<'ready'>
241-
>(e, recipe(stepOptions))
261+
const result = runStep(e, stepOptions)
242262

243263
if (isContainerWithState(result, 'needsReset')) {
244264
return stepExpressionContainerReset(result)

0 commit comments

Comments
 (0)