Skip to content
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

[Chore] Run ts-migrate on core package #286

Closed
wants to merge 8 commits into from

Conversation

chenesan
Copy link
Contributor

Purpose

接下來會開始 migrate core package 裡的 typescript。原則上步驟如下:

  • ts-migrate 一口氣把所有檔案從 js 轉成 ts。
  • 一個一個修掉 @ts-expect-error 的 comment,加上 typing。

這支 PR 直接在 core package 上跑 ts-migrate 轉換所有的 js(除了 icon component / test),並且修正一些 lint error。

ts-migrate

這是 airbnb 製作的工具,目的是節省 ts migration 的時間。
我只有在 packages 底下跑 npx ts-migrate rename corenpx ts-migrate migrate core 兩個指令去轉換 core 裡面的程式碼(有先把 src/icons 列入 exclude)
這會自動把 js 轉換成 ts,並且盡它所能的加上 type。對於沒辦法解的 error,會加上 @ts-expect-error comment 來避免錯誤,之後我們再自己手解。

Review Tips

  • src/ 底下的東西不用看,因為這邊是 ts-migrate 轉過的 code,一些 coding style 可能有被改到、type 也有一堆 any,我想在接下來的 PR 加 types 的時候順道修正。
  • 有升 eslint-plugin-react,原因是把全部的 component 變成 tsx 後,eslint 因為 這個 issue 跑不起來。造成的影響可以見下面的留言。

Changes

  • a list of what have been done
  • maybe some code change

Risk

Usually none, if you have any please write it here.

TODOs

  • Describe what should be done outside of this PR
  • Maybe in other PRs or some manual actions.

Comment on lines +24 to +25
"no-unused-vars": "off"
"@typescript-eslint/no-unused-vars": ["error", { "ignoreRestSiblings": true }]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

這個是避免 import type 的時候造成 no-unused-vars error:
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md
ignoreRestSiblings 是為了讓 const { a, b, ...other } = props 不會出錯,有蠻多地方我們會這樣寫來濾掉不要的 props。

Comment on lines +26 to +29
# Temp turn off following rules because we're doing ts-migrate
# Will turn them on after migration completed.
"max-len": "warn"
"no-use-before-define": "warn"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

這就是在 ts-migrate 後暫時加入的 rule,理由是 ts-migrate 會加入太長的 @ts-expect-error comment,並且有產生一些會提前使用 type 的 code(違反 no-use-before-define)。

Copy link
Contributor

@benny0642 benny0642 Aug 26, 2020

Choose a reason for hiding this comment

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

稍微補充說明一下 @ts-expect-error 的用途:

The 3.9 release of TypeScript introduced @ts-expect-error comments. When a line is prefixed with a @ts-expect-error comment, TypeScript will suppress that error from being reported. If there’s no error, TypeScript will report that @ts-expect-error wasn’t necessary. In the Airbnb codebase we switched to using the @ts-expect-error comment instead of @ts-ignore.

簡單講標上 @ts-expect-error typescript 不會報錯,而且如果你修掉了,他還會提醒你要拿掉 @ts-expect-error

ref: https://medium.com/airbnb-engineering/ts-migrate-a-tool-for-migrating-to-typescript-at-scale-cd23bfeb5cc

Comment on lines +30 to +34
# It's fix after upgrading eslint-plugin-react to v7.20.6
# to keep consistency with rule config in eslint-config-ichef.
# just add `static-variables` in the first line.
# Should remove after we upgrade eslint-plugin-react in eslint-config-ichef.
'react/sort-comp': ['error', {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

這個是升了 eslint-plugin-react 到 7.20 後,有部分的 component 出現 react/sort-comp 的 error。
原因似乎是多了 static-variables 的 group,就會使一些本來不會出錯的地方出錯。
這裡把 eslint-config-ichef 的設定抄過來,加入 static-variables 來解決。
jsx-eslint/eslint-plugin-react#2408

Copy link
Contributor

@benny0642 benny0642 Aug 26, 2020

Choose a reason for hiding this comment

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

這個加在 eslint-config-ichef 中會不會比較好?因為在後台、online restaurant 都有用到 eslint-plugin-react,我們在共用的 eslint-config-ichef 修掉這個問題,就不用三個專案都寫類似的 rule 。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

應該是要這樣,我想說暫時先讓它 pass,最後再來收尾
目前是想先盡快把 ts 加上 typing

Copy link
Contributor

Choose a reason for hiding this comment

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

可以標個 #TODO: 方便日後搜尋XD

Copy link
Contributor Author

Choose a reason for hiding this comment

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

感謝班尼建議,有另外開個 task 了

@@ -7,6 +7,8 @@ const defaultConfigs = require('../../../configs/webpack.dist');
const packageDirname = process.cwd();

module.exports = webpackMerge(defaultConfigs, {
entry: './src/index.ts',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

對,因為現在是 index.ts 了。

Comment on lines +15 to 16
// eslint-disable-next-line react/prop-types
ineditable, // unwanted prop
Copy link
Contributor Author

Choose a reason for hiding this comment

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

這裡在升 eslint-plugin-react 也噴錯,但本意是拿掉不要的 props,想說先暫時 ignore 了

@chenesan chenesan marked this pull request as ready for review August 24, 2020 10:16
@chenesan chenesan self-assigned this Aug 24, 2020
Copy link
Contributor

@benny0642 benny0642 left a comment

Choose a reason for hiding this comment

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

一些 auto migrate 的檔案,那些新增上去的 type,我覺得我目前還無法判斷對錯,我選擇相信乙山、 airbnb 不會亂搞,以及 QE 們的測試。
Others LGTM

Copy link
Contributor

@zhusee2 zhusee2 left a comment

Choose a reason for hiding this comment

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

我覺得很酷

Comment on lines +30 to +34
# It's fix after upgrading eslint-plugin-react to v7.20.6
# to keep consistency with rule config in eslint-config-ichef.
# just add `static-variables` in the first line.
# Should remove after we upgrade eslint-plugin-react in eslint-config-ichef.
'react/sort-comp': ['error', {
Copy link
Contributor

Choose a reason for hiding this comment

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

可以標個 #TODO: 方便日後搜尋XD

@zhusee2
Copy link
Contributor

zhusee2 commented Aug 26, 2020

Benny 前面提到,轉換出來的 type 無法判斷對錯,我個人也是這樣覺得
不過我覺得這就是選擇轉換工具時要付出的成本啦,我們要預期:

  1. 可能 type 會跟想要的不一樣
  2. 之後還是得逐個檔案回去精修 type

@chenesan
Copy link
Contributor Author

決定不轉 ts,先 close 啦

@chenesan chenesan closed this Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants