diff --git a/src/harness/unittests/extractFunctions.ts b/src/harness/unittests/extractFunctions.ts index 715a4f5aa0c..c69026c0be8 100644 --- a/src/harness/unittests/extractFunctions.ts +++ b/src/harness/unittests/extractFunctions.ts @@ -438,6 +438,42 @@ function f() { a; x; }`); + // We propagate numericLiteralFlags, but it's not consumed by the emitter, + // so everything comes out decimal. It would be nice to improve this. + testExtractFunction("extractFunction_VariableDeclaration_Writes_Let_LiteralType1", ` +function f() { + let a = 1; + [#|let x: 0o10 | 10 | 0b10 = 10; + a++;|] + a; x; +}`); + + testExtractFunction("extractFunction_VariableDeclaration_Writes_Let_LiteralType2", ` +function f() { + let a = 1; + [#|let x: "a" | 'b' = 'a'; + a++;|] + a; x; +}`); + + // We propagate numericLiteralFlags, but it's not consumed by the emitter, + // so everything comes out decimal. It would be nice to improve this. + testExtractFunction("extractFunction_VariableDeclaration_Writes_Let_LiteralType1", ` +function f() { + let a = 1; + [#|let x: 0o10 | 10 | 0b10 = 10; + a++;|] + a; x; +}`); + + testExtractFunction("extractFunction_VariableDeclaration_Writes_Let_TypeWithComments", ` +function f() { + let a = 1; + [#|let x: /*A*/ "a" /*B*/ | /*C*/ 'b' /*D*/ = 'a'; + a++;|] + a; x; +}`); + testExtractFunction("extractFunction_VariableDeclaration_Writes_Const_NoType", ` function f() { let a = 1; diff --git a/src/services/utilities.ts b/src/services/utilities.ts index a8dea4ddd0e..c57b82bc8b8 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1350,6 +1350,11 @@ namespace ts { if (visited === node) { // This only happens for leaf nodes - internal nodes always see their children change. const clone = getSynthesizedClone(node); + if (isStringLiteral(clone)) { + clone.textSourceNode = node as any; + } else if (isNumericLiteral(clone)) { + clone.numericLiteralFlags = (node as any).numericLiteralFlags; + } clone.pos = node.pos; clone.end = node.end; return clone; diff --git a/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Writes_Let_LiteralType1.ts b/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Writes_Let_LiteralType1.ts new file mode 100644 index 00000000000..50bad34efce --- /dev/null +++ b/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Writes_Let_LiteralType1.ts @@ -0,0 +1,34 @@ +// ==ORIGINAL== + +function f() { + let a = 1; + /*[#|*/let x: 0o10 | 10 | 0b10 = 10; + a++;/*|]*/ + a; x; +} +// ==SCOPE::Extract to inner function in function 'f'== + +function f() { + let a = 1; + let x: 8 | 10 | 2 = /*RENAME*/newFunction(); + a; x; + + function newFunction() { + let x: 0o10 | 10 | 0b10 = 10; + a++; + return x; + } +} +// ==SCOPE::Extract to function in global scope== + +function f() { + let a = 1; + let x: (8 | 10 | 2) | undefined; + ({ x, a } = /*RENAME*/newFunction(a)); + a; x; +} +function newFunction(a: number) { + let x: 0o10 | 10 | 0b10 = 10; + a++; + return { x, a }; +} diff --git a/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Writes_Let_LiteralType2.ts b/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Writes_Let_LiteralType2.ts new file mode 100644 index 00000000000..2df8ab67e9f --- /dev/null +++ b/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Writes_Let_LiteralType2.ts @@ -0,0 +1,34 @@ +// ==ORIGINAL== + +function f() { + let a = 1; + /*[#|*/let x: "a" | 'b' = 'a'; + a++;/*|]*/ + a; x; +} +// ==SCOPE::Extract to inner function in function 'f'== + +function f() { + let a = 1; + let x: "a" | 'b' = /*RENAME*/newFunction(); + a; x; + + function newFunction() { + let x: "a" | 'b' = 'a'; + a++; + return x; + } +} +// ==SCOPE::Extract to function in global scope== + +function f() { + let a = 1; + let x: ("a" | 'b') | undefined; + ({ x, a } = /*RENAME*/newFunction(a)); + a; x; +} +function newFunction(a: number) { + let x: "a" | 'b' = 'a'; + a++; + return { x, a }; +} diff --git a/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Writes_Let_TypeWithComments.ts b/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Writes_Let_TypeWithComments.ts new file mode 100644 index 00000000000..53599c26d08 --- /dev/null +++ b/tests/baselines/reference/extractFunction/extractFunction_VariableDeclaration_Writes_Let_TypeWithComments.ts @@ -0,0 +1,34 @@ +// ==ORIGINAL== + +function f() { + let a = 1; + /*[#|*/let x: /*A*/ "a" /*B*/ | /*C*/ 'b' /*D*/ = 'a'; + a++;/*|]*/ + a; x; +} +// ==SCOPE::Extract to inner function in function 'f'== + +function f() { + let a = 1; + let x: /*A*/ "a" /*B*/ | /*C*/ 'b' /*D*/ = /*RENAME*/newFunction(); + a; x; + + function newFunction() { + let x: /*A*/ "a" /*B*/ | /*C*/ 'b' /*D*/ = 'a'; + a++; + return x; + } +} +// ==SCOPE::Extract to function in global scope== + +function f() { + let a = 1; + let x: (/*A*/ "a" /*B*/ | /*C*/ 'b' /*D*/) | undefined; + ({ x, a } = /*RENAME*/newFunction(a)); + a; x; +} +function newFunction(a: number) { + let x: /*A*/ "a" /*B*/ | /*C*/ 'b' /*D*/ = 'a'; + a++; + return { x, a }; +}