Skip to content

Conversation

@silverwind
Copy link
Member

@silverwind silverwind commented Nov 3, 2025

A big step towards enabling strict mode in Typescript.

There was definitely a good share of potential bugs while refactoring this. When in doubt, I opted to keep the potentially broken behaviour. Notably, the DOMEvent type is gone, it was broken and we're better off with type assertions on e.target.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Nov 3, 2025
@silverwind silverwind changed the title Enable strictNullChecks Enable TypeScript strictNullChecks Nov 3, 2025
@silverwind silverwind added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Nov 3, 2025
@lunny
Copy link
Member

lunny commented Nov 13, 2025

Too many ! in the class variables, looks ugly! could we assigned anything on the constructor to avoid them? Or we can disable strictPropertyInitialization.

@silverwind
Copy link
Member Author

silverwind commented Nov 13, 2025

Might be possible to move up the ! to the constructor, yes. Generally, thought there are many unavoidable ! for anything queried out of the DOM and I don't think there is a way to avoid those.

Also, I don't think we should relax any of the strict options, unless there is a clear benefit.

Copy link
Member

@delvh delvh left a comment

Choose a reason for hiding this comment

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

LGTM apart from the regression

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Nov 17, 2025
@wxiaoguang wxiaoguang marked this pull request as draft November 23, 2025 10:51
silverwind added a commit that referenced this pull request Nov 27, 2025
- Update JS deps
- Regenerate SVGs
- Fix air `bin` deprecation
- Fix `monaco.languages.typescript` deprecation
- Remove `eslint-plugin-no-use-extend-native`, it's unnecessary with
typescript
- Enable new `@typescript-eslint` rules
- Disable `@typescript-eslint/no-redundant-type-constituents`, this rule
has bugs when not running under `strictNullChecks` (pending in
#35843).
* origin/main:
  [skip ci] Updated translations via Crowdin
  Fix bug when updating user email (go-gitea#36058)
  Add "Go to file", "Delete Directory" to repo file list page (go-gitea#35911)
  Replace `lint-go-gopls` with additional `govet` linters (go-gitea#36028)
  Fix Actions `pull_request.paths` being triggered incorrectly by rebase (go-gitea#36045)
  Fix error handling in mailer and wiki services (go-gitea#36041)
  Update JS deps, fix deprecations (go-gitea#36040)
  Fix incorrect viewed files counter if file has changed (go-gitea#36009)
@silverwind
Copy link
Member Author

silverwind commented Dec 2, 2025

@wxiaoguang for some reason you comments show up on the wrong lines. Can you correct them?

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Dec 2, 2025

@wxiaoguang for some reason you comments show up on the wrong lines.

GitHub's bug:

image

@silverwind
Copy link
Member Author

silverwind commented Dec 2, 2025

FYI comments show correctly in this link: d6dc25a

silverwind and others added 4 commits December 2, 2025 22:02
Co-authored-by: delvh <dev.lh@web.de>
Signed-off-by: silverwind <me@silverwind.io>
wxiaoguang and others added 2 commits December 3, 2025 09:42
Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
wxiaoguang and others added 2 commits December 3, 2025 09:51
Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
@wxiaoguang
Copy link
Contributor

Now the remaining problem (not in this PR's scope) is: how to handle the strict types clearly.

In our case, most are caused by querySelector and getAttribute. Actually we clearly know that these values must exist, but they are from HTML rendered by tmpl.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Dec 3, 2025
@silverwind
Copy link
Member Author

silverwind commented Dec 3, 2025

In our case, most are caused by querySelector and getAttribute. Actually we clearly know that these values must exist, but they are from HTML rendered by tmpl.

Generally I prefer to code defensively and stop the current operation, e.g. if guards.

For cases where one is absolutely sure a element exists, ! can be used, but even then, nothing is guaranteed in the DOM. There could be a browser extension or user customization JS messing with the DOM for example.

@silverwind silverwind enabled auto-merge (squash) December 3, 2025 02:10
@silverwind silverwind added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Dec 3, 2025
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Dec 3, 2025

In our case, most are caused by querySelector and getAttribute. Actually we clearly know that these values must exist, but they are from HTML rendered by tmpl.

Generally I prefer to code defensively and stop the current operation, e.g. if guards. For cases where one is absolutely sure a element exists, ! can be used, but even then, nothing is guaranteed in the DOM. There could be a browser extension messing with the DOM for example.

I think some util functions can help:


const el = mustQueryElem(document, '#xxxxx');
const val = mustGetAttr(el, 'data-my-attr');

In the must functions, if target doesn't exist, throw errors. Then we can catch the bugs as early as possible.

@silverwind silverwind merged commit 46d7ade into go-gitea:main Dec 3, 2025
23 checks passed
@GiteaBot GiteaBot added this to the 1.26.0 milestone Dec 3, 2025
@wxiaoguang wxiaoguang deleted the strictNullChecks2 branch December 3, 2025 02:13
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Dec 3, 2025
@silverwind
Copy link
Member Author

Generally I prefer to use as few such wrappers as possible, and I don't see a major benefit in using such must* wrappers. Wrappers makes it harder to contribute because one needs to familiarize with wrappers first.

@wxiaoguang
Copy link
Contributor

In the must functions, if target doesn't exist, throw errors.

Without this approach, some bugs will be difficult to find. For example: some data- attributes are missing. The ! helps nothing.

@silverwind
Copy link
Member Author

silverwind commented Dec 3, 2025

Yes, I noticed typescript does not complain about things like `${el.getAttribute('nonexist'}` which will stringify to 'null', so yes, some checks on this would be good. I think there is a eslint rule available for exactly this problem somewhere.

@silverwind
Copy link
Member Author

silverwind commented Dec 3, 2025

Relevant rule is: https://typescript-eslint.io/rules/restrict-template-expressions/ with allowNullish set to false.

zjjhot added a commit to zjjhot/gitea that referenced this pull request Dec 3, 2025
* giteaofficial/main:
  Enable TypeScript `strictNullChecks` (go-gitea#35843)
  Update go toolchain to 1.25.5 (go-gitea#36074)
  Revert "adopt changes" (was intendet for go-gitea#33356)
  adopt changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/frontend type/refactoring Existing code has been cleaned up. There should be no new functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants