diff --git a/src/printer.js b/src/printer.js index 9c005ebb9..99695f4b7 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2414,7 +2414,8 @@ function genericPrintNoParens(path, options, print, args) { // declare global { ... } const isGlobalDeclaration = n.name.type === "Identifier" && n.name.name === "global" && - n.modifiers.some(modifier => modifier.type === "TSDeclareKeyword"); + n.modifiers && + n.modifiers.some(modifier => modifier.type === "TSDeclareKeyword"); if (!isGlobalDeclaration) { parts.push(isExternalModule ? "module " : "namespace "); diff --git a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap index 1b16b7d9d..d6f6fdf1b 100644 --- a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap @@ -238,6 +238,42 @@ interface C { `; +exports[`globalIsContextualKeyword.ts 1`] = ` +function a() { + let global = 1; +} +function b() { + class global {} +} + +namespace global { +} + +function foo(global: number) { +} + +let obj = { + global: "123" +}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +function a() { + let global = 1; +} +function b() { + class global {} +} + +namespace global { + +} + +function foo(global: number) {} + +let obj = { + global: "123" +}; + +`; + exports[`indexSignatureWithInitializer.ts 1`] = ` // These used to be indexers, now they are computed properties interface I { diff --git a/tests/typescript/compiler/globalIsContextualKeyword.ts b/tests/typescript/compiler/globalIsContextualKeyword.ts new file mode 100644 index 000000000..ceae834a2 --- /dev/null +++ b/tests/typescript/compiler/globalIsContextualKeyword.ts @@ -0,0 +1,16 @@ +function a() { + let global = 1; +} +function b() { + class global {} +} + +namespace global { +} + +function foo(global: number) { +} + +let obj = { + global: "123" +} \ No newline at end of file