Skip to content

Preserve const enums should keep import refs #28498

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

Merged

Conversation

weswigham
Copy link
Member

For exported const enums exported via export default.

Fixes #23514

markExportAsReferenced fully resolves aliases and so doesn't mark the import when the symbol is brought in from another file like in the given example. export {...} doesn't worry about this because for an export {...}, it is an error to export something not declared within the current file (it exports bindings, not expressions). export default <expr>, however, allows exporting arbitrary expressions - so the symbol associated with the expression likely won't be the same as the alias associated with the export assignment itself.

Meanwhile, markAliasReferenced also didn't consider aliases of const enum-y namespaces as references for preserving imports while in preserveConstEnums mode.

@@ -16014,7 +16014,7 @@ namespace ts {
}

function markAliasReferenced(symbol: Symbol, location: Node) {
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value) && !isInTypeQuery(location) && !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) {
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value) && !isInTypeQuery(location) && (compilerOptions.preserveConstEnums || !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol)))) {
Copy link
Member

@sheetalkamat sheetalkamat Nov 13, 2018

Choose a reason for hiding this comment

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

Don't you need similar change in markExportAsReferenced ? Also I think we should remove the isConstEnumOrConstEnumOnlyModule check or update it before calling markAliasSymbolAsReferenced in checkJsxOpeningLikeElementOrOpeningFragment, checkPropertyAccessExpressionOrQualifiedName, markEntityNameOrEntityExpressionAsReference,

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't you need similar change in markExportAsReferenced ?

Actually, I don't need to call markExportAsReferenced anymore. The markAliasReferenced is more correct for an export assignment (as it mimics what checkIdentifier would do). markExportAsReferenced looks at the wrong symbol for an export assignment (it looks at the default export symbol rather than the symbol of the identifier in the expression), so it hasn't been doing any meaningful work anyway. I'll remove its handling of them. (And you know what? The checking functions built into markAliasSymbolAsReferenced are strange - I'll try to move those so they're local to what they check, too)

Also I think we should remove the isConstEnumOrConstEnumOnlyModule check or update it before calling markAliasSymbolAsReferenced in checkJsxOpeningLikeElementOrOpeningFragment,

I don't know why we check const-ness here, actually. I'm pretty sure we just need to always consider it referenced, const or no (since we need to preserve the import because we'll be referencing it for the factory function in the emit).

checkPropertyAccessExpressionOrQualifiedName

I don't think so - the check there is just checking prop to see if it's even possible for parentSymbol to be const - a fast path. It calls into markAliasReferenced which does the full check.

markEntityNameOrEntityExpressionAsReference

No need - this is only used for decorator metadata, which I think we'd rather have been the same for preserve const enums/non preserved (and const enums are just going to be mapped to Number and their namespaces Object anyway - unlike other types which may have necessitated keeping imports for classes of some sort; so we don't want their imports kept in preserve mode, since we won't use them!).

@MLoughry
Copy link

@weswigham What's the likelihood this makes it into TS 3.5? This bug is preventing us from upgrading to more recent versions of webpack, which must have added validation that errors on conditions (such as this) where a file exports a symbol that's not defined.

@weswigham
Copy link
Member Author

weswigham commented May 13, 2019

preventing us from upgrading to more recent versions of webpack

??? How so? afaik webpack doesn't use any const enums... (and if it somehow did, they shouldn't be in their public API, as putting const enums in a public API is bad for compatibility)?

@MLoughry
Copy link

MLoughry commented May 13, 2019 via email

@weswigham
Copy link
Member Author

The emitted JS

In webpack? They compile as TS now? Or in your project that consumes webpack's definition files?

@MLoughry
Copy link

No, the JS that Typescript emits from transpiling our project, which we then pass off to webpack to bundle.

// types.ts
export const enum Foo {
   Bar = 0,
   Baz = 1
};

export interface ViewState {
  foo: Foo;
}


// code.ts
import { Foo, ViewState } from './types';

export {Foo};

export const SomeViewState = {
  foo: Foo.Bar,
};

// EMITTED code.js
export { Foo };
export const SomeViewState = {
  foo: 0 /* Bar */,
}

Then, when that emitted code.js is passed to webpack for bundling, it throws an error saying that it could not find Foo to export in code.js

@weswigham
Copy link
Member Author

Ahhhhhh ok, so not related to webpack at all really (well, webpack tosses the error since it does dependency analysis but it'd throw at runtime, too), got it. I was just worried that webpack had somehow adopted const enums, despite being a js project :P

@MLoughry
Copy link

Yeah. The strange thing is that webpack doesn't error on this on versions <=4.28.3, but it does for >=4.30.0 (I didn't bother narrowing down in-between).

@weswigham weswigham force-pushed the preserve-const-export-assignment branch from 1924079 to 83893b3 Compare May 24, 2019 20:33
@weswigham
Copy link
Member Author

@typescript-bot test this
@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 24, 2019

Heya @weswigham, I've started to run the extended test suite on this PR at 83893b3. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 24, 2019

Heya @weswigham, I've started to run the parallelized Definitely Typed test suite on this PR at 83893b3. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham
Copy link
Member Author

@typescript-bot test this
@typescript-bot run dt

@weswigham weswigham force-pushed the preserve-const-export-assignment branch from 83893b3 to 27bbfdb Compare June 11, 2019 20:43
@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 11, 2019

Heya @weswigham, I've started to run the parallelized Definitely Typed test suite on this PR at 27bbfdb. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 11, 2019

Heya @weswigham, I've started to run the extended test suite on this PR at 27bbfdb. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham weswigham force-pushed the preserve-const-export-assignment branch from 27bbfdb to 219fbde Compare August 1, 2019 22:07
@weswigham
Copy link
Member Author

@typescript-bot test this
@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 1, 2019

Heya @weswigham, I've started to run the parallelized Definitely Typed test suite on this PR at 219fbde. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 1, 2019

Heya @weswigham, I've started to run the extended test suite on this PR at 219fbde. You can monitor the build here. It should now contribute to this PR's status checks.

for exported const enums exported via export default

Move some functionality around, small cleanup

Remove unneeded const enum check
@weswigham weswigham force-pushed the preserve-const-export-assignment branch from 219fbde to 9a9a2df Compare August 1, 2019 22:21
@weswigham
Copy link
Member Author

@typescript-bot test this
@typescript-bot run dt
@typescript-bot user test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 1, 2019

Heya @weswigham, I've started to run the parallelized Definitely Typed test suite on this PR at 9a9a2df. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 1, 2019

Heya @weswigham, I've started to run the parallelized community code test suite on this PR at 9a9a2df. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 1, 2019

Heya @weswigham, I've started to run the extended test suite on this PR at 9a9a2df. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham
Copy link
Member Author

@typescript-bot test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 1, 2019

Heya @weswigham, I've started to run the extended test suite on this PR at b229333. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham
Copy link
Member Author

reping @sheetalkamat and @sandersn for a review~

@weswigham weswigham merged commit 3b54ffc into microsoft:master Aug 5, 2019
@weswigham weswigham deleted the preserve-const-export-assignment branch August 5, 2019 23:47
@sandersn
Copy link
Member

sandersn commented Aug 6, 2019

Looks like this breaks dojo types. I'll file a bug for followup.

@weswigham
Copy link
Member Author

@sandersn #32747 👍

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.

Generated code when re-exporting a const enum using "preserveConstEnums": true is inconsistent
5 participants