Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge 'decorators' into 'modifiers' on various nodes #49089

Merged
merged 9 commits into from
Jun 10, 2022
4 changes: 1 addition & 3 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1661,8 +1661,6 @@ namespace ts {
// - `BindingElement: BindingPattern Initializer?`
// - https://tc39.es/ecma262/#sec-runtime-semantics-keyedbindinginitialization
// - `BindingElement: BindingPattern Initializer?`
bindEach(node.decorators);
bindEach(node.modifiers);
bind(node.dotDotDotToken);
bind(node.propertyName);
bind(node.initializer);
Expand Down Expand Up @@ -2794,7 +2792,7 @@ namespace ts {
}

function bindNamespaceExportDeclaration(node: NamespaceExportDeclaration) {
if (node.modifiers && node.modifiers.length) {
if (some(node.modifiers)) {
file.bindDiagnostics.push(createDiagnosticForNode(node, Diagnostics.Modifiers_cannot_appear_here));
}
const diag = !isSourceFile(node.parent) ? Diagnostics.Global_module_exports_may_only_appear_at_top_level
Expand Down
202 changes: 95 additions & 107 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ namespace ts {
}

/** Works like Array.prototype.find, returning `undefined` if no element satisfying the predicate is found. */
export function find<T, U extends T>(array: readonly T[], predicate: (element: T, index: number) => element is U): U | undefined;
export function find<T>(array: readonly T[], predicate: (element: T, index: number) => boolean): T | undefined;
export function find<T>(array: readonly T[], predicate: (element: T, index: number) => boolean): T | undefined {
for (let i = 0; i < array.length; i++) {
export function find<T, U extends T>(array: readonly T[] | undefined, predicate: (element: T, index: number) => element is U, startIndex?: number): U | undefined;
export function find<T>(array: readonly T[] | undefined, predicate: (element: T, index: number) => boolean, startIndex?: number): T | undefined;
export function find<T>(array: readonly T[] | undefined, predicate: (element: T, index: number) => boolean, startIndex?: number): T | undefined {
if (array === undefined) return undefined;
for (let i = startIndex ?? 0; i < array.length; i++) {
const value = array[i];
if (predicate(value, i)) {
return value;
Expand All @@ -175,10 +176,11 @@ namespace ts {
return undefined;
}

export function findLast<T, U extends T>(array: readonly T[], predicate: (element: T, index: number) => element is U): U | undefined;
export function findLast<T>(array: readonly T[], predicate: (element: T, index: number) => boolean): T | undefined;
export function findLast<T>(array: readonly T[], predicate: (element: T, index: number) => boolean): T | undefined {
for (let i = array.length - 1; i >= 0; i--) {
export function findLast<T, U extends T>(array: readonly T[] | undefined, predicate: (element: T, index: number) => element is U, startIndex?: number): U | undefined;
export function findLast<T>(array: readonly T[] | undefined, predicate: (element: T, index: number) => boolean, startIndex?: number): T | undefined;
export function findLast<T>(array: readonly T[] | undefined, predicate: (element: T, index: number) => boolean, startIndex?: number): T | undefined {
if (array === undefined) return undefined;
for (let i = startIndex ?? array.length - 1; i >= 0; i--) {
const value = array[i];
if (predicate(value, i)) {
return value;
Expand All @@ -188,17 +190,19 @@ namespace ts {
}

/** Works like Array.prototype.findIndex, returning `-1` if no element satisfying the predicate is found. */
export function findIndex<T>(array: readonly T[], predicate: (element: T, index: number) => boolean, startIndex?: number): number {
for (let i = startIndex || 0; i < array.length; i++) {
export function findIndex<T>(array: readonly T[] | undefined, predicate: (element: T, index: number) => boolean, startIndex?: number): number {
if (array === undefined) return -1;
for (let i = startIndex ?? 0; i < array.length; i++) {
if (predicate(array[i], i)) {
return i;
}
}
return -1;
}

export function findLastIndex<T>(array: readonly T[], predicate: (element: T, index: number) => boolean, startIndex?: number): number {
for (let i = startIndex === undefined ? array.length - 1 : startIndex; i >= 0; i--) {
export function findLastIndex<T>(array: readonly T[] | undefined, predicate: (element: T, index: number) => boolean, startIndex?: number): number {
if (array === undefined) return -1;
for (let i = startIndex ?? array.length - 1; i >= 0; i--) {
if (predicate(array[i], i)) {
return i;
}
Expand Down Expand Up @@ -1079,8 +1083,8 @@ namespace ts {
/**
* Returns the first element of an array if non-empty, `undefined` otherwise.
*/
export function firstOrUndefined<T>(array: readonly T[]): T | undefined {
return array.length === 0 ? undefined : array[0];
export function firstOrUndefined<T>(array: readonly T[] | undefined): T | undefined {
return array === undefined || array.length === 0 ? undefined : array[0];
}

export function first<T>(array: readonly T[]): T {
Expand All @@ -1091,8 +1095,8 @@ namespace ts {
/**
* Returns the last element of an array if non-empty, `undefined` otherwise.
*/
export function lastOrUndefined<T>(array: readonly T[]): T | undefined {
return array.length === 0 ? undefined : array[array.length - 1];
export function lastOrUndefined<T>(array: readonly T[] | undefined): T | undefined {
return array === undefined || array.length === 0 ? undefined : array[array.length - 1];
}

export function last<T>(array: readonly T[]): T {
Expand Down
13 changes: 8 additions & 5 deletions src/compiler/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace ts {
warnAfter?: Version | string;
errorAfter?: Version | string;
typeScriptVersion?: Version | string;
name?: string;
}

export namespace Debug {
Expand Down Expand Up @@ -191,8 +192,10 @@ namespace ts {

export function assertEachNode<T extends Node, U extends T>(nodes: NodeArray<T>, test: (node: T) => node is U, message?: string, stackCrawlMark?: AnyFunction): asserts nodes is NodeArray<U>;
export function assertEachNode<T extends Node, U extends T>(nodes: readonly T[], test: (node: T) => node is U, message?: string, stackCrawlMark?: AnyFunction): asserts nodes is readonly U[];
export function assertEachNode<T extends Node, U extends T>(nodes: NodeArray<T> | undefined, test: (node: T) => node is U, message?: string, stackCrawlMark?: AnyFunction): asserts nodes is NodeArray<U> | undefined;
export function assertEachNode<T extends Node, U extends T>(nodes: readonly T[] | undefined, test: (node: T) => node is U, message?: string, stackCrawlMark?: AnyFunction): asserts nodes is readonly U[] | undefined;
export function assertEachNode(nodes: readonly Node[], test: (node: Node) => boolean, message?: string, stackCrawlMark?: AnyFunction): void;
export function assertEachNode(nodes: readonly Node[], test: (node: Node) => boolean, message?: string, stackCrawlMark?: AnyFunction) {
export function assertEachNode(nodes: readonly Node[] | undefined, test: (node: Node) => boolean, message?: string, stackCrawlMark?: AnyFunction) {
if (shouldAssertFunction(AssertionLevel.Normal, "assertEachNode")) {
assert(
test === undefined || every(nodes, test),
Expand Down Expand Up @@ -708,9 +711,9 @@ namespace ts {
};
}

function createDeprecation(name: string, options: DeprecationOptions & { error: true }): () => never;
function createDeprecation(name: string, options?: DeprecationOptions): () => void;
function createDeprecation(name: string, options: DeprecationOptions = {}) {
export function createDeprecation(name: string, options: DeprecationOptions & { error: true }): () => never;
export function createDeprecation(name: string, options?: DeprecationOptions): () => void;
export function createDeprecation(name: string, options: DeprecationOptions = {}) {
const version = typeof options.typeScriptVersion === "string" ? new Version(options.typeScriptVersion) : options.typeScriptVersion ?? getTypeScriptVersion();
const errorAfter = typeof options.errorAfter === "string" ? new Version(options.errorAfter) : options.errorAfter;
const warnAfter = typeof options.warnAfter === "string" ? new Version(options.warnAfter) : options.warnAfter;
Expand All @@ -730,7 +733,7 @@ namespace ts {
}

export function deprecate<F extends (...args: any[]) => any>(func: F, options?: DeprecationOptions): F {
const deprecation = createDeprecation(getFunctionName(func), options);
const deprecation = createDeprecation(options?.name ?? getFunctionName(func), options);
return wrapFunction(deprecation, func);
}
}
Expand Down
Loading