handle multiple prologue directives

Fixes: #24689
This commit is contained in:
Klaus Meinhardt
2018-07-10 22:42:21 +02:00
parent 1fc1495863
commit f9eb976319
6 changed files with 19 additions and 20 deletions

View File

@@ -401,21 +401,18 @@ namespace ts {
}
/**
* Appends a range of value to begin of an array, returning the array.
*
* @param to The array to which `value` is to be appended. If `to` is `undefined`, a new array
* is created if `value` was appended.
* @param from The values to append to the array. If `from` is `undefined`, nothing is
* appended. If an element of `from` is `undefined`, that element is not appended.
* Prepends statements to an array taking care of prologue directives.
*/
export function prependStatements<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[] | undefined {
export function prependStatements<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[] {
if (from === undefined || from.length === 0) return to;
if (to === undefined) return from.slice();
const prologue = to.length && isPrologueDirective(to[0]) && to.shift();
to.unshift(...from);
if (prologue) {
to.unshift(prologue);
let statementIndex = 0;
// skip all prologue directives to insert at the correct position
for (; statementIndex < to.length; ++statementIndex) {
if (!isPrologueDirective(to[statementIndex])) {
break;
}
}
to.splice(statementIndex, 0, ...from);
return to;
}