Skip to content

Commit 8114af4

Browse files
alxnddrkulyk
andauthored
add basic typescript configuration (metabase#18284)
* add basic typescript configuration * convert a tiny hook use-debounced-value to typescript * allow any for now? * update terser and static viz configs * adjust config * change extension of QuestionActivityTimeline from jsx to js to match jest config * adjust config * skip failing tests * Fix missing question method in unit tests (metabase#18314) * review fix * fix Co-authored-by: Anton Kulyk <[email protected]>
1 parent 2829168 commit 8114af4

11 files changed

+346
-31
lines changed

.babelrc

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
"@babel/plugin-proposal-export-default-from",
66
["@babel/plugin-proposal-decorators", { "legacy": true }]
77
],
8-
"presets": ["@babel/preset-env", "@babel/preset-react"],
8+
"presets": [
9+
"@babel/preset-env",
10+
"@babel/preset-react",
11+
"@babel/preset-typescript"
12+
],
913
"env": {
1014
"development": {
1115
"presets": []

.eslintrc

+21-1
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,25 @@
5858
"ecmaFeatures": {
5959
"legacyDecorators": true
6060
}
61-
}
61+
},
62+
"overrides": [
63+
{
64+
"extends": ["plugin:@typescript-eslint/recommended"],
65+
"files": ["*.ts", "*.tsx"],
66+
"parser": "@typescript-eslint/parser",
67+
"parserOptions": {
68+
"project": "./tsconfig.json"
69+
},
70+
"plugins": ["@typescript-eslint"],
71+
"rules": {
72+
"@typescript-eslint/explicit-module-boundary-types": "off",
73+
"@typescript-eslint/no-inferrable-types": "off",
74+
"@typescript-eslint/no-explicit-any": "off",
75+
"@typescript-eslint/no-use-before-define": [
76+
"error",
77+
{ "functions": false }
78+
]
79+
}
80+
}
81+
]
6282
}

frontend/src/metabase/hooks/use-debounced-value.js renamed to frontend/src/metabase/hooks/use-debounced-value.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, useEffect } from "react";
22

3-
export function useDebouncedValue(value, delay) {
3+
export function useDebouncedValue<TVALUE>(value: TVALUE, delay: number) {
44
const [debouncedValue, setDebouncedValue] = useState(value);
55

66
useEffect(() => {

frontend/test/metabase/query_builder/components/QuestionActivityTimeline.unit.spec.jsx renamed to frontend/test/metabase/query_builder/components/QuestionActivityTimeline.unit.spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe("QuestionActivityTimeline", () => {
3434
beforeEach(() => {
3535
question = {
3636
canWrite: () => false,
37+
getModerationReviews: () => [],
3738
};
3839

3940
render(
@@ -54,6 +55,7 @@ describe("QuestionActivityTimeline", () => {
5455
beforeEach(() => {
5556
question = {
5657
canWrite: () => true,
58+
getModerationReviews: () => [],
5759
};
5860

5961
render(

jest.tz.unit.conf.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/frontend/test/__mocks__/fileMock.js",
55
"^promise-loader\\?global\\!metabase\\/lib\\/ga-metadata$": "<rootDir>/frontend/src/metabase/lib/ga-metadata.js"
66
},
7-
"testMatch": ["<rootDir>/frontend/test/**/*.tz.unit.spec.js?(x)"],
7+
"testMatch": ["<rootDir>/frontend/test/**/*.tz.unit.spec.{js,ts,jsx,tsx}"],
88
"modulePaths": ["<rootDir>/frontend/test", "<rootDir>/frontend/src"],
99
"setupFiles": [
1010
"<rootDir>/frontend/test/jest-setup.js",
@@ -18,7 +18,7 @@
1818
},
1919
"coverageDirectory": "./",
2020
"coverageReporters": ["text", "json-summary"],
21-
"collectCoverageFrom": ["frontend/src/**/*.js", "frontend/src/**/*.jsx"],
21+
"collectCoverageFrom": ["frontend/src/**/*.{js,ts,jsx,tsx}"],
2222
"coveragePathIgnorePatterns": [
2323
"/node_modules/",
2424
"/frontend/src/metabase/visualizations/lib/errors.js"

jest.unit.conf.json

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/frontend/test/__mocks__/fileMock.js",
55
"^promise-loader\\?global\\!metabase\\/lib\\/ga-metadata$": "<rootDir>/frontend/src/metabase/lib/ga-metadata.js"
66
},
7-
"testPathIgnorePatterns": ["<rootDir>/frontend/test/.*/.*.tz.unit.spec.js"],
8-
"testMatch": ["<rootDir>/**/*.unit.spec.js"],
7+
"testPathIgnorePatterns": ["<rootDir>/frontend/test/.*/.*.tz.unit.spec.{js,ts}"],
8+
"testMatch": ["<rootDir>/**/*.unit.spec.js", "<rootDir>/**/*.unit.spec.{js,ts}"],
99
"modulePaths": [
1010
"<rootDir>/frontend/test",
1111
"<rootDir>/frontend/src",
@@ -25,18 +25,16 @@
2525
"coverageDirectory": "./coverage",
2626
"coverageReporters": ["text", "html", "lcov"],
2727
"collectCoverageFrom": [
28-
"frontend/src/**/*.js",
29-
"frontend/src/**/*.jsx",
30-
"enterprise/frontend/src/**/*.js",
31-
"enterprise/frontend/src/**/*.jsx"
28+
"frontend/src/**/*.{js,jsx,ts,tsx}",
29+
"enterprise/frontend/src/**/*.{js,jsx,ts,tsx}"
3230
],
3331
"coveragePathIgnorePatterns": [
3432
"/node_modules/",
3533
"/frontend/src/metabase/visualizations/lib/errors.js",
3634
"/frontend/src/cljs/",
3735
"/frontend/src/metabase/internal/",
3836
"/frontend/test/",
39-
".styled.jsx",
37+
".styled.{jsx,tsx}",
4038
".info.js"
4139
],
4240
"testEnvironment": "jsdom",

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"@babel/plugin-transform-flow-strip-types": "^7.14.5",
106106
"@babel/preset-env": "^7.14.7",
107107
"@babel/preset-react": "^7.14.5",
108+
"@babel/preset-typescript": "^7.15.0",
108109
"@cypress/skip-test": "^2.6.0",
109110
"@cypress/webpack-preprocessor": "^5.9.1",
110111
"@percy/cli": "^1.0.0-beta.48",
@@ -114,6 +115,9 @@
114115
"@testing-library/jest-dom": "^4.0.0",
115116
"@testing-library/react": "^11.0.2",
116117
"@testing-library/user-event": "^12.6.0",
118+
"@types/styled-components": "^3.0.4",
119+
"@typescript-eslint/eslint-plugin": "^4.33.0",
120+
"@typescript-eslint/parser": "^4.33.0",
117121
"babel-eslint": "10.1.0",
118122
"babel-jest": "^27.0.6",
119123
"babel-loader": "^8.2.2",
@@ -154,6 +158,7 @@
154158
"raf": "^3.4.0",
155159
"shadow-cljs": "2.11.20",
156160
"style-loader": "^0.19.0",
161+
"typescript": "^4.4.3",
157162
"webpack": "^5.37.0",
158163
"webpack-cli": "^4.7.0",
159164
"webpack-dev-server": "^3.11.2",
@@ -167,7 +172,8 @@
167172
"concurrently": "yarn && concurrently --kill-others -p name",
168173
"dev": "yarn concurrently -n 'backend,frontend,cljs,docs' -c 'blue,green,yellow,magenta' 'clojure -M:run' 'yarn build-hot:js' 'yarn build-hot:cljs' 'yarn docs'",
169174
"dev-ee": "yarn concurrently -n 'backend,frontend,cljs,docs' -c 'blue,green,yellow,magenta' 'clojure -M:run:ee' 'MB_EDITION=ee yarn build-hot:js' 'MB_EDITION=ee yarn build-hot:cljs' 'yarn docs'",
170-
"lint": "yarn lint-eslint && yarn lint-prettier && yarn lint-docs-links && yarn lint-yaml",
175+
"type-check": "yarn && tsc --noEmit",
176+
"lint": "yarn lint-eslint && yarn lint-prettier && yarn lint-docs-links && yarn lint-yaml && yarn type-check",
171177
"lint-eslint": "yarn build-quick:cljs && eslint --ext .js --ext .jsx --rulesdir frontend/lint/eslint-rules --max-warnings 0 enterprise/frontend/src frontend/src frontend/test",
172178
"lint-prettier": "yarn && prettier -l '{enterprise/,}frontend/**/*.{js,jsx,css}' || (echo '\nThese files are not formatted correctly. Did you forget to \"yarn prettier\"?' && false)",
173179
"lint-docs-links": "yarn && ./bin/verify-doc-links",

tsconfig.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"compilerOptions": {
3+
"jsx": "react",
4+
"target": "esnext",
5+
"lib": ["dom", "dom.iterable", "esnext"],
6+
"allowSyntheticDefaultImports": true,
7+
"module": "commonjs",
8+
"isolatedModules": true,
9+
"strict": true,
10+
"moduleResolution": "node",
11+
"skipLibCheck": true,
12+
"paths": {
13+
"*": [
14+
"./frontend/src/*",
15+
"./frontend/test/*",
16+
"./enterprise/frontend/src/*",
17+
"./enterprise/frontend/test/*"
18+
]
19+
},
20+
"forceConsistentCasingInFileNames": true
21+
},
22+
"include": [
23+
"frontend/src/**/*.ts",
24+
"frontend/src/**/*.tsx",
25+
"enterprise/frontend/src/**/*.ts",
26+
"enterprise/frontend/src/**/*.tsx",
27+
"frontend/test/**/*.ts",
28+
"frontend/test/**/*.tsx",
29+
"enterprise/frontend/test/**/*.ts",
30+
"enterprise/frontend/test/**/*.tsx"
31+
],
32+
"exclude": ["node_modules"]
33+
}

webpack.config.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ const config = (module.exports = {
6363
module: {
6464
rules: [
6565
{
66-
test: /\.(js|jsx)$/,
66+
test: /\.(tsx?|jsx?)$/,
6767
exclude: /node_modules|cljs/,
6868
use: [{ loader: "babel-loader", options: BABEL_CONFIG }],
6969
},
7070
{
71-
test: /\.(js|jsx)$/,
71+
test: /\.(tsx?|jsx?)$/,
7272
exclude: /node_modules|cljs|\.spec\.js/,
7373
use: [
7474
{
@@ -101,7 +101,16 @@ const config = (module.exports = {
101101
],
102102
},
103103
resolve: {
104-
extensions: [".webpack.js", ".web.js", ".js", ".jsx", ".css", ".svg"],
104+
extensions: [
105+
".webpack.js",
106+
".web.js",
107+
".js",
108+
".jsx",
109+
".ts",
110+
".tsx",
111+
".css",
112+
".svg",
113+
],
105114
alias: {
106115
assets: ASSETS_PATH,
107116
fonts: FONTS_PATH,
@@ -269,14 +278,15 @@ if (WEBPACK_BUNDLE !== "production") {
269278
}),
270279
);
271280
} else {
272-
273281
// Don't bother with ESLint for CI/production (we catch linting errors with another CI run)
274282
config.module.rules = config.module.rules.filter(rule => {
275-
return Array.isArray(rule.use) ? rule.use[0].loader != "eslint-loader" : true
283+
return Array.isArray(rule.use)
284+
? rule.use[0].loader != "eslint-loader"
285+
: true;
276286
});
277287

278288
config.plugins.push(
279-
new TerserPlugin({ parallel: true, test: /\.jsx?($|\?)/i }),
289+
new TerserPlugin({ parallel: true, test: /\.(tsx?|jsx?)($|\?)/i }),
280290
);
281291

282292
config.devtool = "source-map";

webpack.static-viz.config.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ module.exports = {
2727
module: {
2828
rules: [
2929
{
30-
test: /\.(js|jsx)$/,
30+
test: /\.(tsx?|jsx?)$/,
3131
exclude: /node_modules/,
3232
use: [{ loader: "babel-loader", options: BABEL_CONFIG }],
3333
},
3434
{
35-
test: /\.(js|jsx)$/,
35+
test: /\.(tsx?|jsx?)$/,
3636
exclude: /node_modules/,
3737
use: [
3838
{
@@ -46,11 +46,9 @@ module.exports = {
4646
],
4747
},
4848
resolve: {
49-
extensions: [".webpack.js", ".web.js", ".js", ".jsx"],
49+
extensions: [".webpack.js", ".web.js", ".js", ".jsx", ".ts", ".tsx"],
5050
alias: {
5151
metabase: SRC_PATH,
5252
},
5353
},
54-
55-
}
56-
54+
};

0 commit comments

Comments
 (0)