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

Add flag to allow access to UMD globals from modules #30776

Merged
merged 8 commits into from
Apr 19, 2019

Conversation

andrewbranch
Copy link
Member

Closes #10178.

A summary, since the issue discussion is quite long: the allowUmdGlobalAccess flag will allow accessing a global defined in a UMD module from anywhere, including from modules. This could potentially be unsafe since not all UMD modules define a global in the presence of a module system, but under some circumstances you really might need the ability. E.g. in a browser environment where one UMD module loads before require.js, that module will only be available as a global. So, this flag is an escape hatch for users stuck in scenarios like that.

@andrewbranch andrewbranch added the Add a Flag Any problem can be solved by flags, except for the problem of having too many flags label Apr 5, 2019
src/compiler/commandLineParser.ts Outdated Show resolved Hide resolved
src/compiler/checker.ts Outdated Show resolved Hide resolved
@RyanCavanaugh
Copy link
Member

Need to revert submodule updates, otherwise LGTM

@weswigham
Copy link
Member

weswigham commented Apr 6, 2019

This flag quite literally just suppresses the error since we already "do the right thing" in the presence of the error with respect to types. Should we not just concede to community demand and add a generalized "suppressions": ["TS2686"]-type compiler option? Because that's all this new command is an alias for. (And I imagine if we do the more generalized option we can be smart and still return the errors as suggestions, which we do not (yet?) do here)

@andrewbranch
Copy link
Member Author

@weswigham good point. I'm sure I'm missing some prior discussion here, but aren't there some error codes that are virtually unsuppressible, or would have horrifying consequences if suppressed? This particular error is a great example of something that would work, but I'm thinking about e.g. parser errors... suppressing a diagnostic from parseExpectedToken would essentially allow people to invent their own more concise non-standard syntax; in other cases, invalid syntax could parse as a totally different syntax tree than a user is expecting, without surfacing any diagnostics.

I guess a better summary of what I'm asking is—while I don't personally feel the need to try to force users into writing better TypeScript against their will, would a feature like that, on average, actually result in a better user experience? Or would it inadvertently wreak more havoc than solve problems?

As a newbie, this is a legitimate question, not rhetorical, and I'd happily defer to the team's consensus.

@weswigham
Copy link
Member

or would have horrifying consequences if suppressed?

We already have // @ts-ignore comments that let you suppress anything, sooooooo

@ajafff
Copy link
Contributor

ajafff commented Apr 6, 2019

We already have // @ts-ignore comments that let you suppress anything

IIRC that's not the case. It doesn't suppress parse errors and declaration emit diagnostics (don't know about binder diagnostics)

@weswigham
Copy link
Member

It doesn't suppress parse errors and declaration emit diagnostics (don't know about binder diagnostics)

We can exclude those from the errors we'll suppress, too, then. I'm just thinking that adding a suppressions argument that acts like a ts-ignore for all errors of a specific type is probably more general, and I know I've seen requests for it (much as I personally loathe the idea of suppressing any of our errors).

@DanielRosenwasser DanielRosenwasser removed the Add a Flag Any problem can be solved by flags, except for the problem of having too many flags label Apr 8, 2019
@@ -1635,7 +1635,7 @@ namespace ts {
if (result && isInExternalModule && (meaning & SymbolFlags.Value) === SymbolFlags.Value && !(originalLocation!.flags & NodeFlags.JSDoc)) {
const merged = getMergedSymbol(result);
if (length(merged.declarations) && every(merged.declarations, d => isNamespaceExportDeclaration(d) || isSourceFile(d) && !!d.symbol.globalExports)) {
error(errorLocation!, Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, unescapeLeadingUnderscores(name)); // TODO: GH#18217
errorOrSuggestion(!compilerOptions.allowUmdGlobalAccess, errorLocation!, Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, unescapeLeadingUnderscores(name));
Copy link
Member

Choose a reason for hiding this comment

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

Should this diagnostic change to additionally mention or enable allowUmdGlobalAccess if?

Copy link
Member Author

Choose a reason for hiding this comment

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

Since this is errorOrSuggestion based on the compiler option, we would have to use two different messages depending on the value of the option (since users with it enabled should not see a message that says "Enable the 'allowUmdGlobalAccess' compiler option")... do you think that's worth it?

Copy link
Member

Choose a reason for hiding this comment

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

I'd leave as-is. In general for errors we think are usually correct, we try not to mention commandline flags to disable the error, since people will often try disabling the error before even checking if their code works or not

@andrewbranch andrewbranch merged commit b6a0988 into microsoft:master Apr 19, 2019
@andrewbranch andrewbranch deleted the feature/10178 branch April 19, 2019 01:05
export as namespace Foo;

// @filename: a.ts
/// <reference path="foo.d.ts" />
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a requirement in this file?

Given the original issue #1078 is sooo convoluted, it may be well worth a bit of write-up how this new feature works.

P.S. I have a feeling it may be extremely useful in some scenarios I am involved, but need a more defined behaviour/usage/spec to know for sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, the original issue is a lot to absorb, but this “feature” is actually quite simple. All it does is suppress a particular error message. This one:

image

The triple slash reference isn't related to this feature at all; it's related to the @noImplicitReferences option which is just a configuration for our test harness.

Does that clear things up, or do you have any additional specific questions?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, all clear.

It's awesome: from complex to trivial in a couple sentences.

Copy link

@tqma113 tqma113 left a comment

Choose a reason for hiding this comment

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

LGTM, That's great.

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.

Consider allowing access to UMD globals from modules
8 participants