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
refactor: use type guard instead of unnecessary or incorrect type assertions #659
Conversation
logger.error('Cannot parse the styles of', ((<any>metadata.controller || {}).name || {}).text, e); | ||
const { | ||
controller: { name } | ||
} = metadata; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs defaults in order to guard the same way the original did. Not sure if intentional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The property controller
always exists according to:
codelyzer/src/angular/metadata.ts
Lines 23 to 27 in da0f553
export class DirectiveMetadata { | |
controller!: ts.ClassDeclaration; | |
decorator!: ts.Decorator; | |
selector!: string; | |
} |
... and it makes sense, because once we instantiate a DirectiveMetadata
, we pass all properties:
codelyzer/src/angular/metadataReader.ts
Lines 63 to 67 in da0f553
return Object.assign(new DirectiveMetadata(), { | |
controller: d, | |
decorator: dec, | |
selector: selector.unwrap() | |
}); |
... now I'm curious on why we are using Object.assign
instead of a trivial constructor
. Thoughts?
Anyway, these "defaults" should have been changed in #631, but I really forgot about it. 😞
bump 👽 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -95,7 +95,7 @@ export class SourceMappingVisitor extends RuleWalker { | |||
parentAST: any; | |||
private consumer: SourceMapConsumer; | |||
|
|||
constructor(sourceFile: ts.SourceFile, options: IOptions, protected codeWithMap: CodeWithSourceMap, protected basePosition: number) { | |||
constructor(sourceFile: ts.SourceFile, options: IOptions, public codeWithMap: CodeWithSourceMap, protected basePosition: number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not protected
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was necessary because I've changed the type of context
in trackByRule#35
from any
to BasicTemplateAstVisitor
(https://github.com/mgechev/codelyzer/pull/659/files#diff-26d9296d5e0a25d3fe6a9b174e67da35R35)... it gives an error with protected
:
src/trackByFunctionRule.ts(44,30): error TS2446: Property 'codeWithMap' is protected and only accessible through an instance of class 'TrackByFunctionTemplateVisitor'.
@@ -8,7 +8,7 @@ import { ComponentMetadata } from '../metadata'; | |||
import { RecursiveAngularExpressionVisitor } from './recursiveAngularExpressionVisitor'; | |||
import { SourceMappingVisitor } from '../sourceMappingVisitor'; | |||
|
|||
const getExpressionDisplacement = (binding: any) => { | |||
const getExpressionDisplacement = (binding: ast.TemplateAst) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
This:
PS: You can see that there are a lot more places that could be changed, but these or I can't replace, because changing them, breaks something or unfortunately it has to be any.