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

feat(type-checking): Add type-checking pre-commit hooks #32261

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

alveifbklsiu259
Copy link

feat(type-checking): Add type-checking pre-commit hooks

SUMMARY

This PR introduces some pre-commit hooks to check TypeScript files using tscw-config. This enhancement aims to streamline the development process by running type checks only on changed files.

NOTES

  • Since different packages (e.g., superset-frontend, superset-websocket...) use their own tsconfig.json and may require different declaration files, multiple type-checking hooks are used.
  • JS/JSX files will be processed by the type-checking hooks, but checkJs is not enabled by default. To enable type-checking for JS/JSX files, either include //@ts-check at the top of each JS/JSX file or enable it in the package's tsconfig.json.
  • Each package, such as superset-frontend, superset-websocket... will include all files ending with .d.ts in their respective directories (recursively). However, if one needs to include third-party declaration files, one can either import them directly in the files that need them or declare them in the type field of the package's tsconfig.json. For example import "@testing-library/jest-dom"

DISCLOSURE

I am the author of tscw-config. I genuinely believe that this library offers an effective solution to the issue discussed in Issue #27379.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Fix Detected
Performance Blocking File System Operations ▹ view
Logging Incorrect Log Levels for stdout/stderr ▹ view
Error Handling Missing Error Context In Catch Block ▹ view
Files scanned
File Path Reviewed
scripts/check-type.js

Explore our documentation to understand the languages and file types we support and the files we ignore.

Need a new review? Comment /korbit-review on this PR and I'll review your latest changes.

Korbit Guide: Usage and Customization

Interacting with Korbit

  • You can manually ask Korbit to review your PR using the /korbit-review command in a comment at the root of your PR.
  • You can ask Korbit to generate a new PR description using the /korbit-generate-pr-description command in any comment on your PR.
  • Too many Korbit comments? I can resolve all my comment threads if you use the /korbit-resolve command in any comment on your PR.
  • Chat with Korbit on issues we post by tagging @korbit-ai in your reply.
  • Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.

Customizing Korbit

  • Check out our docs on how you can make Korbit work best for you and your team.
  • Customize Korbit for your organization through the Korbit Console.

Feedback and Support

Comment on lines 72 to 76
if (child.stdout) {
console.log(child.stdout);
} else {
console.log(child.stderr);
}

This comment was marked as resolved.

Comment on lines 53 to 57
let declarationFiles = getFilesRecursivelySync(
packageRootDir,
DECLARATION_FILE_REGEX,
excludedDeclarationDirs
);

This comment was marked as resolved.

Comment on lines 79 to 82
} catch (e) {
console.error(e);
exit(1);
}

This comment was marked as resolved.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Congrats on making your first PR and thank you for contributing to Superset! 🎉 ❤️

We hope to see you in our Slack community too! Not signed up? Use our Slack App to self-register.

… add error context

- Updated log levels for stdout and stderr to reflect appropriate severity.
- Refactored fs operations to use asynchronous method.
- Enhanced error handling by adding context information.
@sadpandajoe
Copy link
Member

@mistercrunch don't we already have some sort of type checking? Adding you since you did a lot of work to clean up our pre-commits

}

const packageRootDir = getPackage(packageArg);
const packagePathRegex = new RegExp(`^${packageRootDir}\/`);

Check failure

Code scanning / CodeQL

Regular expression injection High

This regular expression is constructed from a
command-line argument
.
Copy link
Author

Choose a reason for hiding this comment

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

Copy link

codecov bot commented Feb 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.45%. Comparing base (76d897e) to head (5040e52).
Report is 1448 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master   #32261       +/-   ##
===========================================
+ Coverage   60.48%   83.45%   +22.96%     
===========================================
  Files        1931      546     -1385     
  Lines       76236    39097    -37139     
  Branches     8568        0     -8568     
===========================================
- Hits        46114    32629    -13485     
+ Misses      28017     6468    -21549     
+ Partials     2105        0     -2105     
Flag Coverage Δ
hive 48.49% <ø> (-0.67%) ⬇️
javascript ?
mysql 75.85% <ø> (?)
postgres 75.92% <ø> (?)
presto 53.02% <ø> (-0.78%) ⬇️
python 83.45% <ø> (+19.96%) ⬆️
sqlite 75.43% <ø> (?)
unit 61.06% <ø> (+3.44%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mistercrunch
Copy link
Member

@sadpandajoe yes we do in CI, but this add the checks to pre-commit so issues can be caught at commit time, which is better. This adds in a fair amount of complexity to deliver on it.

Wondering if it would make sense to only do it on superset-frontend/**/js-and-ts-files to keep pre-commit.yml a bit simpler.

@alveifbklsiu259 is it all super fast?

hooks:
- id: type-checking-frontend
name: Type-Checking (Frontend)
entry: ./scripts/check-type.js
Copy link
Member

Choose a reason for hiding this comment

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

From my understanding this will check all the files, not just the ones touched in the commit is that right? Same for all other entries.

If so it seems this will greatly slow down commit speed.

You can see how I did it for eslint, but by using $@, I'm able to pass the list of files to check to the command, so it will only check the files touched in that specific commit.

Copy link
Member

Choose a reason for hiding this comment

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

know that npm run type take 10-30 seconds, we don't want to add this kind of lag for each commit, especially when you're not even touching these files.

Key here is to somehow pass the list of files touched in the commit (provided by pre-commit), and only run the checks on those files. Now guessing tsc may have to go and import all the related files referenced in that seed file, and could end up taking quite some time if you end up covering files-that-import-files-that-import-other-files, ...

Copy link
Author

@alveifbklsiu259 alveifbklsiu259 Feb 15, 2025

Choose a reason for hiding this comment

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

From my understanding this will check all the files, not just the ones touched in the commit is that right?

It seems that we don't have to explicitly pass $@, pre-commit will only passed the touched files:

// check-type.js
void (async () => {
  const args = process.argv.slice(2);
  console.log(args)
  exit(1)

1

is it all super fast?

I wouldn't say it is super fast, but it is faster than checking all files.

2
3

Key here is to somehow pass the list of files touched in the commit (provided by pre-commit), and only run the checks on those files.

This is exactly what this pre-commit hook does.

... may have to go and import all the related files referenced in that seed file, and could end up taking quite some time if you end up covering files-that-import-files-that-import-other-files

This is still true for this hook. For example:

// superset-frontend/src/components/AsyncAceEditor/index.tsx
import AsyncEsmComponent, {
  PlaceholderProps,
} from 'src/components/AsyncEsmComponent';

If superset-frontend/src/components/AsyncAceEditor/index.tsx is the only file in the staging area, running this hook will also check src/components/AsyncEsmComponent/index.tsx and the files it imports and so on.

Wondering if it would make sense to only do it on superset-frontend/**/js-and-ts-files to keep pre-commit.yml a bit simpler.

Sure, as long as we only want to type-check the files under superset-frontend, but we still need to exclude superset-frontend/cypress-base/, since superset-frontend/cypress-base/ uses a different tsconfig.json and it contains a conflicting declaration file. i.e. superset-frontend/cypress-base/cypress/support/index.d.ts.

- Removed type-checking-websocket hook
- Removed type-checking-embedded-sdk hook
- Removed type-checking-cypress hook
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