Skip to content

refactor: typescript implement #148

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

Closed
wants to merge 1 commit into from

Conversation

dancerphil
Copy link
Contributor

@dancerphil dancerphil commented Oct 26, 2021

Working in Progress

Do not merge.

This will let us provide d.ts in next major or minor release. #135

Something will include in this PR

  • ts implement for site

Something will not include in this but in next PR

  • remove recompose
  • use @testing-libary/react-hooks to add some tests

node rollup.js
node node_modules/.bin/postcss -o style/index.css src/styles/index.css
node scripts/fix-css.js style/index.css
rm -rf cjs/ && tsc
Copy link
Owner

Choose a reason for hiding this comment

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

恐怕我们不能现在就改成tsc编译。之前版本是一个bundle,它可以直接用于<script>引入等方式,而tsc的产物并不是打包的,这会是一个BREAKING

Copy link
Contributor Author

Choose a reason for hiding this comment

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

你觉得应该怎么处理?

Copy link
Owner

Choose a reason for hiding this comment

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

在这个大版本中,乖乖保留原来的rollup构建吧,下个大版本改为tsc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rollup 怎么生成 d.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.

版本控制可以交给你,我这个能发个 3.0-alpha.0 就好?
我想你后续对 3.0 也有很多构想吧。你可以在这个基础上搞。

Copy link
Owner

Choose a reason for hiding this comment

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

tsc生成定义,然后用rollup生成bundle,这样行不?入口是index.js -> index.d.ts,能对上应该就没问题

Copy link
Contributor Author

@dancerphil dancerphil Oct 27, 2021

Choose a reason for hiding this comment

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

可能的问题

  1. dependencies 要不要加上,因为可能会依赖他们的类型
  2. import {xxx} from 'react-diff-view/es/tokenize' 会被 ts 支持,但是 js 不支持,因为类型好像必须是 esm 的,没法合起来,不过也许可以只覆盖一个 index.js,其他的还是 esm?不知道 script 引入的要求是什么?

@@ -19,6 +26,7 @@ const UnifiedDecoration = ({hideGutter, className, gutterClassName, contentClass
);
}

// @ts-ignore
Copy link
Owner

Choose a reason for hiding this comment

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

这是children的类型问题?这个类型能不能再强化一下,弄成tuple的话,jsx能不能认出来……

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 类型推断上的问题,Children.count() 不会有推导,实际是对的,所以 ignore 了,不影响使用

Copy link
Owner

Choose a reason for hiding this comment

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

👌

viewType,
gutterType = 'default',
selectedChanges = [],
widgets = {},
Copy link
Owner

Choose a reason for hiding this comment

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

这里个[]{},最好放外面定义个常量?不然用默认值的时候,每次都是新对象,而react-diff-view整个性能非常依赖memo

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 {createContext, useContext} from 'react';

interface Context {
gutterType: 'default' | 'none' | 'anchor';
Copy link
Owner

Choose a reason for hiding this comment

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

这几个string literal union感觉应该是单独的类型,用到的地方不少

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

viewType: 'unified' | 'split';
selectedChanges: string[];
monotonous: boolean;
widgets: {
Copy link
Owner

Choose a reason for hiding this comment

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

Record<string, ReactNode>是不是比较好

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

widgets: {
[key: string]: any;
};
renderGutter: () => any;
Copy link
Owner

Choose a reason for hiding this comment

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

返回应该是ReactNode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

type: 'insert';
content: string;
isInsert: true;
isDelete?: boolean;
Copy link
Owner

Choose a reason for hiding this comment

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

这里应该固定值,没有或者false吧,都知道是insert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里严格会导致需要改代码,就不是初衷了

Copy link
Owner

Choose a reason for hiding this comment

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

有道理


export type Change = InsertChange | DeleteChange | NormalChange;

export interface HunkType {
Copy link
Owner

Choose a reason for hiding this comment

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

这个类型的命名我有点想法,一般XxxType会给人是一种枚举的感觉,叫HunkInfo之类的会不会更好,下同

Copy link
Contributor Author

Choose a reason for hiding this comment

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

可以

isBinary: boolean;
// 'rename' 和 'copy' 理论上存在但实际很少用到
type: 'add' | 'delete' | 'modify' | 'rename' | 'copy';

Copy link
Owner

Choose a reason for hiding this comment

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

多了空行

generateAnchorID?: (change: Change) => any;
selectedChanges?: string[];
tokens?: any[] | null;
widgets?: {
Copy link
Owner

Choose a reason for hiding this comment

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

统一用Record吧,我记得我们的TS lint规则里有一条这个,只是react-diff-view现在没用我们的规则

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好像没有,我从内部挪过来的

@@ -57,7 +64,7 @@ const findNearestNormalChangeIndex = ({changes}, start) => {
return -1;
};

const splitRangeToValidOnes = (hunks, start, end) => {
const splitRangeToValidOnes = (hunks: HunkType[], start: number, end: number): Array<[number, number]> => {
Copy link
Owner

Choose a reason for hiding this comment

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

[number, number]拉出来造一个Range的类型更有语义

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个 PR 不会改语义,合入之后可以改。

Copy link
Owner

Choose a reason for hiding this comment

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

我的意思是,放一个type Range = [number number],在类型上更有可读性

Copy link
Contributor Author

Choose a reason for hiding this comment

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

哦,明白了

@stale
Copy link

stale bot commented Nov 10, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 10, 2021
@stale stale bot removed the stale label Nov 10, 2021
@nghiepdev
Copy link

Up!!!

@garyray-k
Copy link

👀

@otakustay
Copy link
Owner

I decide to move all implementation to TypeScript and publish version 2 with some small breaking changes, please track #189 for progress.

@otakustay otakustay closed this Feb 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants