Skip to content

Commit f9eb976

Browse files
committed
handle multiple prologue directives
Fixes: #24689
1 parent 1fc1495 commit f9eb976

File tree

6 files changed

+19
-20
lines changed

6 files changed

+19
-20
lines changed

src/compiler/utilities.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,21 +401,18 @@ namespace ts {
401401
}
402402

403403
/**
404-
* Appends a range of value to begin of an array, returning the array.
405-
*
406-
* @param to The array to which `value` is to be appended. If `to` is `undefined`, a new array
407-
* is created if `value` was appended.
408-
* @param from The values to append to the array. If `from` is `undefined`, nothing is
409-
* appended. If an element of `from` is `undefined`, that element is not appended.
404+
* Prepends statements to an array taking care of prologue directives.
410405
*/
411-
export function prependStatements<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[] | undefined {
406+
export function prependStatements<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[] {
412407
if (from === undefined || from.length === 0) return to;
413-
if (to === undefined) return from.slice();
414-
const prologue = to.length && isPrologueDirective(to[0]) && to.shift();
415-
to.unshift(...from);
416-
if (prologue) {
417-
to.unshift(prologue);
408+
let statementIndex = 0;
409+
// skip all prologue directives to insert at the correct position
410+
for (; statementIndex < to.length; ++statementIndex) {
411+
if (!isPrologueDirective(to[statementIndex])) {
412+
break;
413+
}
418414
}
415+
to.splice(statementIndex, 0, ...from);
419416
return to;
420417
}
421418

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6094,14 +6094,9 @@ declare namespace ts {
60946094
function nodeIsMissing(node: Node | undefined): boolean;
60956095
function nodeIsPresent(node: Node | undefined): boolean;
60966096
/**
6097-
* Appends a range of value to begin of an array, returning the array.
6098-
*
6099-
* @param to The array to which `value` is to be appended. If `to` is `undefined`, a new array
6100-
* is created if `value` was appended.
6101-
* @param from The values to append to the array. If `from` is `undefined`, nothing is
6102-
* appended. If an element of `from` is `undefined`, that element is not appended.
6097+
* Prepends statements to an array taking care of prologue directives.
61036098
*/
6104-
function prependStatements<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[] | undefined;
6099+
function prependStatements<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[];
61056100
/**
61066101
* Determine if the given comment is a triple-slash
61076102
*
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//// [destructuringTempOccursAfterPrologue.ts]
22
function test(p: any) {
33
'use strict';
4+
'use strong';
45
p = { prop: p } = p;
56
}
67

78
//// [destructuringTempOccursAfterPrologue.js]
89
function test(p) {
910
'use strict';
11+
'use strong';
1012
var _a;
1113
p = (_a = p, p = _a.prop, _a);
1214
}

tests/baselines/reference/destructuringTempOccursAfterPrologue.symbols

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ function test(p: any) {
44
>p : Symbol(p, Decl(destructuringTempOccursAfterPrologue.ts, 0, 14))
55

66
'use strict';
7+
'use strong';
78
p = { prop: p } = p;
89
>p : Symbol(p, Decl(destructuringTempOccursAfterPrologue.ts, 0, 14))
9-
>prop : Symbol(prop, Decl(destructuringTempOccursAfterPrologue.ts, 2, 9))
10+
>prop : Symbol(prop, Decl(destructuringTempOccursAfterPrologue.ts, 3, 9))
1011
>p : Symbol(p, Decl(destructuringTempOccursAfterPrologue.ts, 0, 14))
1112
>p : Symbol(p, Decl(destructuringTempOccursAfterPrologue.ts, 0, 14))
1213
}

tests/baselines/reference/destructuringTempOccursAfterPrologue.types

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ function test(p: any) {
66
'use strict';
77
>'use strict' : "use strict"
88

9+
'use strong';
10+
>'use strong' : "use strong"
11+
912
p = { prop: p } = p;
1013
>p = { prop: p } = p : any
1114
>p : any
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
function test(p: any) {
22
'use strict';
3+
'use strong';
34
p = { prop: p } = p;
45
}

0 commit comments

Comments
 (0)