Skip to content

Migrate to TypeScript #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 54 commits into from
Nov 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
d068194
rename files to TS
Khartir Sep 6, 2019
5665d4a
convert react-async to TS
Khartir Sep 6, 2019
797ef86
add linting for TS
Khartir Sep 6, 2019
c22687b
remove old types
Khartir Sep 6, 2019
3381fbe
Merge branch 'next' into switch-to-ts
ghengeveld Sep 6, 2019
5c9d146
fix storybook
Khartir Sep 6, 2019
89b1c08
rge branch 'next' into switch-to-ts
Khartir Sep 6, 2019
0321c69
add @pika/plugin-bundle-types
phryneas Sep 12, 2019
70393af
hotfix for type generation
phryneas Sep 16, 2019
5d545c9
add missing exports, lint
phryneas Sep 16, 2019
6aaa045
prevent multiple different typescript versions to be installed
phryneas Sep 20, 2019
65eebf3
move common types to types.ts
phryneas Sep 20, 2019
d370e89
jest config: also transform tsx files
phryneas Sep 20, 2019
430f7c3
readability of globalScope
phryneas Sep 20, 2019
f3d2284
move inline typecast to variable
phryneas Sep 20, 2019
2e5333c
better typings for createInstance and default Async
phryneas Sep 20, 2019
436e49b
remove unneccesary genererics
phryneas Sep 20, 2019
8dd1086
remove accidental types
phryneas Sep 20, 2019
e08b6ed
inline nullify into render
phryneas Sep 20, 2019
d709416
wildcard-export all types
phryneas Sep 20, 2019
9c5d5ce
introduce ReducerAsyncState type, type promise as optional undefined
phryneas Sep 20, 2019
e320bb2
Merge branch 'next' into switch-to-ts
Khartir Sep 20, 2019
663b060
remove unused imports
Khartir Sep 20, 2019
0512a0c
fix react being included in types rollup
Khartir Sep 20, 2019
a978568
fix enum names
Khartir Sep 20, 2019
825c9ba
implements MockAbortController
Khartir Sep 20, 2019
850aab7
set default options
Khartir Sep 20, 2019
ade7fe1
set types for fetch-callback
Khartir Sep 20, 2019
ca118fe
Fix failing test
Khartir Sep 20, 2019
306befa
Match propTypes to TS-types
Khartir Sep 20, 2019
c40d5d8
add tests for helpers with missing state
Khartir Sep 25, 2019
2cb5c78
prevent Async context consumers to be used outside of Async components
Oct 18, 2019
ef8deff
alias AsyncState<T> & AsyncConstructor<T> for readability
Oct 18, 2019
8ccfc0f
move MockAbortController out and also use it in Async component
Oct 18, 2019
e95a542
Revert "Allow overriding the 'resource' argument of 'fetch' when invo…
Khartir Oct 18, 2019
e73b0e0
Merge branch 'next' into switch-to-ts
Khartir Oct 18, 2019
bf9d08a
Reapply #150
Khartir Oct 18, 2019
f6245df
fix typo
Khartir Oct 21, 2019
bce7795
cleanup config
Khartir Oct 21, 2019
8bbcec4
remove yarn.lock
Khartir Oct 21, 2019
4e04af2
remove TS-dependency in package
Khartir Oct 21, 2019
7d55881
remove resolutions
Khartir Oct 23, 2019
3dc3fc1
update pika-packages
Khartir Oct 23, 2019
372c536
update dependencies
Khartir Oct 23, 2019
326b3bd
fix test
Khartir Oct 23, 2019
caf3b5d
remove dependency of react-async to fix chromatic
Khartir Oct 23, 2019
c8f99ed
Merge remote-tracking branch 'origin/next' into switch-to-ts
phryneas Oct 26, 2019
c23f462
add typescript as a dev dependency
phryneas Oct 26, 2019
46bbabb
fix resolutions for tests that switch react versions
phryneas Oct 26, 2019
638a1ee
add umd build, add fields for module, unpkgs, jsdelivr to final packa…
phryneas Oct 28, 2019
3f7c51c
Merge remote-tracking branch 'origin/next' into switch-to-ts
phryneas Oct 28, 2019
3bc18ce
Apply suggestions from code review
phryneas Oct 30, 2019
b4cbfd4
Merge branch 'next' into pr/Khartir/105
ghengeveld Nov 10, 2019
9a50543
Bump dependencies.
ghengeveld Nov 10, 2019
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
16 changes: 14 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,17 @@
"react": {
"version": "detect"
}
}
}
},
"overrides": [{
"files": "packages/**/*.{ts,tsx}",
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"modules": true
}
}
}]
}
8 changes: 8 additions & 0 deletions .storybook/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
module.exports = async ({ config }) => {
delete config.module.rules[0].include
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: [['react-app', { flow: false, typescript: true }]],
},
});
config.resolve.extensions.push('.ts', '.tsx');
return config
}
6 changes: 3 additions & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module.exports = {
presets: ["@babel/preset-react"],
plugins: ["@babel/plugin-proposal-object-rest-spread"],
plugins: ["@babel/plugin-proposal-object-rest-spread", "@babel/plugin-proposal-class-properties"],
Copy link
Member

Choose a reason for hiding this comment

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

I don't want to add a dependency on class properties, because it will force users to have the same babel config. It's not as commonly available as rest spread. Static class properties can be easily avoided by doing ClassName.method = ... outside of the class definition.

Copy link
Member

Choose a reason for hiding this comment

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

It isn't.
TypeScript transpiles class-properties away completely - we use babel only for the tests, so this is only a dependency for running the tests.


env: {
test: {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: ["@babel/plugin-transform-runtime"],
presets: ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
plugins: ["@babel/plugin-transform-runtime", "@babel/plugin-proposal-class-properties"],
},
},
}
2 changes: 1 addition & 1 deletion examples/with-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"now-build": "SKIP_PREFLIGHT_CHECK=true react-scripts build"
},
"dependencies": {
"@types/node": "12.12.6",
"@types/node": "12.12.7",
"@types/react": "16.9.11",
"@types/react-dom": "16.9.4",
"react": "16.11.0",
Expand Down
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module.exports = {
coverageDirectory: "<rootDir>/coverage",
verbose: true,
bail: true,
transform: { "^.+\\.js$": "babel-jest" },
transform: {
"^.+\\.[tj]sx?$": "babel-jest",
},
projects: ["<rootDir>/packages/*"],
setupFiles: ["<rootDir>/jest.setup.js"],
testPathIgnorePatterns: ["/node_modules/", "/pkg/"],
Expand Down
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
"start": "run-p start:*",
"start:examples": "now dev",
"start:storybook": "start-storybook -p 6006",
"lint": "eslint packages/*/src/*.js",
"lint": "eslint packages/*/src/*.{js,ts,tsx}",
"test": "jest packages/*/src/*.spec.js",
"test:watch": "yarn test -- --watch",
"test:devtools": "jest react-async-devtools/src",
"test:components": "jest src/Async.spec.js --collectCoverageFrom=src/Async.js",
"test:backwards": "yarn add -D -W react@16.3.1 react-dom@16.3.1 && yarn test:components",
"test:forwards": "yarn add -D -W react@next react-dom@next && yarn test",
"test:latest": "yarn add -D -W react@latest react-dom@latest && yarn test",
"test:backwards": "yarn add -D -W react@16.3.1 react-dom@16.3.1 && yarn resolutions:fix-react && yarn test:components",
"test:forwards": "yarn add -D -W react@next react-dom@next && yarn resolutions:fix-react && yarn test",
"test:latest": "yarn add -D -W react@latest react-dom@latest && yarn resolutions:fix-react && yarn test",
"test:compat": "yarn test:backwards && yarn test:forwards && yarn test:latest",
"test:examples": "CI=1 lerna run --scope '*-example' test -- --passWithNoTests --watchAll=false",
"test:chromatic": "chromatic --app-code iiua39bmt0j --build-script-name build:storybook",
"resolutions:fix-react": "jq '.resolutions.react = .devDependencies.react|.resolutions.\"react-dom\"=.devDependencies.react' package.json > package.json.new; mv package.json.new package.json; yarn install",
"ci": "yarn lint && yarn test:compat && yarn test:examples",
"build:packages": "lerna run --scope 'react-async*' build",
"build:examples": "lerna run --scope '*-example' build",
Expand All @@ -36,18 +37,25 @@
},
"devDependencies": {
"@babel/core": "7.7.2",
"@babel/plugin-proposal-class-properties": "7.7.0",
"@babel/plugin-proposal-object-rest-spread": "7.6.2",
"@babel/plugin-transform-runtime": "7.6.2",
"@babel/preset-env": "7.7.1",
"@babel/preset-react": "7.7.0",
"@babel/preset-typescript": "7.7.2",
"@pika/pack": "0.5.0",
"@pika/plugin-build-node": "0.7.1",
"@pika/plugin-build-types": "0.7.1",
"@pika/plugin-build-umd": "0.7.1",
"@pika/plugin-build-web": "0.7.1",
"@pika/plugin-bundle-types": "0.7.1",
"@pika/plugin-standard-pkg": "0.7.1",
"@pika/plugin-ts-standard-pkg": "0.7.1",
"@storybook/react": "5.2.6",
"@testing-library/jest-dom": "4.2.3",
"@testing-library/react": "9.3.2",
"@typescript-eslint/eslint-plugin": "2.6.1",
"@typescript-eslint/parser": "2.6.1",
"babel-eslint": "10.0.3",
"babel-jest": "24.9.0",
"babel-loader": "8.0.6",
Expand All @@ -61,13 +69,15 @@
"eslint-plugin-react-hooks": "2.2.0",
"jest": "24.9.0",
"lerna": "3.18.4",
"node-jq": "1.11.0",
"now": "16.4.4",
"npm-run-all": "4.1.5",
"prettier": "1.19.1",
"prop-types": "15.7.2",
"react": "16.11.0",
"react-async": "9.0.0",
"react-dom": "16.11.0",
"storybook-chromatic": "3.1.0"
"storybook-chromatic": "3.1.0",
"typescript": "3.7.2"
}
}
8 changes: 4 additions & 4 deletions packages/react-async-devtools/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import { actionTypes, reducer, globalScope } from "react-async"
import { ActionTypes, reducer, globalScope } from "react-async"

import { Root, Range, Checkbox, Label, Small, Ol, Li, Button } from "./components"

Expand All @@ -17,14 +17,14 @@ globalScope.__REACT_ASYNC__.devToolsDispatcher = (action, dispatch) => {
state.update(action)
}
switch (action.type) {
case actionTypes.start:
case ActionTypes.start:
if (state.intercept) {
dispatch({ ...action, payload: undefined })
state.update(action, run)
} else run()
break
case actionTypes.fulfill:
case actionTypes.reject:
case ActionTypes.fulfill:
case ActionTypes.reject:
setTimeout(run, state.latency * 1000)
break
default:
Expand Down
22 changes: 13 additions & 9 deletions packages/react-async/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,26 @@
"@pika/pack": {
"pipeline": [
[
"@pika/plugin-standard-pkg",
{
"exclude": [
"specs.js",
"*.spec.js"
]
}
"@pika/plugin-ts-standard-pkg"
],
[
"@pika/plugin-build-node"
],
[
"@pika/plugin-build-web"
"@pika/plugin-build-web",
{
"entrypoint": [
"module",
"unpkg",
"jsdelivr"
]
}
],
[
"@pika/plugin-build-umd"
],
[
"@pika/plugin-build-types"
"@pika/plugin-bundle-types"
]
]
}
Expand Down
32 changes: 32 additions & 0 deletions packages/react-async/src/Async.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("Async", () => {
<Async.Initial>
{value => {
one = value
return null
}}
</Async.Initial>
</Async>
Expand All @@ -47,6 +48,7 @@ describe("Async", () => {
<Async.Initial>
{value => {
two = value
return null
}}
</Async.Initial>
</Async>
Expand All @@ -55,6 +57,36 @@ describe("Async", () => {
})
})

describe("rendering context consumers without provider should throw an error", () => {
for (const Component of [
Async.Initial,
Async.Pending,
Async.Fulfilled,
Async.Rejected,
Async.Settled,
]) {
test("does not throw an error when rendered within <Async>", () => {
expect(() =>
render(
<Async>
<Component>{() => null}</Component>
</Async>
)
).not.toThrowError()
})
test("does throw an error when not rendered within <Async>", () => {
// Prevent the thrown error from showing up in test output by mocking console.error.
jest.spyOn(console, "error")
global.console.error.mockImplementation(() => {})

expect(() => render(<Component>{() => null}</Component>)).toThrowError()

// Restore the original console.error so other tests will still print errors that occur.
global.console.error.mockRestore()
})
}
})

describe("Async.Fulfilled", () => {
test("renders only after the promise is resolved", async () => {
const promiseFn = () => resolveTo("ok")
Expand Down
Loading