Merge pull request #200 from stefanwille/upgrade-prettier
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Upgrade prettier and enable prettier on TypeScript code
authored by Michel Weststrate · committed by GitHub · parents 255d135, e06b330 · Browse files · 7 files·+140 added−111 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+140 added−111 removed
- tests:+88 added−85 removed
- other:+52 added−26 removed
Provenance
- Commit
1139f57- Tree
046be97- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
255d135- Parent 2
e06b330
7 files changed+140 added−111 removed
modified:__tests__/flow/ts.ts+4 added-4 removed
77 unmodified lines7879808182828384858687888990919292939495969596979877 unmodified lines : state}
produce({x: 3, z: {}}, draftState => { const a = draftState; const a = draftState
if (a) { a.x // $ExpectError a.y }})
produce([1], draftState => { const a = original(draftState); const a = original(draftState) if (a) { // $ExpectError const b: string = a[0]; const c: number = a[0]; const b: string = a[0] const c: number = a[0] }})modified:__tests__/types.ts+84 added-81 removed
11234567891011124567891011121314151617181920212223241617181920212223242526272829303132333435362728293031323334353637383940414243444546474849505152383940414243444546474849505152535455565758596061626364656667685455565758596061626364656667686970717273747576777879808182838485717273747576777879808182838485868786878990919293888990919293949596979899import produce, { produce as produce2, applyPatches, Patch} from '../src/immer';import produce, {produce as produce2, applyPatches, Patch} from "../src/immer"
interface State { readonly num: number; readonly foo?: string; bar: string; readonly baz: { readonly x: number; readonly y: number; }; readonly arr: ReadonlyArray<{ readonly value: string }>; readonly arr2: { readonly value: string }[]; readonly num: number readonly foo?: string bar: string readonly baz: { readonly x: number readonly y: number } readonly arr: ReadonlyArray<{readonly value: string}> readonly arr2: {readonly value: string}[]}
const state: State = { num: 0, bar: 'foo', baz: { x: 1, y: 2, }, arr: [{ value: 'asdf' }], arr2: [{ value: 'asdf' }],}; num: 0, bar: "foo", baz: { x: 1, y: 2 }, arr: [{value: "asdf"}], arr2: [{value: "asdf"}]}
const expectedState: State = { num: 1, foo: 'bar', bar: 'foo', baz: { x: 2, y: 3, }, arr: [{ value: 'foo' }, { value: 'asf' }], arr2: [{ value: 'foo' }, { value: 'asf' }],}; num: 1, foo: "bar", bar: "foo", baz: { x: 2, y: 3 }, arr: [{value: "foo"}, {value: "asf"}], arr2: [{value: "foo"}, {value: "asf"}]}
it('can update readonly state via standard api', () => { const newState = produce<State>(state, draft => { draft.num++; draft.foo = 'bar'; draft.bar = 'foo'; draft.baz.x++; draft.baz.y++; draft.arr[0].value = 'foo'; draft.arr.push({ value: 'asf' }); draft.arr2[0].value = 'foo'; draft.arr2.push({ value: 'asf' }); }); expect(newState).not.toBe(state); expect(newState).toEqual(expectedState);});it("can update readonly state via standard api", () => { const newState = produce<State>(state, draft => { draft.num++ draft.foo = "bar" draft.bar = "foo" draft.baz.x++ draft.baz.y++ draft.arr[0].value = "foo" draft.arr.push({value: "asf"}) draft.arr2[0].value = "foo" draft.arr2.push({value: "asf"}) }) expect(newState).not.toBe(state) expect(newState).toEqual(expectedState)})
it('can update readonly state via curried api', () => { const newState = produce<State>(draft => { draft.num++; draft.foo = 'bar'; draft.bar = 'foo'; draft.baz.x++; draft.baz.y++; draft.arr[0].value = 'foo'; draft.arr.push({ value: 'asf' }); draft.arr2[0].value = 'foo'; draft.arr2.push({ value: 'asf' }); })(state); expect(newState).not.toBe(state); expect(newState).toEqual(expectedState);});it("can update readonly state via curried api", () => { const newState = produce<State>(draft => { draft.num++ draft.foo = "bar" draft.bar = "foo" draft.baz.x++ draft.baz.y++ draft.arr[0].value = "foo" draft.arr.push({value: "asf"}) draft.arr2[0].value = "foo" draft.arr2.push({value: "asf"}) })(state) expect(newState).not.toBe(state) expect(newState).toEqual(expectedState)})
it("can update use the non-default export", () => { const newState = produce2<State>(draft => { draft.num++ draft.foo = "bar" draft.bar = "foo" draft.baz.x++ draft.baz.y++ draft.arr[0].value = "foo" draft.arr.push({value: "asf"}) draft.arr2[0].value = "foo" draft.arr2.push({value: "asf"}) })(state) expect(newState).not.toBe(state) expect(newState).toEqual(expectedState)})
it('can update use the non-default export', () => { const newState = produce2<State>(draft => { draft.num++; draft.foo = 'bar'; draft.bar = 'foo'; draft.baz.x++; draft.baz.y++; draft.arr[0].value = 'foo'; draft.arr.push({ value: 'asf' }); draft.arr2[0].value = 'foo'; draft.arr2.push({ value: 'asf' }); })(state); expect(newState).not.toBe(state); expect(newState).toEqual(expectedState);});
it('can apply patches', () => {it("can apply patches", () => { let patches: Patch[] produce({ x: 3 }, d=> { d.x++ }, (p) => { patches = p }) produce( {x: 3}, d => { d.x++ }, p => { patches = p } )
expect(applyPatches({}, patches)).toEqual({x: 4})})modified:src/immer.d.ts+36 added-18 removed
1 // Mapped type to remove readonly modifiers from state2 // Based on https://github.com/Microsoft/TypeScript/blob/d4dc67aab233f5a8834dff16531baf99b16fea78/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L120-L1293-export type DraftObject<T> = {4- -readonly [P in keyof T]: Draft<T[P]>;5-};6-export interface DraftArray<T> extends Array<Draft<T>> { }7-export type Draft<T> =8- T extends any[] ? DraftArray<T[number]> :9- T extends ReadonlyArray<any> ? DraftArray<T[number]> :10- T extends object ? DraftObject<T> :11- T;3+export type DraftObject<T> = {-readonly [P in keyof T]: Draft<T[P]>}4+export interface DraftArray<T> extends Array<Draft<T>> {}5+export type Draft<T> = T extends any[]6+ ? DraftArray<T[number]>7+ : T extends ReadonlyArray<any>8+ ? DraftArray<T[number]>9+ : T extends object ? DraftObject<T> : T10 11 export interface Patch {14- op: "replace" | "remove" | "add",15- path: (string|number)[]12+ op: "replace" | "remove" | "add"13+ path: (string | number)[]14 value?: any15 }16 17 export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void35 unmodified lines53 initialState: S54 ): (currentState: S | undefined, a: A, b: B) => S55 // 3 additional arguments of types A, B and C56 <S = any, A = any, B = any, C = any>(59- recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S,57+ recipe: (58+ this: Draft<S>,59+ draftState: Draft<S>,60+ a: A,61+ b: B,62+ c: C63+ ) => void | S,64 initialState: S65 ): (currentState: S | undefined, a: A, b: B, c: C) => S66 // any number of additional arguments, but with loss of type safety67 // this may be alleviated if "variadic kinds" makes it into Typescript:68 // https://github.com/Microsoft/TypeScript/issues/545369 <S = any>(66- recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S,70+ recipe: (71+ this: Draft<S>,72+ draftState: Draft<S>,73+ ...extraArgs: any[]74+ ) => void | S,75 initialState: S76 ): (currentState: S | undefined, ...extraArgs: any[]) => S77 78 // curried invocations without default initial state79 // 0 additional arguments72- <S = any>(73- recipe: (this: Draft<S>, draftState: Draft<S>) => void | S74- ): (currentState: S) => S80+ <S = any>(recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): (81+ currentState: S82+ ) => S83 // 1 additional argument of type A84 <S = any, A = any>(85 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S86 ): (currentState: S, a: A) => S2 unmodified lines89 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S90 ): (currentState: S, a: A, b: B) => S91 // 3 additional arguments of types A, B and C92 <S = any, A = any, B = any, C = any>(85- recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S93+ recipe: (94+ this: Draft<S>,95+ draftState: Draft<S>,96+ a: A,97+ b: B,98+ c: C99+ ) => void | S100 ): (currentState: S, a: A, b: B, c: C) => S101 // any number of additional arguments, but with loss of type safety102 // this may be alleviated if "variadic kinds" makes it into Typescript:103 // https://github.com/Microsoft/TypeScript/issues/5453104 <S = any>(91- recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S105+ recipe: (106+ this: Draft<S>,107+ draftState: Draft<S>,108+ ...extraArgs: any[]109+ ) => void | S110 ): (currentState: S, ...extraArgs: any[]) => S111 }112 113 export const produce: IProduce21 unmodified linesmodified:src/patches.js+5 added-3 removed
81 unmodified lines82 const origValue = baseValue[key]83 const value = resultValue[key]84 const op = !assignedValue85 ? "remove"86- : key in baseValue ? "replace" : "add"86+ : key in baseValue87+ ? "replace"88+ : "add"89 if (origValue === baseValue && op === "replace") return90 const path = basepath.concat(key)91 patches.push(op === "remove" ? {op, path} : {op, path, value})92 inversePatches.push(93 op === "add"94 ? {op: "remove", path}95 : op === "remove"94- ? {op: "add", path, value: origValue}95- : {op: "replace", path, value: origValue}96+ ? {op: "add", path, value: origValue}97+ : {op: "replace", path, value: origValue}98 )99 })100 }101 45 unmodified linesmodified:src/proxy.js+3 added-1 removed
112 unmodified lines113 114 function getOwnPropertyDescriptor(state, prop) {115 const owner = state.modified116 ? state.copy117- : has(state.proxies, prop) ? state.proxies : state.base117+ : has(state.proxies, prop)118+ ? state.proxies119+ : state.base120 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)121 if (descriptor && !(Array.isArray(owner) && prop === "length"))122 descriptor.configurable = true123 return descriptor71 unmodified linesmodified:package.json+6 added-2 removed
13 unmodified lines14 "test:perf": "NODE_ENV=production yarn-or-npm build && cd __performance_tests__ && babel-node add-data.js && babel-node todo.js && babel-node incremental.js",15 "test:flow": "yarn-or-npm flow check",16 "coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",17 "build": "rimraf dist/ && cross-env NODE_ENV=production rollup -c && cpx \"src/immer.{d.ts,js.flow}\" dist",18- "prettier": "prettier \"*/**/*.js\" --ignore-path ./.prettierignore --write",18+ "prettier": "prettier \"*/**/*.js\" \"*/**/*.ts\" --ignore-path ./.prettierignore --write",19 "precommit": "lint-staged",20 "prepublish": "yarn-or-npm run build",21 "release": "np --no-cleanup"22 },54 unmodified lines77 "lint-staged": {78 "*.js": [79 "prettier --write",80 "git add"81+ ],82+ "*.ts": [83+ "prettier --write",84+ "git add"85 ]86 },87 "jest": {88 "transform": {15 unmodified lines104 "json",105 "node"106 ]107 }104-}108+}modified:yarn.lock+2 added-2 removed
3929 unmodified lines3930 version "0.2.0"3931 resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"3932 3933 prettier@^1.9.2:3934- version "1.9.2"3935- resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.9.2.tgz#96bc2132f7a32338e6078aeb29727178c6335827"3934+ version "1.14.3"3935+ resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.14.3.tgz#90238dd4c0684b7edce5f83b0fb7328e48bd0895"3936 3937 pretty-format@^21.2.1:3938 version "21.2.1"3939 resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-21.2.1.tgz#ae5407f3cf21066cd011aa1ba5fce7b6a2eddb36"1277 unmodified lines