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

Elide type-only exports when the TS transform is enabled #433

Merged
merged 1 commit into from Mar 11, 2019

Conversation

alangpierce
Copy link
Owner

Fixes #398

This ended up having a lot of cases and complexity. We actually care about the
known type declarations and the known value declarations.

  • In ESM mode, only elide exports that are known to be a type and not known to
    be a value. This is true both for named export syntax and default export.
  • In CJS mode, elide named exports that are either completely unknown or known
    to be a type. For default exports, it's the same logic as ESM: we only elide
    if we know it as a type and not as a value.

This required going through all declarations (at least in TS parsing mode) and
making sure we add the right identifier info to each of them, which in turn had
consequences on the react-hot-loader transform, so that had to be fixed.

One challenge is that default exports should be elided only when they export a
single identifier, which we can't know statically. Instead, we now use the
rhsEndIndex field on the export token to know when the export statement
actually ends, and use that to determine if the export is just an identifier.

Fixes #398

This ended up having a *lot* of cases and complexity. We actually care about the
known type declarations and the known value declarations.
* In ESM mode, only elide exports that are known to be a type and not known to
  be a value. This is true both for named export syntax and default export.
* In CJS mode, elide named exports that are either completely unknown or known
  to be a type. For default exports, it's the same logic as ESM: we only elide
  if we know it as a type and not as a value.

This required going through all declarations (at least in TS parsing mode) and
making sure we add the right identifier info to each of them, which in turn had
consequences on the react-hot-loader transform, so that had to be fixed.

One challenge is that default exports should be elided only when they export a
single identifier, which we can't know statically. Instead, we now use the
`rhsEndIndex` field on the `export` token to know when the export statement
actually ends, and use that to determine if the export is just an identifier.
@codecov-io
Copy link

Codecov Report

Merging #433 into master will increase coverage by 0.21%.
The diff coverage is 88.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #433      +/-   ##
==========================================
+ Coverage   80.22%   80.43%   +0.21%     
==========================================
  Files          47       49       +2     
  Lines        5041     5122      +81     
  Branches     1175     1199      +24     
==========================================
+ Hits         4044     4120      +76     
- Misses        696      700       +4     
- Partials      301      302       +1
Impacted Files Coverage Δ
src/transformers/RootTransformer.ts 92.48% <ø> (ø) ⬆️
src/transformers/CJSImportTransformer.ts 87.83% <100%> (+0.33%) ⬆️
src/transformers/ReactHotLoaderTransformer.ts 100% <100%> (ø) ⬆️
src/parser/traverser/lval.ts 97.01% <100%> (ø) ⬆️
src/parser/tokenizer/index.ts 74.29% <100%> (+0.36%) ⬆️
src/parser/traverser/statement.ts 82.12% <100%> (+0.5%) ⬆️
src/util/shouldElideDefaultExport.ts 75% <75%> (ø)
src/util/getDeclarationInfo.ts 80% <80%> (ø)
src/parser/plugins/typescript.ts 75.1% <87.5%> (+0.07%) ⬆️
src/transformers/ESMImportTransformer.ts 82.75% <88.23%> (+3.29%) ⬆️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9eba7fb...cdbee61. Read the comment docs.

@alangpierce alangpierce merged commit bc6e361 into master Mar 11, 2019
@alangpierce alangpierce deleted the elide-type-exports branch March 11, 2019 07:16
alangpierce added a commit that referenced this pull request Dec 1, 2020
Fixes #563
Fixes #473

The type-only export elision implemented in #433 had a false positive for
destructure delcarations, since `getDeclarationInfo` only looks at top-level
declarations and top-level expressions like `const {x} = ...` weren't being
marked correctly. To fix, we can mark object shorthand declarations as top-level
when applicable. I also refactored both relevant code paths to avoid the
repeated assignment left-hand side.
alangpierce added a commit that referenced this pull request Dec 2, 2020
Fixes #563
Fixes #473

The type-only export elision implemented in #433 had a false positive for
destructure delcarations, since `getDeclarationInfo` only looks at top-level
declarations and top-level expressions like `const {x} = ...` weren't being
marked correctly. To fix, we can mark object shorthand declarations as top-level
when applicable. I also refactored both relevant code paths to avoid the
repeated assignment left-hand side.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

exporting a type outside of its declaration incorrectly outputs an export
2 participants