Skip to content

Parse reserved identifiers as dotted names of ambient modules #55282

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8229,7 +8229,7 @@ namespace Parser {
// If we are parsing a dotted namespace name, we want to
// propagate the 'Namespace' flag across the names if set.
const namespaceFlag = flags & NodeFlags.Namespace;
const name = parseIdentifier();
const name = flags & NodeFlags.NestedNamespace ? parseIdentifierName() : parseIdentifier();
const body = parseOptional(SyntaxKind.DotToken)
? parseModuleOrNamespaceDeclaration(getNodePos(), /*hasJSDoc*/ false, /*modifiers*/ undefined, NodeFlags.NestedNamespace | namespaceFlag) as NamespaceDeclaration
: parseModuleBlock();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts(11,1): error TS2304: Cannot find name 'declare'.
ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts(11,9): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts(11,16): error TS2819: Namespace name cannot be 'debugger'.
ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts(11,25): error TS1005: ';' expected.


==== ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts (4 errors) ====
// https://github.com/microsoft/TypeScript/issues/7840

declare module chrome.debugger {
Copy link
Member

Choose a reason for hiding this comment

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

we haven't (yet) deprecated the module keyword, but please use declare namespace for new tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While the syntax is supported - shouldn't I add extra tests with declare namespace instead of changing this one?

declare var tabId: number;
}

export const tabId = chrome.debugger.tabId;

declare module test.class {}

declare module debugger {} // still an error
~~~~~~~
!!! error TS2304: Cannot find name 'declare'.
~~~~~~
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
~~~~~~~~
!!! error TS2819: Namespace name cannot be 'debugger'.
~
!!! error TS1005: ';' expected.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts] ////

//// [ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts]
// https://github.com/microsoft/TypeScript/issues/7840

declare module chrome.debugger {
declare var tabId: number;
}

export const tabId = chrome.debugger.tabId;

declare module test.class {}

declare module debugger {} // still an error


//// [ambientModuleDeclarationWithReservedIdentifierInDottedPath.js]
"use strict";
// https://github.com/microsoft/TypeScript/issues/7840
Object.defineProperty(exports, "__esModule", { value: true });
exports.tabId = void 0;
exports.tabId = chrome.debugger.tabId;
declare;
module;
debugger;
{ } // still an error


//// [ambientModuleDeclarationWithReservedIdentifierInDottedPath.d.ts]
export declare const tabId: number;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts] ////

=== ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts ===
// https://github.com/microsoft/TypeScript/issues/7840

declare module chrome.debugger {
>chrome : Symbol(chrome, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 0, 0))
>debugger : Symbol(debugger, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 2, 22))

declare var tabId: number;
>tabId : Symbol(tabId, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 3, 15))
}

export const tabId = chrome.debugger.tabId;
>tabId : Symbol(tabId, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 6, 12))
>chrome.debugger.tabId : Symbol(chrome.debugger.tabId, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 3, 15))
>chrome.debugger : Symbol(chrome.debugger, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 2, 22))
>chrome : Symbol(chrome, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 0, 0))
>debugger : Symbol(chrome.debugger, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 2, 22))
>tabId : Symbol(chrome.debugger.tabId, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 3, 15))

declare module test.class {}
>test : Symbol(test, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 6, 43))
>class : Symbol(class, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts, 8, 20))

declare module debugger {} // still an error

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts] ////

=== ambientModuleDeclarationWithReservedIdentifierInDottedPath.ts ===
// https://github.com/microsoft/TypeScript/issues/7840

declare module chrome.debugger {
>chrome : typeof chrome
>debugger : typeof debugger

declare var tabId: number;
>tabId : number
}

export const tabId = chrome.debugger.tabId;
>tabId : number
>chrome.debugger.tabId : number
>chrome.debugger : typeof chrome.debugger
>chrome : typeof chrome
>debugger : typeof chrome.debugger
>tabId : number

declare module test.class {}

declare module debugger {} // still an error
>declare : any
>module : any

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts(9,1): error TS2304: Cannot find name 'declare'.
ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts(9,9): error TS2304: Cannot find name 'namespace'.
ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts(9,19): error TS2819: Namespace name cannot be 'debugger'.
ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts(9,28): error TS1005: ';' expected.


==== ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts (4 errors) ====
declare namespace chrome.debugger {
declare var tabId: number;
}

export const tabId = chrome.debugger.tabId;

declare namespace test.class {}

declare namespace debugger {} // still an error
~~~~~~~
!!! error TS2304: Cannot find name 'declare'.
~~~~~~~~~
!!! error TS2304: Cannot find name 'namespace'.
~~~~~~~~
!!! error TS2819: Namespace name cannot be 'debugger'.
~
!!! error TS1005: ';' expected.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts] ////

//// [ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts]
declare namespace chrome.debugger {
declare var tabId: number;
}

export const tabId = chrome.debugger.tabId;

declare namespace test.class {}

declare namespace debugger {} // still an error


//// [ambientModuleDeclarationWithReservedIdentifierInDottedPath2.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tabId = void 0;
exports.tabId = chrome.debugger.tabId;
declare;
namespace;
debugger;
{ } // still an error


//// [ambientModuleDeclarationWithReservedIdentifierInDottedPath2.d.ts]
export declare const tabId: number;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts] ////

=== ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts ===
declare namespace chrome.debugger {
>chrome : Symbol(chrome, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 0, 0))
>debugger : Symbol(debugger, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 0, 25))

declare var tabId: number;
>tabId : Symbol(tabId, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 1, 15))
}

export const tabId = chrome.debugger.tabId;
>tabId : Symbol(tabId, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 4, 12))
>chrome.debugger.tabId : Symbol(chrome.debugger.tabId, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 1, 15))
>chrome.debugger : Symbol(chrome.debugger, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 0, 25))
>chrome : Symbol(chrome, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 0, 0))
>debugger : Symbol(chrome.debugger, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 0, 25))
>tabId : Symbol(chrome.debugger.tabId, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 1, 15))

declare namespace test.class {}
>test : Symbol(test, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 4, 43))
>class : Symbol(class, Decl(ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts, 6, 23))

declare namespace debugger {} // still an error

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/conformance/ambient/ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts] ////

=== ambientModuleDeclarationWithReservedIdentifierInDottedPath2.ts ===
declare namespace chrome.debugger {
>chrome : typeof chrome
>debugger : typeof debugger

declare var tabId: number;
>tabId : number
}

export const tabId = chrome.debugger.tabId;
>tabId : number
>chrome.debugger.tabId : number
>chrome.debugger : typeof chrome.debugger
>chrome : typeof chrome
>debugger : typeof chrome.debugger
>tabId : number

declare namespace test.class {}

declare namespace debugger {} // still an error
>declare : any
>namespace : any

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @declaration: true

// https://github.com/microsoft/TypeScript/issues/7840

declare module chrome.debugger {
declare var tabId: number;
}

export const tabId = chrome.debugger.tabId;

declare module test.class {}

declare module debugger {} // still an error
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @declaration: true

declare namespace chrome.debugger {
declare var tabId: number;
}

export const tabId = chrome.debugger.tabId;

declare namespace test.class {}

declare namespace debugger {} // still an error