Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ typings/
# Yarn Integrity file
.yarn-integrity

# rollup-plugin-typescript2 cache
.rpt2_cache

# dotenv environment variables file
.env

Expand Down
4 changes: 2 additions & 2 deletions __tests__/base.js → __tests__/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict"
import produce, {setAutoFreeze, setUseProxies} from "../src/immer"
import deepFreeze from "deep-freeze"
import cloneDeep from "lodash.clonedeep"
import deepFreeze = require("deep-freeze")
import cloneDeep = require("lodash.clonedeep")
import * as lodash from "lodash"

jest.setTimeout(1000)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 20 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test:flow": "yarn-or-npm flow check",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"build": "rimraf dist/ && cross-env NODE_ENV=production rollup -c && cpx \"src/immer.{d.ts,js.flow}\" dist",
"prettier": "prettier \"*/**/*.js\" --ignore-path ./.prettierignore --write",
"prettier": "prettier \"*/**/*.js\" \"*/**/*.ts\" --ignore-path ./.prettierignore --write",
"precommit": "lint-staged",
"prepublish": "yarn-or-npm run build",
"release": "np --no-cleanup"
Expand All @@ -38,10 +38,11 @@
"dist/"
],
"devDependencies": {
"@types/deep-freeze": "^0.1.1",
"@types/jest": "^22.0.0",
"@types/lodash.clonedeep": "^4.5.3",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-jest": "^22.0.4",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
Expand All @@ -66,20 +67,34 @@
"rollup-plugin-commonjs": "^8.2.6",
"rollup-plugin-filesize": "^1.5.0",
"rollup-plugin-node-resolve": "^3.0.2",
"rollup-plugin-typescript2": "^0.12.0",
"rollup-plugin-uglify": "^2.0.1",
"ts-jest": "^22.4.2",
"typescript": "^2.6.2",
"uglify-es": "^3.3.6",
"yarn-or-npm": "^2.0.4"
},
"lint-staged": {
"*.js": [
"*.ts": [
"prettier --write",
"git add"
]
},
"jest": {
"transform": {
"^.+\\.jsx?$": "babel-jest"
}
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"testPathIgnorePatterns": [
"__tests__/flow"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
}
4 changes: 3 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {minify} from "uglify-es"
import commonjs from "rollup-plugin-commonjs"
import filesize from "rollup-plugin-filesize"
import resolve from "rollup-plugin-node-resolve"
import typescript from "rollup-plugin-typescript2"
import uglify from "rollup-plugin-uglify"
import babel from "rollup-plugin-babel"

function getConfig(dest, format, ugly) {
const conf = {
input: "src/immer.js",
input: "src/immer.ts",
output: {
exports: "named",
file: dest,
Expand All @@ -20,6 +21,7 @@ function getConfig(dest, format, ugly) {
jsnext: true
}),
commonjs(),
typescript(),
babel({
babelrc: false,
presets: [
Expand Down
File renamed without changes.
7 changes: 2 additions & 5 deletions src/es5.js → src/es5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import {
is,
isProxyable,
isProxy,
PROXY_STATE,
shallowCopy,
RETURNED_AND_MODIFIED_ERROR,
has,
each,
finalize
} from "./common"
Expand Down Expand Up @@ -191,10 +191,7 @@ function shallowEqual(objA, objB) {
const keysB = Object.keys(objB)
if (keysA.length !== keysB.length) return false
for (let i = 0; i < keysA.length; i++) {
if (
!hasOwnProperty.call(objB, keysA[i]) ||
!is(objA[keysA[i]], objB[keysA[i]])
) {
if (!has(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/immer.js → src/immer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {produceEs5} from "./es5"
* @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified
* @returns {any} a new state, or the base state if nothing was modified
*/
export default function produce(baseState, producer) {
export default function produce(baseState, producer?) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these parameters have types defined?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually yes. I think the reason I added the question mark was to try to make __tests__/flow/ts.ts happier

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question mark is correct though, produce can be invoked with one argument, basically there are three forms:

produce(baseState, producer) => nextState
produce(producer) => baseState => nextState // curried form
produce(producer, initialState) => baseState => nextState // curried form with default state

// prettier-ignore
if (arguments.length !== 1 && arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)

Expand Down
4 changes: 2 additions & 2 deletions src/proxy.js → src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const objectTraps = {
deleteProperty,
getOwnPropertyDescriptor,
defineProperty,
setPrototypeOf() {
setPrototypeOf(): never {
throw new Error("Don't even try this...")
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ function getOwnPropertyDescriptor(state, prop) {
return descriptor
}

function defineProperty() {
function defineProperty(): never {
throw new Error(
"Immer does currently not support defining properties on draft objects"
)
Expand Down
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es2015",
"module": "es2015",
"moduleResolution": "node",
"noImplicitThis": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"removeComments": false,
"sourceMap": true,
"declaration": true,
"noEmitOnError": true,
"rootDir": "src",
"outDir": "dist",
"lib": ["es5", "es2015"]
},
"include": [
"src/**/*.ts"
]
}
Loading