Skip to content

Commit d7d76c1

Browse files
authored
Migrate to TypeScript (#105)
Migrate to TypeScript
2 parents 5f1c1b1 + 9a50543 commit d7d76c1

26 files changed

+1084
-748
lines changed

.eslintrc

+14-2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,17 @@
2020
"react": {
2121
"version": "detect"
2222
}
23-
}
24-
}
23+
},
24+
"overrides": [{
25+
"files": "packages/**/*.{ts,tsx}",
26+
"parser": "@typescript-eslint/parser",
27+
"plugins": ["@typescript-eslint"],
28+
"parserOptions": {
29+
"ecmaVersion": 6,
30+
"sourceType": "module",
31+
"ecmaFeatures": {
32+
"modules": true
33+
}
34+
}
35+
}]
36+
}

.storybook/webpack.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
module.exports = async ({ config }) => {
22
delete config.module.rules[0].include
3+
config.module.rules.push({
4+
test: /\.(ts|tsx)$/,
5+
loader: require.resolve('babel-loader'),
6+
options: {
7+
presets: [['react-app', { flow: false, typescript: true }]],
8+
},
9+
});
10+
config.resolve.extensions.push('.ts', '.tsx');
311
return config
412
}

babel.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module.exports = {
22
presets: ["@babel/preset-react"],
3-
plugins: ["@babel/plugin-proposal-object-rest-spread"],
3+
plugins: ["@babel/plugin-proposal-object-rest-spread", "@babel/plugin-proposal-class-properties"],
44

55
env: {
66
test: {
7-
presets: ["@babel/preset-env", "@babel/preset-react"],
8-
plugins: ["@babel/plugin-transform-runtime"],
7+
presets: ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
8+
plugins: ["@babel/plugin-transform-runtime", "@babel/plugin-proposal-class-properties"],
99
},
1010
},
1111
}

examples/with-typescript/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"now-build": "SKIP_PREFLIGHT_CHECK=true react-scripts build"
1515
},
1616
"dependencies": {
17-
"@types/node": "12.12.6",
17+
"@types/node": "12.12.7",
1818
"@types/react": "16.9.11",
1919
"@types/react-dom": "16.9.4",
2020
"react": "16.11.0",

jest.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ module.exports = {
1717
coverageDirectory: "<rootDir>/coverage",
1818
verbose: true,
1919
bail: true,
20-
transform: { "^.+\\.js$": "babel-jest" },
20+
transform: {
21+
"^.+\\.[tj]sx?$": "babel-jest",
22+
},
2123
projects: ["<rootDir>/packages/*"],
2224
setupFiles: ["<rootDir>/jest.setup.js"],
2325
testPathIgnorePatterns: ["/node_modules/", "/pkg/"],

package.json

+15-5
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
"start": "run-p start:*",
1616
"start:examples": "now dev",
1717
"start:storybook": "start-storybook -p 6006",
18-
"lint": "eslint packages/*/src/*.js",
18+
"lint": "eslint packages/*/src/*.{js,ts,tsx}",
1919
"test": "jest packages/*/src/*.spec.js",
2020
"test:watch": "yarn test -- --watch",
2121
"test:devtools": "jest react-async-devtools/src",
2222
"test:components": "jest src/Async.spec.js --collectCoverageFrom=src/Async.js",
23-
"test:backwards": "yarn add -D -W react@16.3.1 react-dom@16.3.1 && yarn test:components",
24-
"test:forwards": "yarn add -D -W react@next react-dom@next && yarn test",
25-
"test:latest": "yarn add -D -W react@latest react-dom@latest && yarn test",
23+
"test:backwards": "yarn add -D -W react@16.3.1 react-dom@16.3.1 && yarn resolutions:fix-react && yarn test:components",
24+
"test:forwards": "yarn add -D -W react@next react-dom@next && yarn resolutions:fix-react && yarn test",
25+
"test:latest": "yarn add -D -W react@latest react-dom@latest && yarn resolutions:fix-react && yarn test",
2626
"test:compat": "yarn test:backwards && yarn test:forwards && yarn test:latest",
2727
"test:examples": "CI=1 lerna run --scope '*-example' test -- --passWithNoTests --watchAll=false",
2828
"test:chromatic": "chromatic --app-code iiua39bmt0j --build-script-name build:storybook",
29+
"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",
2930
"ci": "yarn lint && yarn test:compat && yarn test:examples",
3031
"build:packages": "lerna run --scope 'react-async*' build",
3132
"build:examples": "lerna run --scope '*-example' build",
@@ -36,18 +37,25 @@
3637
},
3738
"devDependencies": {
3839
"@babel/core": "7.7.2",
40+
"@babel/plugin-proposal-class-properties": "7.7.0",
3941
"@babel/plugin-proposal-object-rest-spread": "7.6.2",
4042
"@babel/plugin-transform-runtime": "7.6.2",
4143
"@babel/preset-env": "7.7.1",
4244
"@babel/preset-react": "7.7.0",
45+
"@babel/preset-typescript": "7.7.2",
4346
"@pika/pack": "0.5.0",
4447
"@pika/plugin-build-node": "0.7.1",
4548
"@pika/plugin-build-types": "0.7.1",
49+
"@pika/plugin-build-umd": "0.7.1",
4650
"@pika/plugin-build-web": "0.7.1",
51+
"@pika/plugin-bundle-types": "0.7.1",
4752
"@pika/plugin-standard-pkg": "0.7.1",
53+
"@pika/plugin-ts-standard-pkg": "0.7.1",
4854
"@storybook/react": "5.2.6",
4955
"@testing-library/jest-dom": "4.2.3",
5056
"@testing-library/react": "9.3.2",
57+
"@typescript-eslint/eslint-plugin": "2.6.1",
58+
"@typescript-eslint/parser": "2.6.1",
5159
"babel-eslint": "10.0.3",
5260
"babel-jest": "24.9.0",
5361
"babel-loader": "8.0.6",
@@ -61,13 +69,15 @@
6169
"eslint-plugin-react-hooks": "2.2.0",
6270
"jest": "24.9.0",
6371
"lerna": "3.18.4",
72+
"node-jq": "1.11.0",
6473
"now": "16.4.4",
6574
"npm-run-all": "4.1.5",
6675
"prettier": "1.19.1",
6776
"prop-types": "15.7.2",
6877
"react": "16.11.0",
6978
"react-async": "9.0.0",
7079
"react-dom": "16.11.0",
71-
"storybook-chromatic": "3.1.0"
80+
"storybook-chromatic": "3.1.0",
81+
"typescript": "3.7.2"
7282
}
7383
}

packages/react-async-devtools/src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react"
2-
import { actionTypes, reducer, globalScope } from "react-async"
2+
import { ActionTypes, reducer, globalScope } from "react-async"
33

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

@@ -17,14 +17,14 @@ globalScope.__REACT_ASYNC__.devToolsDispatcher = (action, dispatch) => {
1717
state.update(action)
1818
}
1919
switch (action.type) {
20-
case actionTypes.start:
20+
case ActionTypes.start:
2121
if (state.intercept) {
2222
dispatch({ ...action, payload: undefined })
2323
state.update(action, run)
2424
} else run()
2525
break
26-
case actionTypes.fulfill:
27-
case actionTypes.reject:
26+
case ActionTypes.fulfill:
27+
case ActionTypes.reject:
2828
setTimeout(run, state.latency * 1000)
2929
break
3030
default:

packages/react-async/package.json

+13-9
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,26 @@
2929
"@pika/pack": {
3030
"pipeline": [
3131
[
32-
"@pika/plugin-standard-pkg",
33-
{
34-
"exclude": [
35-
"specs.js",
36-
"*.spec.js"
37-
]
38-
}
32+
"@pika/plugin-ts-standard-pkg"
3933
],
4034
[
4135
"@pika/plugin-build-node"
4236
],
4337
[
44-
"@pika/plugin-build-web"
38+
"@pika/plugin-build-web",
39+
{
40+
"entrypoint": [
41+
"module",
42+
"unpkg",
43+
"jsdelivr"
44+
]
45+
}
46+
],
47+
[
48+
"@pika/plugin-build-umd"
4549
],
4650
[
47-
"@pika/plugin-build-types"
51+
"@pika/plugin-bundle-types"
4852
]
4953
]
5054
}

packages/react-async/src/Async.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe("Async", () => {
3838
<Async.Initial>
3939
{value => {
4040
one = value
41+
return null
4142
}}
4243
</Async.Initial>
4344
</Async>
@@ -47,6 +48,7 @@ describe("Async", () => {
4748
<Async.Initial>
4849
{value => {
4950
two = value
51+
return null
5052
}}
5153
</Async.Initial>
5254
</Async>
@@ -55,6 +57,36 @@ describe("Async", () => {
5557
})
5658
})
5759

60+
describe("rendering context consumers without provider should throw an error", () => {
61+
for (const Component of [
62+
Async.Initial,
63+
Async.Pending,
64+
Async.Fulfilled,
65+
Async.Rejected,
66+
Async.Settled,
67+
]) {
68+
test("does not throw an error when rendered within <Async>", () => {
69+
expect(() =>
70+
render(
71+
<Async>
72+
<Component>{() => null}</Component>
73+
</Async>
74+
)
75+
).not.toThrowError()
76+
})
77+
test("does throw an error when not rendered within <Async>", () => {
78+
// Prevent the thrown error from showing up in test output by mocking console.error.
79+
jest.spyOn(console, "error")
80+
global.console.error.mockImplementation(() => {})
81+
82+
expect(() => render(<Component>{() => null}</Component>)).toThrowError()
83+
84+
// Restore the original console.error so other tests will still print errors that occur.
85+
global.console.error.mockRestore()
86+
})
87+
}
88+
})
89+
5890
describe("Async.Fulfilled", () => {
5991
test("renders only after the promise is resolved", async () => {
6092
const promiseFn = () => resolveTo("ok")

0 commit comments

Comments
 (0)