mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Preserve newlines between try/catch/finally, if/else, do/while (#39280)
* Preserve newlines between try/catch, if/else, do/while * Update baselines
This commit is contained in:
1 parent
4601a786aa
commit
3d22a4fd72
50 files changed
+124
-109
No files matched your search
+16
-7
@@ -2667,7 +2667,7 @@ namespace ts {
|
||||
emitTokenWithComment(SyntaxKind.CloseParenToken, node.expression.end, writePunctuation, node);
|
||||
emitEmbeddedStatement(node, node.thenStatement);
|
||||
if (node.elseStatement) {
|
||||
writeLineOrSpace(node);
|
||||
writeLineOrSpace(node, node.thenStatement, node.elseStatement);
|
||||
emitTokenWithComment(SyntaxKind.ElseKeyword, node.thenStatement.end, writeKeyword, node);
|
||||
if (node.elseStatement.kind === SyntaxKind.IfStatement) {
|
||||
writeSpace();
|
||||
@@ -2690,11 +2690,11 @@ namespace ts {
|
||||
function emitDoStatement(node: DoStatement) {
|
||||
emitTokenWithComment(SyntaxKind.DoKeyword, node.pos, writeKeyword, node);
|
||||
emitEmbeddedStatement(node, node.statement);
|
||||
if (isBlock(node.statement)) {
|
||||
if (isBlock(node.statement) && !preserveSourceNewlines) {
|
||||
writeSpace();
|
||||
}
|
||||
else {
|
||||
writeLineOrSpace(node);
|
||||
writeLineOrSpace(node, node.statement, node.expression);
|
||||
}
|
||||
|
||||
emitWhileClause(node, node.statement.end);
|
||||
@@ -2836,11 +2836,11 @@ namespace ts {
|
||||
writeSpace();
|
||||
emit(node.tryBlock);
|
||||
if (node.catchClause) {
|
||||
writeLineOrSpace(node);
|
||||
writeLineOrSpace(node, node.tryBlock, node.catchClause);
|
||||
emit(node.catchClause);
|
||||
}
|
||||
if (node.finallyBlock) {
|
||||
writeLineOrSpace(node);
|
||||
writeLineOrSpace(node, node.catchClause || node.tryBlock, node.finallyBlock);
|
||||
emitTokenWithComment(SyntaxKind.FinallyKeyword, (node.catchClause || node.tryBlock).end, writeKeyword, node);
|
||||
writeSpace();
|
||||
emit(node.finallyBlock);
|
||||
@@ -4249,10 +4249,19 @@ namespace ts {
|
||||
return pos! < 0 ? pos! : pos! + tokenString.length;
|
||||
}
|
||||
|
||||
function writeLineOrSpace(node: Node) {
|
||||
if (getEmitFlags(node) & EmitFlags.SingleLine) {
|
||||
function writeLineOrSpace(parentNode: Node, prevChildNode: Node, nextChildNode: Node) {
|
||||
if (getEmitFlags(parentNode) & EmitFlags.SingleLine) {
|
||||
writeSpace();
|
||||
}
|
||||
else if (preserveSourceNewlines) {
|
||||
const lines = getLinesBetweenNodes(parentNode, prevChildNode, nextChildNode);
|
||||
if (lines) {
|
||||
writeLine(lines);
|
||||
}
|
||||
else {
|
||||
writeSpace();
|
||||
}
|
||||
}
|
||||
else {
|
||||
writeLine();
|
||||
}
|
||||
|
||||
@@ -9,8 +9,7 @@ async function f():Promise<void> {
|
||||
try {
|
||||
const result = await fetch('https://typescriptlang.org');
|
||||
console.log(result);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
+2
-4
@@ -10,12 +10,10 @@ async function f():Promise<void> {
|
||||
try {
|
||||
const result = await fetch('https://typescriptlang.org');
|
||||
console.log(result);
|
||||
}
|
||||
catch (rejection) {
|
||||
} catch (rejection) {
|
||||
console.log("rejected:", rejection);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
+2
-4
@@ -19,12 +19,10 @@ async function f():Promise<void> {
|
||||
try {
|
||||
const result = await fetch('https://typescriptlang.org');
|
||||
return res(result);
|
||||
}
|
||||
catch (rejection) {
|
||||
} catch (rejection) {
|
||||
return rej(rejection);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return catch_err(err);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -15,8 +15,7 @@ async function f(){
|
||||
try {
|
||||
const res = await fetch("https://typescriptlang.org");
|
||||
result = 0;
|
||||
}
|
||||
catch (rej) {
|
||||
} catch (rej) {
|
||||
result = 1;
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -15,8 +15,7 @@ async function f(){
|
||||
try {
|
||||
const res = await fetch("https://typescriptlang.org");
|
||||
result = 0;
|
||||
}
|
||||
catch (rej) {
|
||||
} catch (rej) {
|
||||
result = 1;
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -15,8 +15,7 @@ async function f(){
|
||||
try {
|
||||
const res = await fetch("https://typescriptlang.org");
|
||||
result = 0;
|
||||
}
|
||||
catch (rej) {
|
||||
} catch (rej) {
|
||||
result = 1;
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -39,8 +39,7 @@ async function f(){
|
||||
try {
|
||||
const result_1 = await fetch("https://typescriptlang.org");
|
||||
result = await res(result_1);
|
||||
}
|
||||
catch (reject) {
|
||||
} catch (reject) {
|
||||
result = await rej(reject);
|
||||
}
|
||||
return res(result);
|
||||
|
||||
+1
-2
@@ -9,8 +9,7 @@ async function f():Promise<void> {
|
||||
try {
|
||||
const result = await fetch('https://typescriptlang.org');
|
||||
return console.log(result);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,7 @@ async function f():Promise<void> {
|
||||
try {
|
||||
const result = await fetch('https://typescriptlang.org');
|
||||
return res(result);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return catch_err(err);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -16,8 +16,7 @@ async function innerPromise(): Promise<string> {
|
||||
try {
|
||||
const blob = await resp.blob();
|
||||
blob_1 = blob.byteOffset;
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
blob_1 = 'Error';
|
||||
}
|
||||
return blob_1.toString();
|
||||
|
||||
+1
-2
@@ -16,8 +16,7 @@ async function innerPromise(): Promise<string> {
|
||||
try {
|
||||
const { blob } = await resp.blob();
|
||||
blob_1 = blob.byteOffset;
|
||||
}
|
||||
catch ({ message }) {
|
||||
} catch ({ message }) {
|
||||
blob_1 = 'Error ' + message;
|
||||
}
|
||||
return blob_1.toString();
|
||||
|
||||
+1
-2
@@ -16,8 +16,7 @@ async function innerPromise(): Promise<string> {
|
||||
try {
|
||||
const blob = await resp.blob();
|
||||
result = blob.byteOffset;
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
result = 'Error';
|
||||
}
|
||||
const { x } = result;
|
||||
|
||||
+1
-2
@@ -16,8 +16,7 @@ async function innerPromise(): Promise<string> {
|
||||
try {
|
||||
const { blob } = await resp.blob();
|
||||
result = blob.byteOffset;
|
||||
}
|
||||
catch ({ message }) {
|
||||
} catch ({ message }) {
|
||||
result = 'Error ' + message;
|
||||
}
|
||||
const [x, y] = result;
|
||||
|
||||
+1
-2
@@ -16,8 +16,7 @@ async function innerPromise(): Promise<string> {
|
||||
try {
|
||||
const { blob } = await resp.blob();
|
||||
result = [0, blob.byteOffset];
|
||||
}
|
||||
catch ({ message }) {
|
||||
} catch ({ message }) {
|
||||
result = ['Error ', message];
|
||||
}
|
||||
const [x, y] = result;
|
||||
|
||||
+1
-2
@@ -12,8 +12,7 @@ async function f() {
|
||||
let x = fetch("https://typescriptlang.org").then(res => console.log(res));
|
||||
try {
|
||||
return x;
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return console.log("Error!", err);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -12,8 +12,7 @@ async function f() {
|
||||
let x = fetch("https://typescriptlang.org").then(res => console.log(res));
|
||||
try {
|
||||
return x;
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return console.log("Error!", err);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-4
@@ -10,12 +10,10 @@ async function f(): Promise<void> {
|
||||
try {
|
||||
const res = await fetch('https://typescriptlang.org');
|
||||
return console.log(res);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return console.log("err", err);
|
||||
}
|
||||
}
|
||||
catch (err2) {
|
||||
} catch (err2) {
|
||||
return console.log("err2", err2);
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -10,6 +10,5 @@ async function f() {
|
||||
try {
|
||||
const x = await fetch('https://typescriptlang.org');
|
||||
return x.statusText;
|
||||
}
|
||||
catch (e) { }
|
||||
} catch (e) { }
|
||||
}
|
||||
+1
-2
@@ -10,6 +10,5 @@ async function f() {
|
||||
try {
|
||||
const x = await fetch('https://typescriptlang.org');
|
||||
return x.statusText;
|
||||
}
|
||||
catch (e) { }
|
||||
} catch (e) { }
|
||||
}
|
||||
@@ -9,8 +9,7 @@ function /*[#|*/f/*|]*/():Promise<void | Response> {
|
||||
async function f():Promise<void | Response> {
|
||||
try {
|
||||
await fetch('https://typescriptlang.org');
|
||||
}
|
||||
catch (rejection) {
|
||||
} catch (rejection) {
|
||||
return console.log("rejected:", rejection);
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,7 @@ function /*[#|*/f/*|]*/():Promise<void | Response> {
|
||||
async function f():Promise<void | Response> {
|
||||
try {
|
||||
await fetch('https://typescriptlang.org');
|
||||
}
|
||||
catch (rej) {
|
||||
} catch (rej) {
|
||||
return console.log(rej);
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,7 @@ function /*[#|*/f/*|]*/():Promise<void | Response> {
|
||||
async function f():Promise<void | Response> {
|
||||
try {
|
||||
return fetch('https://typescriptlang.org');
|
||||
}
|
||||
catch (rej) {
|
||||
} catch (rej) {
|
||||
return console.log(rej);
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,7 @@ function /*[#|*/f/*|]*/() {
|
||||
async function f() {
|
||||
try {
|
||||
await fetch('https://typescriptlang.org');
|
||||
}
|
||||
catch (rejection) {
|
||||
} catch (rejection) {
|
||||
return console.log("rejected:", rejection);
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,7 @@ function /*[#|*/f/*|]*/() {
|
||||
async function f() {
|
||||
try {
|
||||
await fetch('https://typescriptlang.org');
|
||||
}
|
||||
catch (rejection) {
|
||||
} catch (rejection) {
|
||||
return console.log("rejected:", rejection);
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,7 @@ function my_print (resp): Promise<void> {
|
||||
async function f() {
|
||||
try {
|
||||
return my_print(fetch("https://typescriptlang.org").then(res => console.log(res)));
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return console.log("Error!", err);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -10,8 +10,7 @@ function /*[#|*/f/*|]*/() {
|
||||
async function f() {
|
||||
try {
|
||||
return fetch(Promise.resolve(1).then(res => "https://typescriptlang.org"));
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -10,8 +10,7 @@ function /*[#|*/f/*|]*/() {
|
||||
async function f() {
|
||||
try {
|
||||
return fetch(Promise.resolve(1).then(res => "https://typescriptlang.org"));
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ async function f(){
|
||||
const i_1 = await fetch("https://typescriptlang.org");
|
||||
const res = i_1.ok;
|
||||
return i + 1;
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return i - 1;
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -12,8 +12,7 @@ async function f() {
|
||||
try {
|
||||
const x = await Promise.resolve(1);
|
||||
return await Promise.reject(x);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -12,8 +12,7 @@ async function f() {
|
||||
try {
|
||||
const x = await Promise.resolve(1);
|
||||
return await Promise.reject(x);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -11,8 +11,7 @@ async function f() {
|
||||
try {
|
||||
const x = await Promise.resolve();
|
||||
x_2 = 1;
|
||||
}
|
||||
catch (x_1) {
|
||||
} catch (x_1) {
|
||||
x_2 = "a";
|
||||
}
|
||||
return !!x_2;
|
||||
|
||||
+1
-2
@@ -11,8 +11,7 @@ async function f() {
|
||||
try {
|
||||
const x = await Promise.resolve();
|
||||
x_2 = 1;
|
||||
}
|
||||
catch (x_1) {
|
||||
} catch (x_1) {
|
||||
x_2 = "a";
|
||||
}
|
||||
return !!x_2;
|
||||
|
||||
+1
-2
@@ -11,8 +11,7 @@ async function f() {
|
||||
try {
|
||||
await Promise.resolve();
|
||||
result = ({ x: 3 });
|
||||
}
|
||||
catch (e) {
|
||||
} catch (e) {
|
||||
result = ({ x: "a" });
|
||||
}
|
||||
const { x } = result;
|
||||
|
||||
+1
-2
@@ -11,8 +11,7 @@ async function f() {
|
||||
try {
|
||||
await Promise.resolve();
|
||||
result = ({ x: 3 });
|
||||
}
|
||||
catch (e) {
|
||||
} catch (e) {
|
||||
result = ({ x: "a" });
|
||||
}
|
||||
const { x } = result;
|
||||
|
||||
+1
-2
@@ -16,8 +16,7 @@ async function get() {
|
||||
try {
|
||||
return Promise
|
||||
.resolve<APIResponse<{ email: string; }>>({ success: true, data: { email: "" } });
|
||||
}
|
||||
catch (e) {
|
||||
} catch (e) {
|
||||
const result: APIResponse<{ email: string; }> = ({ success: false });
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ verify.codeFix({
|
||||
`class MyClass {
|
||||
constructor() { }
|
||||
foo() {
|
||||
try { }
|
||||
catch () { }
|
||||
try { } catch () { }
|
||||
}
|
||||
}
|
||||
`,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
////function foo() {
|
||||
//// /*1*/if (true) {
|
||||
//// console.log(1);
|
||||
//// } else {
|
||||
//// console.log(1);
|
||||
//// }
|
||||
////
|
||||
//// do {
|
||||
//// console.log(1);
|
||||
//// }
|
||||
////
|
||||
//// while (true);
|
||||
////
|
||||
//// try {
|
||||
//// console.log(1);
|
||||
//// } catch {
|
||||
//// void 0;
|
||||
//// } finally {
|
||||
//// void 0;
|
||||
//// }/*2*/
|
||||
////}
|
||||
|
||||
goTo.select("1", "2");
|
||||
edit.applyRefactor({
|
||||
refactorName: "Extract Symbol",
|
||||
actionName: "function_scope_1",
|
||||
actionDescription: "Extract to function in global scope",
|
||||
newContent:
|
||||
`function foo() {
|
||||
/*RENAME*/newFunction();
|
||||
}
|
||||
|
||||
function newFunction() {
|
||||
if (true) {
|
||||
console.log(1);
|
||||
} else {
|
||||
console.log(1);
|
||||
}
|
||||
|
||||
do {
|
||||
console.log(1);
|
||||
}
|
||||
|
||||
while (true);
|
||||
|
||||
try {
|
||||
console.log(1);
|
||||
} catch {
|
||||
void 0;
|
||||
} finally {
|
||||
void 0;
|
||||
}
|
||||
}
|
||||
`
|
||||
});
|
||||
Reference in new issue
Block a user