From 1a1e6fc1dd1b2bf5ed3db6ba357a9f9306ef072b Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 4 May 2017 11:20:52 -0700 Subject: [PATCH] Print empty member lookup (#1506) I have no idea if it's even valid but it threw on 8 typescript tests, now it doesn't. ```js TypeError: Cannot read property 'type' of null at printMemberLookup (prettier/src/printer.js:3062:16) ``` --- src/printer.js | 5 +++-- .../typescript/compiler/__snapshots__/jsfmt.spec.js.snap | 8 ++++++++ tests/typescript/compiler/badArrayIndex.ts | 1 + tests/typescript/compiler/jsfmt.spec.js | 1 + 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/typescript/compiler/badArrayIndex.ts create mode 100644 tests/typescript/compiler/jsfmt.spec.js diff --git a/src/printer.js b/src/printer.js index 1a7acc6e..cbbd1168 100644 --- a/src/printer.js +++ b/src/printer.js @@ -775,7 +775,7 @@ function genericPrintNoParens(path, options, print, args) { path.call(print, "name"), printTypeParameters(path, options, print, "typeParameters"), " " - ); + ); } if (n.heritageClauses) { prefix.push( @@ -2149,7 +2149,7 @@ function genericPrintNoParens(path, options, print, args) { if (n.type !== "TSCallSignature") { parts.push("new "); } - var isType = n.type === "TSConstructorType"; + var isType = n.type === "TSConstructorType"; parts.push( printTypeParameters(path, options, print, "typeParameters"), "(", @@ -3063,6 +3063,7 @@ function printMemberLookup(path, options, print) { } if ( + !n.property || (n.property.type === "Literal" && typeof n.property.value === "number") || n.property.type === "NumericLiteral" ) { diff --git a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..ad6a1e8b --- /dev/null +++ b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`badArrayIndex.ts 1`] = ` +var results = number[]; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +var results = number[]; + +`; diff --git a/tests/typescript/compiler/badArrayIndex.ts b/tests/typescript/compiler/badArrayIndex.ts new file mode 100644 index 00000000..110bf52e --- /dev/null +++ b/tests/typescript/compiler/badArrayIndex.ts @@ -0,0 +1 @@ +var results = number[]; diff --git a/tests/typescript/compiler/jsfmt.spec.js b/tests/typescript/compiler/jsfmt.spec.js new file mode 100644 index 00000000..bc085c48 --- /dev/null +++ b/tests/typescript/compiler/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" });