Skip to content

Commit

Permalink
fix(platform-browser): debug element query predicates not compatible …
Browse files Browse the repository at this point in the history
…with strictFunctionTypes (angular#30993)

Currently developers can use the `By` class to construct common
`DebugElement` query predicates. e.g. `By.directive(MyDirective)`.

The `directive()` and `all()` predicates are currently returning
a predicate that works for `DebugElement` nodes. This return type
is too strict since the predicate is not specific to `DebugElement`
instances and can also apply to `DebugNode` instances.

Meaning that developers are currently able to use the `directive()`
predicate when using `queryAllNodes()`. This is a common practice
but will break when the project is compiled with TypeScript's
`--strictFunctionTypes` flag as the `DebugElement` predicate type
is not assignable to predicates for `DebugNode`. In order to make
these predicates usable with `--strictFuntionTypes` enabled, we
adjust the predicate type to reflect what is actually needed for
evaluation of the predicate.

PR Close angular#30993
  • Loading branch information
devversion authored and sabeersulaiman committed Sep 6, 2019
1 parent df23e45 commit 2b179fa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions packages/platform-browser/src/dom/debug/by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {DebugElement, Predicate, Type} from '@angular/core';
import {DebugElement, DebugNode, Predicate, Type} from '@angular/core';
import {getDOM} from '../../dom/dom_adapter';


Expand All @@ -18,14 +18,14 @@ import {getDOM} from '../../dom/dom_adapter';
*/
export class By {
/**
* Match all elements.
* Match all nodes.
*
* @usageNotes
* ### Example
*
* {@example platform-browser/dom/debug/ts/by/by.ts region='by_all'}
*/
static all(): Predicate<DebugElement> { return (debugElement) => true; }
static all(): Predicate<DebugNode> { return () => true; }

/**
* Match elements by the given CSS selector.
Expand All @@ -44,14 +44,14 @@ export class By {
}

/**
* Match elements that have the given directive present.
* Match nodes that have the given directive present.
*
* @usageNotes
* ### Example
*
* {@example platform-browser/dom/debug/ts/by/by.ts region='by_directive'}
*/
static directive(type: Type<any>): Predicate<DebugElement> {
return (debugElement) => debugElement.providerTokens !.indexOf(type) !== -1;
static directive(type: Type<any>): Predicate<DebugNode> {
return (debugNode) => debugNode.providerTokens !.indexOf(type) !== -1;
}
}
4 changes: 2 additions & 2 deletions tools/public_api_guard/platform-browser/platform-browser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export declare class BrowserTransferStateModule {
}

export declare class By {
static all(): Predicate<DebugElement>;
static all(): Predicate<DebugNode>;
static css(selector: string): Predicate<DebugElement>;
static directive(type: Type<any>): Predicate<DebugElement>;
static directive(type: Type<any>): Predicate<DebugNode>;
}

export declare function disableDebugTools(): void;
Expand Down

0 comments on commit 2b179fa

Please sign in to comment.