Skip to content

Commit 665d465

Browse files
crisbetodylhunn
authored andcommitted
refactor(language-service): make selector nullable (#48193)
This is a follow-up from #48147. Changes the `DirectiveSymbol.selector` to be nullable since it's possible to have directives without a selector. PR Close #48193
1 parent 40c138c commit 665d465

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

packages/compiler-cli/src/ngtsc/typecheck/api/scope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface PotentialDirective {
4949
/**
5050
* The selector for the directive or component.
5151
*/
52-
selector: string;
52+
selector: string|null;
5353

5454
/**
5555
* `true` if this directive is a component.

packages/compiler-cli/src/ngtsc/typecheck/src/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
601601
const scope = this.getScopeData(component);
602602
if (scope !== null) {
603603
for (const directive of scope.directives) {
604+
if (directive.selector === null) {
605+
continue;
606+
}
607+
604608
for (const selector of CssSelector.parse(directive.selector)) {
605609
if (selector.element === null || tagMap.has(selector.element)) {
606610
// Skip this directive if it doesn't match an element tag, or if another directive has

packages/compiler-cli/src/ngtsc/typecheck/src/template_symbol_builder.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ export class SymbolBuilder {
180180
tsSymbol: symbol.tsSymbol,
181181
exposedInputs: current.inputs,
182182
exposedOutputs: current.outputs,
183-
// TODO(crisbeto): rework `DirectiveSymbol` to make
184-
// `selector` nullable and remove the `|| ''` here.
185-
selector: meta.selector || '',
183+
selector: meta.selector,
186184
isComponent: meta.isComponent,
187185
ngModule: this.getDirectiveModule(current.directive.node),
188186
kind: SymbolKind.Directive,

packages/language-service/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {NgCompiler} from '@angular/compiler-cli/src/ngtsc/core';
1010
import {absoluteFrom, absoluteFromSourceFile, AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
1111
import {isExternalResource} from '@angular/compiler-cli/src/ngtsc/metadata';
1212
import {DeclarationNode} from '@angular/compiler-cli/src/ngtsc/reflection';
13-
import {DirectiveSymbol, PotentialDirective, TemplateTypeChecker} from '@angular/compiler-cli/src/ngtsc/typecheck/api';
13+
import {DirectiveSymbol, TemplateTypeChecker} from '@angular/compiler-cli/src/ngtsc/typecheck/api';
1414
import * as e from '@angular/compiler/src/expression_parser/ast'; // e for expression AST
1515
import * as t from '@angular/compiler/src/render3/r3_ast'; // t for template AST
1616
import ts from 'typescript';
@@ -224,7 +224,7 @@ function difference<T>(left: Set<T>, right: Set<T>): Set<T> {
224224
* @returns The list of directives matching the tag name via the strategy described above.
225225
*/
226226
// TODO(atscott): Add unit tests for this and the one for attributes
227-
export function getDirectiveMatchesForElementTag<T extends {selector: string}>(
227+
export function getDirectiveMatchesForElementTag<T extends {selector: string | null}>(
228228
element: t.Template|t.Element, directives: T[]): Set<T> {
229229
const attributes = getAttributes(element);
230230
const allAttrs = attributes.map(toAttributeCssSelector);
@@ -269,7 +269,7 @@ export function getDirectiveMatchesForAttribute(
269269
* Given a list of directives and a text to use as a selector, returns the directives which match
270270
* for the selector.
271271
*/
272-
function getDirectiveMatchesForSelector<T extends {selector: string}>(
272+
function getDirectiveMatchesForSelector<T extends {selector: string | null}>(
273273
directives: T[], selector: string): Set<T> {
274274
try {
275275
const selectors = CssSelector.parse(selector);

0 commit comments

Comments
 (0)