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

Scripts: Add TypeScript support to linting command #27143

Merged
merged 20 commits into from
Feb 25, 2021

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Nov 20, 2020

Description

With some TypeScript in Gutenberg, .ts and .tsx (TypeScript) files should undergo the similar formatting and linting to .js files.

How has this been tested?

Change a .ts file, stage, and commit it.
Work on @wordpress/react-i18n and see that lint issues are reported correctly.

Types of changes

Breaking change: Add lint setup and rules for TypeScript files.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@github-actions
Copy link

github-actions bot commented Nov 20, 2020

Size Change: +6.66 kB (0%)

Total Size: 1.39 MB

Filename Size Change
build/a11y/index.js 1.14 kB +1 B (0%)
build/annotations/index.js 3.79 kB +13 B (0%)
build/api-fetch/index.js 3.41 kB +5 B (0%)
build/block-directory/index.js 9.1 kB -2 B (0%)
build/block-editor/index.js 124 kB +91 B (0%)
build/block-editor/style-rtl.css 12.1 kB +15 B (0%)
build/block-editor/style.css 12.1 kB +16 B (0%)
build/block-library/blocks/button/style-rtl.css 479 B +19 B (+4%)
build/block-library/blocks/button/style.css 479 B +19 B (+4%)
build/block-library/blocks/buttons/editor-rtl.css 315 B +82 B (+35%) 🚨
build/block-library/blocks/buttons/editor.css 315 B +82 B (+35%) 🚨
build/block-library/blocks/buttons/style-rtl.css 364 B +61 B (+20%) 🚨
build/block-library/blocks/buttons/style.css 363 B +60 B (+20%) 🚨
build/block-library/blocks/navigation-link/editor.css 395 B -2 B (-1%)
build/block-library/blocks/query/editor-rtl.css 814 B +655 B (+412%) 🆘
build/block-library/blocks/query/editor.css 812 B +652 B (+408%) 🆘
build/block-library/blocks/site-logo/style-rtl.css 115 B -2 B (-2%)
build/block-library/blocks/site-logo/style.css 115 B -2 B (-2%)
build/block-library/blocks/social-links/style-rtl.css 1.32 kB -43 B (-3%)
build/block-library/blocks/social-links/style.css 1.32 kB -42 B (-3%)
build/block-library/blocks/table/editor-rtl.css 478 B -11 B (-2%)
build/block-library/blocks/table/editor.css 478 B -11 B (-2%)
build/block-library/blocks/table/style-rtl.css 390 B +4 B (+1%)
build/block-library/blocks/table/style.css 390 B +4 B (+1%)
build/block-library/editor-rtl.css 9.44 kB +390 B (+4%)
build/block-library/editor.css 9.43 kB +390 B (+4%)
build/block-library/index.js 148 kB +3.32 kB (+2%)
build/block-library/style-rtl.css 8.83 kB +21 B (0%)
build/block-library/style.css 8.84 kB +22 B (0%)
build/block-library/theme-rtl.css 736 B -12 B (-2%)
build/block-library/theme.css 736 B -12 B (-2%)
build/blocks/index.js 48.3 kB +22 B (0%)
build/components/index.js 272 kB +91 B (0%)
build/compose/index.js 11.1 kB +3 B (0%)
build/core-data/index.js 16.8 kB +9 B (0%)
build/customize-widgets/index.js 4.08 kB +4 B (0%)
build/data-controls/index.js 831 B +1 B (0%)
build/data/index.js 8.87 kB +7 B (0%)
build/date/index.js 31.8 kB +1 B (0%)
build/deprecated/index.js 769 B +1 B (0%)
build/dom/index.js 4.95 kB +17 B (0%)
build/edit-navigation/index.js 11 kB +1 B (0%)
build/edit-post/index.js 307 kB +162 B (0%)
build/edit-site/index.js 26.4 kB +265 B (+1%)
build/edit-widgets/index.js 20.1 kB +164 B (+1%)
build/editor/index.js 42.1 kB +5 B (0%)
build/element/index.js 4.62 kB +7 B (0%)
build/format-library/index.js 6.77 kB -2 B (0%)
build/i18n/index.js 4.01 kB +4 B (0%)
build/keyboard-shortcuts/index.js 2.54 kB +7 B (0%)
build/keycodes/index.js 1.96 kB +7 B (0%)
build/media-utils/index.js 5.36 kB +7 B (0%)
build/notices/index.js 1.86 kB +3 B (0%)
build/nux/index.js 3.42 kB +3 B (0%)
build/plugins/index.js 2.55 kB +2 B (0%)
build/primitives/index.js 1.42 kB +6 B (0%)
build/priority-queue/index.js 791 B +1 B (0%)
build/redux-routine/index.js 2.83 kB +2 B (0%)
build/reusable-blocks/index.js 3.77 kB +5 B (0%)
build/rich-text/index.js 13.4 kB +11 B (0%)
build/server-side-render/index.js 2.82 kB +53 B (+2%)
build/token-list/index.js 1.27 kB -1 B (0%)
build/url/index.js 3.02 kB +2 B (0%)
build/viewport/index.js 1.86 kB +1 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/autop/index.js 2.84 kB 0 B
build/blob/index.js 665 B 0 B
build/block-directory/style-rtl.css 1.01 kB 0 B
build/block-directory/style.css 1.01 kB 0 B
build/block-library/blocks/archives/editor-rtl.css 61 B 0 B
build/block-library/blocks/archives/editor.css 60 B 0 B
build/block-library/blocks/audio/editor-rtl.css 58 B 0 B
build/block-library/blocks/audio/editor.css 58 B 0 B
build/block-library/blocks/audio/style-rtl.css 103 B 0 B
build/block-library/blocks/audio/style.css 103 B 0 B
build/block-library/blocks/block/editor-rtl.css 161 B 0 B
build/block-library/blocks/block/editor.css 161 B 0 B
build/block-library/blocks/button/editor-rtl.css 475 B 0 B
build/block-library/blocks/button/editor.css 474 B 0 B
build/block-library/blocks/calendar/style-rtl.css 208 B 0 B
build/block-library/blocks/calendar/style.css 208 B 0 B
build/block-library/blocks/categories/editor-rtl.css 84 B 0 B
build/block-library/blocks/categories/editor.css 83 B 0 B
build/block-library/blocks/categories/style-rtl.css 79 B 0 B
build/block-library/blocks/categories/style.css 79 B 0 B
build/block-library/blocks/code/style-rtl.css 90 B 0 B
build/block-library/blocks/code/style.css 90 B 0 B
build/block-library/blocks/columns/editor-rtl.css 190 B 0 B
build/block-library/blocks/columns/editor.css 190 B 0 B
build/block-library/blocks/columns/style-rtl.css 421 B 0 B
build/block-library/blocks/columns/style.css 421 B 0 B
build/block-library/blocks/cover/editor-rtl.css 390 B 0 B
build/block-library/blocks/cover/editor.css 389 B 0 B
build/block-library/blocks/cover/style-rtl.css 1.25 kB 0 B
build/block-library/blocks/cover/style.css 1.25 kB 0 B
build/block-library/blocks/embed/editor-rtl.css 486 B 0 B
build/block-library/blocks/embed/editor.css 486 B 0 B
build/block-library/blocks/embed/style-rtl.css 396 B 0 B
build/block-library/blocks/embed/style.css 395 B 0 B
build/block-library/blocks/file/editor-rtl.css 199 B 0 B
build/block-library/blocks/file/editor.css 198 B 0 B
build/block-library/blocks/file/style-rtl.css 248 B 0 B
build/block-library/blocks/file/style.css 248 B 0 B
build/block-library/blocks/freeform/editor-rtl.css 2.45 kB 0 B
build/block-library/blocks/freeform/editor.css 2.45 kB 0 B
build/block-library/blocks/gallery/editor-rtl.css 689 B 0 B
build/block-library/blocks/gallery/editor.css 690 B 0 B
build/block-library/blocks/gallery/style-rtl.css 1.07 kB 0 B
build/block-library/blocks/gallery/style.css 1.06 kB 0 B
build/block-library/blocks/group/editor-rtl.css 318 B 0 B
build/block-library/blocks/group/editor.css 317 B 0 B
build/block-library/blocks/group/style-rtl.css 57 B 0 B
build/block-library/blocks/group/style.css 57 B 0 B
build/block-library/blocks/heading/editor-rtl.css 129 B 0 B
build/block-library/blocks/heading/editor.css 129 B 0 B
build/block-library/blocks/heading/style-rtl.css 76 B 0 B
build/block-library/blocks/heading/style.css 76 B 0 B
build/block-library/blocks/html/editor-rtl.css 281 B 0 B
build/block-library/blocks/html/editor.css 281 B 0 B
build/block-library/blocks/image/editor-rtl.css 717 B 0 B
build/block-library/blocks/image/editor.css 716 B 0 B
build/block-library/blocks/image/style-rtl.css 477 B 0 B
build/block-library/blocks/image/style.css 478 B 0 B
build/block-library/blocks/latest-comments/editor-rtl.css 159 B 0 B
build/block-library/blocks/latest-comments/editor.css 158 B 0 B
build/block-library/blocks/latest-comments/style-rtl.css 269 B 0 B
build/block-library/blocks/latest-comments/style.css 269 B 0 B
build/block-library/blocks/latest-posts/editor-rtl.css 137 B 0 B
build/block-library/blocks/latest-posts/editor.css 137 B 0 B
build/block-library/blocks/latest-posts/style-rtl.css 523 B 0 B
build/block-library/blocks/latest-posts/style.css 522 B 0 B
build/block-library/blocks/list/editor-rtl.css 65 B 0 B
build/block-library/blocks/list/editor.css 65 B 0 B
build/block-library/blocks/list/style-rtl.css 63 B 0 B
build/block-library/blocks/list/style.css 63 B 0 B
build/block-library/blocks/media-text/editor-rtl.css 191 B 0 B
build/block-library/blocks/media-text/editor.css 191 B 0 B
build/block-library/blocks/media-text/style-rtl.css 535 B 0 B
build/block-library/blocks/media-text/style.css 532 B 0 B
build/block-library/blocks/more/editor-rtl.css 434 B 0 B
build/block-library/blocks/more/editor.css 434 B 0 B
build/block-library/blocks/navigation-link/editor-rtl.css 395 B 0 B
build/block-library/blocks/navigation-link/style-rtl.css 704 B 0 B
build/block-library/blocks/navigation-link/style.css 702 B 0 B
build/block-library/blocks/navigation/editor-rtl.css 1.34 kB 0 B
build/block-library/blocks/navigation/editor.css 1.34 kB 0 B
build/block-library/blocks/navigation/style-rtl.css 195 B 0 B
build/block-library/blocks/navigation/style.css 195 B 0 B
build/block-library/blocks/nextpage/editor-rtl.css 395 B 0 B
build/block-library/blocks/nextpage/editor.css 395 B 0 B
build/block-library/blocks/page-list/editor-rtl.css 214 B 0 B
build/block-library/blocks/page-list/editor.css 214 B 0 B
build/block-library/blocks/page-list/style-rtl.css 527 B 0 B
build/block-library/blocks/page-list/style.css 526 B 0 B
build/block-library/blocks/paragraph/editor-rtl.css 109 B 0 B
build/block-library/blocks/paragraph/editor.css 109 B 0 B
build/block-library/blocks/paragraph/style-rtl.css 273 B 0 B
build/block-library/blocks/paragraph/style.css 273 B 0 B
build/block-library/blocks/post-author/editor-rtl.css 209 B 0 B
build/block-library/blocks/post-author/editor.css 209 B 0 B
build/block-library/blocks/post-author/style-rtl.css 183 B 0 B
build/block-library/blocks/post-author/style.css 184 B 0 B
build/block-library/blocks/post-comments-form/style-rtl.css 250 B 0 B
build/block-library/blocks/post-comments-form/style.css 250 B 0 B
build/block-library/blocks/post-content/editor-rtl.css 139 B 0 B
build/block-library/blocks/post-content/editor.css 139 B 0 B
build/block-library/blocks/post-excerpt/editor-rtl.css 73 B 0 B
build/block-library/blocks/post-excerpt/editor.css 73 B 0 B
build/block-library/blocks/post-featured-image/editor-rtl.css 338 B 0 B
build/block-library/blocks/post-featured-image/editor.css 338 B 0 B
build/block-library/blocks/post-featured-image/style-rtl.css 100 B 0 B
build/block-library/blocks/post-featured-image/style.css 100 B 0 B
build/block-library/blocks/preformatted/style-rtl.css 63 B 0 B
build/block-library/blocks/preformatted/style.css 63 B 0 B
build/block-library/blocks/pullquote/editor-rtl.css 183 B 0 B
build/block-library/blocks/pullquote/editor.css 183 B 0 B
build/block-library/blocks/pullquote/style-rtl.css 316 B 0 B
build/block-library/blocks/pullquote/style.css 316 B 0 B
build/block-library/blocks/query-loop/editor-rtl.css 90 B 0 B
build/block-library/blocks/query-loop/editor.css 89 B 0 B
build/block-library/blocks/query-loop/style-rtl.css 315 B 0 B
build/block-library/blocks/query-loop/style.css 317 B 0 B
build/block-library/blocks/query-pagination-numbers/editor-rtl.css 122 B 0 B
build/block-library/blocks/query-pagination-numbers/editor.css 121 B 0 B
build/block-library/blocks/query-pagination/editor-rtl.css 270 B 0 B
build/block-library/blocks/query-pagination/editor.css 262 B 0 B
build/block-library/blocks/query-pagination/style-rtl.css 168 B 0 B
build/block-library/blocks/query-pagination/style.css 168 B 0 B
build/block-library/blocks/quote/editor-rtl.css 61 B 0 B
build/block-library/blocks/quote/editor.css 61 B 0 B
build/block-library/blocks/quote/style-rtl.css 169 B 0 B
build/block-library/blocks/quote/style.css 169 B 0 B
build/block-library/blocks/rss/editor-rtl.css 201 B 0 B
build/block-library/blocks/rss/editor.css 202 B 0 B
build/block-library/blocks/rss/style-rtl.css 290 B 0 B
build/block-library/blocks/rss/style.css 290 B 0 B
build/block-library/blocks/search/editor-rtl.css 165 B 0 B
build/block-library/blocks/search/editor.css 165 B 0 B
build/block-library/blocks/search/style-rtl.css 342 B 0 B
build/block-library/blocks/search/style.css 344 B 0 B
build/block-library/blocks/separator/editor-rtl.css 99 B 0 B
build/block-library/blocks/separator/editor.css 99 B 0 B
build/block-library/blocks/separator/style-rtl.css 236 B 0 B
build/block-library/blocks/separator/style.css 236 B 0 B
build/block-library/blocks/shortcode/editor-rtl.css 504 B 0 B
build/block-library/blocks/shortcode/editor.css 504 B 0 B
build/block-library/blocks/site-logo/editor-rtl.css 201 B 0 B
build/block-library/blocks/site-logo/editor.css 201 B 0 B
build/block-library/blocks/social-link/editor-rtl.css 164 B 0 B
build/block-library/blocks/social-link/editor.css 165 B 0 B
build/block-library/blocks/social-links/editor-rtl.css 696 B 0 B
build/block-library/blocks/social-links/editor.css 696 B 0 B
build/block-library/blocks/spacer/editor-rtl.css 302 B 0 B
build/block-library/blocks/spacer/editor.css 302 B 0 B
build/block-library/blocks/spacer/style-rtl.css 48 B 0 B
build/block-library/blocks/spacer/style.css 48 B 0 B
build/block-library/blocks/subhead/editor-rtl.css 99 B 0 B
build/block-library/blocks/subhead/editor.css 99 B 0 B
build/block-library/blocks/subhead/style-rtl.css 80 B 0 B
build/block-library/blocks/subhead/style.css 80 B 0 B
build/block-library/blocks/tag-cloud/editor-rtl.css 118 B 0 B
build/block-library/blocks/tag-cloud/editor.css 118 B 0 B
build/block-library/blocks/tag-cloud/style-rtl.css 94 B 0 B
build/block-library/blocks/tag-cloud/style.css 94 B 0 B
build/block-library/blocks/template-part/editor-rtl.css 557 B 0 B
build/block-library/blocks/template-part/editor.css 556 B 0 B
build/block-library/blocks/text-columns/editor-rtl.css 95 B 0 B
build/block-library/blocks/text-columns/editor.css 95 B 0 B
build/block-library/blocks/text-columns/style-rtl.css 166 B 0 B
build/block-library/blocks/text-columns/style.css 166 B 0 B
build/block-library/blocks/verse/editor-rtl.css 62 B 0 B
build/block-library/blocks/verse/editor.css 62 B 0 B
build/block-library/blocks/verse/style-rtl.css 87 B 0 B
build/block-library/blocks/verse/style.css 87 B 0 B
build/block-library/blocks/video/editor-rtl.css 504 B 0 B
build/block-library/blocks/video/editor.css 503 B 0 B
build/block-library/blocks/video/style-rtl.css 193 B 0 B
build/block-library/blocks/video/style.css 193 B 0 B
build/block-library/common-rtl.css 1.08 kB 0 B
build/block-library/common.css 1.08 kB 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.06 kB 0 B
build/components/style-rtl.css 15.5 kB 0 B
build/components/style.css 15.5 kB 0 B
build/customize-widgets/style-rtl.css 168 B 0 B
build/customize-widgets/style.css 168 B 0 B
build/dom-ready/index.js 576 B 0 B
build/edit-navigation/style-rtl.css 1.26 kB 0 B
build/edit-navigation/style.css 1.25 kB 0 B
build/edit-post/style-rtl.css 6.81 kB 0 B
build/edit-post/style.css 6.8 kB 0 B
build/edit-site/style-rtl.css 4.41 kB 0 B
build/edit-site/style.css 4.41 kB 0 B
build/edit-widgets/style-rtl.css 3.2 kB 0 B
build/edit-widgets/style.css 3.2 kB 0 B
build/editor/editor-styles-rtl.css 543 B 0 B
build/editor/editor-styles.css 545 B 0 B
build/editor/style-rtl.css 3.89 kB 0 B
build/editor/style.css 3.89 kB 0 B
build/escape-html/index.js 735 B 0 B
build/format-library/style-rtl.css 637 B 0 B
build/format-library/style.css 639 B 0 B
build/hooks/index.js 2.28 kB 0 B
build/html-entities/index.js 622 B 0 B
build/is-shallow-equal/index.js 698 B 0 B
build/list-reusable-blocks/index.js 3.14 kB 0 B
build/list-reusable-blocks/style-rtl.css 629 B 0 B
build/list-reusable-blocks/style.css 628 B 0 B
build/nux/style-rtl.css 731 B 0 B
build/nux/style.css 727 B 0 B
build/react-i18n/index.js 1.45 kB 0 B
build/reusable-blocks/style-rtl.css 225 B 0 B
build/reusable-blocks/style.css 225 B 0 B
build/shortcode/index.js 1.7 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.22 kB 0 B

compressed-size-action

package.json Show resolved Hide resolved
package-lock.json Outdated Show resolved Hide resolved
@gziolo gziolo added the [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. label Nov 20, 2020
@sirreal sirreal force-pushed the update/lint-staged-ts branch from f18c6f5 to f1fe9fe Compare November 24, 2020 22:11
packages/eslint-plugin/package.json Outdated Show resolved Hide resolved
@@ -31,12 +31,14 @@
],
"main": "index.js",
"dependencies": {
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
Copy link
Member Author

Choose a reason for hiding this comment

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

Parser is required for TS syntax.

packages/eslint-plugin/package.json Outdated Show resolved Hide resolved
packages/data/src/types.ts Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@gziolo
Copy link
Member

gziolo commented Nov 25, 2020

This is where we are when calling npm run lint-js -- --quiet:

The prop value with an expression type of JSXFragment could not be resolved. Please file issue to get this fixed immediately.

/Users/gziolo/Projects/gutenberg/packages/data/src/types.ts
   7:15  error  'listener' is defined but never used  no-unused-vars
  19:17  error  'registry' is defined but never used  no-unused-vars
  23:13  error  'state' is defined but never used     no-unused-vars
  23:25  error  'action' is defined but never used    no-unused-vars
  31:14  error  'store' is defined but never used     no-unused-vars

/Users/gziolo/Projects/gutenberg/packages/editor/src/components/post-saved-state/index.js
  79:26  error  ["_links"] is better written in dot notation  dot-notation

/Users/gziolo/Projects/gutenberg/packages/editor/src/components/post-trash/check.js
  21:31  error  ["rest_base"] is better written in dot notation  dot-notation

✖ 7 problems (7 errors, 0 warnings)
  2 errors and 0 warnings potentially fixable with the `--fix` option.

.eslintignore Outdated Show resolved Hide resolved
@sirreal sirreal force-pushed the update/lint-staged-ts branch from 02108a5 to 629ece7 Compare February 12, 2021 09:02
@sirreal
Copy link
Member Author

sirreal commented Feb 12, 2021

I fixed up some lint issues just to demo this. The when committing the react-i18n changes, lint-stages reports:

husky > pre-commit (node v14.15.5)
  ↓ Stashing changes... [skipped]
    → No partially staged files found...
  ❯ Running tasks...
    ↓ Running tasks for package-lock.json [skipped]
      → No staged files match package-lock.json
    ↓ Running tasks for packages/*/package.json [skipped]
      → No staged files match packages/*/package.json
    ↓ Running tasks for *.scss [skipped]
      → No staged files match *.scss
    ❯ Running tasks for *.{js,ts,tsx}
      ✔ wp-scripts format-js
      ✖ wp-scripts lint-js
    ↓ Running tasks for {docs/{toc.json,tool/*.js},packages/{*/README.md,components/src/*/**/README.md}} [skipped]
      → No staged files match {docs/{toc.json,tool/*.js},packages/{*/README.md,components/src/*/**/README.md}}
    ✔ Running tasks for packages/**/*.{js,ts,tsx}



✖ wp-scripts lint-js found some errors. Please fix them and try committing again.

…/gutenberg/packages/react-i18n/src/index.tsx
   30:1  error  Missing JSDoc @param "i18n" declaration     jsdoc/require-param
   48:1  error  Missing JSDoc @param "props" declaration    jsdoc/require-param
  116:0  error  Missing JSDoc @param "InnerComponent" type  jsdoc/require-param-type
  117:0  error  Missing JSDoc @return type                  jsdoc/require-returns-type

✖ 4 problems (4 errors, 0 warnings)
  2 errors and 0 warnings potentially fixable with the `--fix` option.

@sirreal sirreal marked this pull request as ready for review February 12, 2021 09:16
@sirreal
Copy link
Member Author

sirreal commented Feb 12, 2021

This is failing because some existing TS files have lint errors. There seem to be a lot of formatting issues at least. I'd appreciate if anyone wants to help with the cleanup.

@sirreal sirreal added [Tool] ESLint plugin /packages/eslint-plugin [Tool] WP Scripts /packages/scripts [Status] In Progress Tracking issues with work in progress [Type] Breaking Change For PRs that introduce a change that will break existing functionality [Type] Build Tooling Issues or PRs related to build tooling Needs Dev Ready for, and needs developer efforts labels Feb 12, 2021
@gziolo
Copy link
Member

gziolo commented Feb 12, 2021

This is failing because some existing TS files have lint errors. There seem to be a lot of formatting issues at least. I'd appreciate if anyone wants to help with the cleanup.

How many errors are we talking about?

Should we start with enabling linting for TS only in one of the packages to keep this PR small? Do you feel comfortable allowing TS linting in @wordpress/scripts? It's perfectly fine to add linting for files with TS extensions, but the rest might wait. I'm just checking since it means breaking changes into two packages 😃

*/
// Disable reason: Type-only import, this is fine. See https://github.com/typescript-eslint/typescript-eslint/issues/2661
// eslint-disable-next-line no-restricted-imports
import type { ComponentType, PropsWithChildren } from 'react';
Copy link
Member

Choose a reason for hiding this comment

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

It depends how you look at this ESLint warning 😃 We bring React API from @wordpress/element that acts as a proxy. It wouldn't be that bad idea to re-export React types used from @wordpress/element to align with how you use code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree up to a point.

We looked at doing that and it was a nightmare at the time and would've required a lot of maintenance effort for little or no benefit. There's no way to work nicely with types in the module import/export system in JSDoc. We would've had to manually re-export every single type (with their type arguments!) one by one via JSDoc.

Now that we're allowing some TypeScript, it may be more feasible to re-export all the types, but I haven't looked in many months. It still won't be as easy as something like export type * from 'react' as far as I know.

Copy link
Member

@gziolo gziolo Feb 17, 2021

Choose a reason for hiding this comment

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

Unless there is some way to let ESLint know that it should give itself a break when seeing import type when processing no-restricted-imports 🤔

Let's see: eslint/eslint#13758, typescript-eslint/typescript-eslint#2661.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should list React as peer dependency in that case? There are more packages that would require the same handling.

Copy link
Member

Choose a reason for hiding this comment

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

I reviewed all the changes again.

It won't scale with so many types required from React or Reakit 🙁

Should we at least disable this rule for all packages/**/types.ts files? Ideally, the rule shouldn't error for type imports in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should just disable those rules for packages/components... G2 imports a lot from reakit as well, outside of typescript files.

Copy link
Member

Choose a reason for hiding this comment

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

We could as well remove those checks for react and reakit if that becomes an issue. Although, my initial comment still applies. Technically speaking both those libraries are implementation details and shouldn't leak outside of @wordpress/element (react) and @wordpress/components (reakit). If there is no way to re-export the types that are used in other packages, then we shouldn't fight with the linter and disable the rule as a primary solution. We better remove the rule that becomes hostile.

packages/eslint-plugin/configs/recommended.js Outdated Show resolved Hide resolved
@sirreal sirreal force-pushed the update/lint-staged-ts branch from 4b55261 to b463f13 Compare February 17, 2021 09:50
@sarayourfriend
Copy link
Contributor

@sirreal There are a lot of unrelated linting errors in this PR right?

@gziolo
Copy link
Member

gziolo commented Feb 17, 2021

@sirreal There are a lot of unrelated linting errors in this PR right?

There are 47 errors reported and as far as I can tell they are valid issues with TS files.

@sarayourfriend
Copy link
Contributor

There are 47 errors reported and as far as I can tell they are valid issues with TS files.

Oh gotcha, I was mixing up errors and warnings! @sirreal I'm happy to tackle fixing the errors in the TS files.

@sarayourfriend
Copy link
Contributor

sarayourfriend commented Feb 17, 2021

Hmmm, it looks like there's something funky going on here that I'm not quite sure of.

Following the trail of errors from control-label/types we get an error about not being able to resolve ../text/types. If we go to that module, which exists at that correct path, we see that it has an error about not being able to resolve ../truncate/types. That module also exists at that path and itself has no errors. I'm not sure why eslint is unable to resolve these moduels 🤔

Also when I ran npm install on this branch I ended up with loads of package-lock.json changes.

Something further is even more wrong. For the following code in components/ui/utils/types:

export type Options< T extends As, P extends ViewOwnProps< {}, T > > = {
	as: T;
	name: string;
	useHook: ( props: P ) => any;
	memo?: boolean;
};

I am seeing the following error:

'props' is defined but never used. eslint(no-unused-vars)

However, props there is just a type annotation. Is something wrong with @typescript-eslint/parser?

@sarayourfriend
Copy link
Contributor

Why do you think that TS support for both @wordpress/eslint-plugin and @wordpress/scripts it's a breaking change rather than a new feature?

I'm not sure it is a breaking change, but I guess if someone had been using @wordpress/eslint-plugin and had already integrated TypeScript into their project, suddenly they'll start getting new errors to fix if they hadn't been linting TypeScript files. I think I'd classify that as a breaking change rather than just a new feature, at least if new features should be 'non-disruptive' this isn't that.

@gziolo
Copy link
Member

gziolo commented Feb 25, 2021

Yes, it might be an excellent reason to be cautious in case someone has integrated TS support themselves. That makes sense 👍🏻

In fact, we won't be able to publish a new version of both packages to npm until WordPress 5.7 is out which is on Match 9th, so we still might add more breaking changes to the mix 😄

@sirreal
Copy link
Member Author

sirreal commented Feb 25, 2021

My reasoning for the breaking change is that:

  • We're introducing new rules.
  • We're linting files that wouldn't be linted before (*.ts).

Either could mean that the upgrade requires some manual work to get this ready and is worth mentioning so folks no what to expect. Anything that may require work on the consumer's end to upgrade is my understanding of a breaking change.

I personally don't have enough expertise to tell that everything is perfect, but it is good enough for me to start using it with Gutenberg.

I certainly don't know whether this is perfect, but it's at a point where it can start getting exercise. If we're blocked on package releases can we next package releases? Maybe we can start getting feedback from other projects. @nerrad may have know some projects interested in using some of the TypeScript lint/prettier functionality.

@sirreal
Copy link
Member Author

sirreal commented Feb 25, 2021

I can't approve this because I opened it, although @sarayourfriend has done a lot of the work 🙇‍♂️ . I think it's ready to be merged and see how it performs.

@sarayourfriend sarayourfriend merged commit e7a1de3 into master Feb 25, 2021
@sarayourfriend sarayourfriend deleted the update/lint-staged-ts branch February 25, 2021 16:43
@gziolo
Copy link
Member

gziolo commented Feb 25, 2021

If we're blocked on package releases can we next package releases?

Sure thing, we can do it as soon as there is the next Gutenberg plugin RC ready 👍🏻

@github-actions github-actions bot added this to the Gutenberg 10.2 milestone Feb 25, 2021
@nerrad
Copy link
Contributor

nerrad commented Feb 25, 2021

I certainly don't know whether this is perfect, but it's at a point where it can start getting exercise. If we're blocked on package releases can we next package releases? Maybe we can start getting feedback from other projects. @nerrad may have know some projects interested in using some of the TypeScript lint/prettier functionality.

We're implementing TS in WooCommerce Blocks feature plugin and definitely could benefit from this update (which looks good afaict).

@gziolo gziolo changed the title Add .ts extension to lint-staged scripts Scripts: Add TypeScript support to linting command Mar 8, 2021
Chrico added a commit to Chrico/gutenberg that referenced this pull request Jun 27, 2024
…hecker is executed.

eslint/no-unused-vars got disabled when introducing support for TypeScript with WordPress#27143 (2-3 years ago) when Gutenberg was moving to Typescript. By disabling this feature, the Typescript type checker will not be executed. We actively enable "no-unused-vars" and "@typescript-eslint/no-unused-vars" and additionally setting "ignoreRestSiblings" to "true", which is actively used in Gutenberg.

See more information in: WordPress#54305
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tool] ESLint plugin /packages/eslint-plugin [Tool] WP Scripts /packages/scripts [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. [Type] Breaking Change For PRs that introduce a change that will break existing functionality [Type] Build Tooling Issues or PRs related to build tooling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants