Skip to content

moduleAugmentations may contain an Identifier#18009

Merged
3 commits merged into
masterfrom
declare_global
Aug 25, 2017
Merged

moduleAugmentations may contain an Identifier#18009
3 commits merged into
masterfrom
declare_global

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Aug 24, 2017

Fixes #17347

@ghost ghost requested a review from amcasey August 25, 2017 16:09
Comment thread src/compiler/program.ts
function getTextOfLiteral(literal: LiteralExpression): string {
return literal.text;
function moduleNameIsEqualTo(a: StringLiteral | Identifier, b: StringLiteral | Identifier): boolean {
return a.kind === SyntaxKind.StringLiteral
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just to confirm, the string literal "global" is not equal to the identifier global?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

export {}; // this is a module

// Augments a module named "global"
declare module "global" {
    export const x: number;
}

// Augments the global scope
declare global {
    const y: number;
}

// Module access: "y" fails
import { x as _x, y as _y } from "global";

// Global access: "x" fails
x; y;

Comment thread src/compiler/program.ts
// file.imports may not be undefined if there exists dynamic import
let imports: StringLiteral[];
let moduleAugmentations: StringLiteral[];
let moduleAugmentations: Array<StringLiteral | Identifier>;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For my own edification, what's the difference between T[] and Array<T>?

Copy link
Copy Markdown
Author

@ghost ghost Aug 25, 2017

Choose a reason for hiding this comment

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

No semantic difference. I prefer to use Array<> when wrapping unions to avoid needing parentheses. (Array<A | B> instead of (A | B)[]). (See array-type (the "array-simple" option), which maybe we should start using.)

Comment thread src/compiler/program.ts Outdated
// Because global augmentation doesn't have string literal name, we can check for global augmentation as such.
const nonGlobalAugmentation = filter(file.moduleAugmentations, (moduleAugmentation) => moduleAugmentation.kind === SyntaxKind.StringLiteral);
const moduleNames = map(concatenate(file.imports, nonGlobalAugmentation), getTextOfLiteral);
const moduleNames = getModuleImportAndAugmentationNames(file);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should the name of this local indicate that it contains augmentations? (It may be implied - I don't know what an augmentation is.)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

A module augmentation adds new members to an external module. In order to do this we need to resolve that module, so we're considering both imports and augmentations together on purpose, since they both need to be resolved.
Handbook ref: http://www.typescriptlang.org/docs/handbook/declaration-merging.html

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It just seems strange that we say "ImportAndAugmentationNames" in the function name but just "moduleNames" in the local name.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

How about just calling the function getModuleNames then?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If they're consistent, I'm happy. :)

Comment thread src/compiler/program.ts
function getModuleImportAndAugmentationNames({ imports, moduleAugmentations }: SourceFile): string[] {
const res = imports.map(i => i.text);
for (const aug of moduleAugmentations) {
if (aug.kind === SyntaxKind.StringLiteral) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about the augmentations that are identifiers?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

We're interested in the names of modules that have to be resolved; declare global doesn't require any module resolution.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unless that's obvious to someone more experience, please consider adding a comment to that effect.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

@ghost ghost force-pushed the declare_global branch from f16f53d to 130127d Compare August 25, 2017 17:55
Copy link
Copy Markdown
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

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

Seems reasonable, but I don't have a deep understanding.

@ghost ghost merged commit 4d05bfd into master Aug 25, 2017
@ghost ghost deleted the declare_global branch August 25, 2017 22:14
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants