fix(core): pipes injecting viewProviders when used on a component host node (#36512)

The flag that determines whether something should be able to inject from `viewProviders` is opt-out and the pipes weren't opted out, resulting in them being able to see the viewProviders if they're placed on a component host node.

Fixes #36146.

PR Close #36512
This commit is contained in:
crisbeto
2020-04-17 06:30:04 +02:00
committed by Matias Niemelä
parent bb150c2704
commit 81d23b33ef
3 changed files with 137 additions and 1 deletions

View File

@ -71,7 +71,7 @@ import {stringifyForError} from './util/misc_utils';
*/
let includeViewProviders = true;
function setIncludeViewProviders(v: boolean): boolean {
export function setIncludeViewProviders(v: boolean): boolean {
const oldValue = includeViewProviders;
includeViewProviders = v;
return oldValue;

View File

@ -11,6 +11,7 @@ import {PipeTransform} from '../change_detection/pipe_transform';
import {setInjectImplementation} from '../di/injector_compatibility';
import {getFactoryDef} from './definition';
import {setIncludeViewProviders} from './di';
import {store, ɵɵdirectiveInject} from './instructions/all';
import {PipeDef, PipeDefList} from './interfaces/definition';
import {HEADER_OFFSET, LView, TVIEW} from './interfaces/view';
@ -47,7 +48,12 @@ export function ɵɵpipe(index: number, pipeName: string): any {
const pipeFactory = pipeDef.factory || (pipeDef.factory = getFactoryDef(pipeDef.type, true));
const previousInjectImplementation = setInjectImplementation(ɵɵdirectiveInject);
// DI for pipes is supposed to behave like directives when placed on a component
// host node, which means that we have to disable access to `viewProviders`.
const previousIncludeViewProviders = setIncludeViewProviders(false);
const pipeInstance = pipeFactory();
setIncludeViewProviders(previousIncludeViewProviders);
setInjectImplementation(previousInjectImplementation);
store(tView, getLView(), index, pipeInstance);
return pipeInstance;