-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as Lint from 'tslint/lib/lint'; | ||
import {FileNameRule, COMPONENT_TYPE} from './fileNameBase'; | ||
import {SelectorValidator} from './util/selectorValidator'; | ||
|
||
const FAILURE_STRING = 'The component should have a better filename'; | ||
|
||
export class Rule extends FileNameRule { | ||
constructor(ruleName: string, value: any, disabledIntervals: Lint.IDisabledInterval[]) { | ||
super( | ||
ruleName, | ||
value, | ||
disabledIntervals, | ||
//SelectorValidator.prefix(value[1]), | ||
//function(){}, | ||
FAILURE_STRING, | ||
COMPONENT_TYPE.DIRECTIVE); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint/lib/lint'; | ||
|
||
export enum COMPONENT_TYPE { | ||
COMPONENT, | ||
DIRECTIVE, | ||
ANY | ||
}; | ||
|
||
export class FileNameRule extends Lint.Rules.AbstractRule { | ||
constructor(ruleName: string, | ||
value: any, | ||
disabledIntervals: Lint.IDisabledInterval[], | ||
//private validator: Function, | ||
private failureString: string, | ||
private target: COMPONENT_TYPE = COMPONENT_TYPE.ANY) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
super(ruleName, value, disabledIntervals); | ||
} | ||
|
||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
let documentRegistry = ts.createDocumentRegistry(); | ||
let languageServiceHost = Lint.createLanguageServiceHost('file.ts', sourceFile.getFullText()); | ||
let languageService : ts.LanguageService = ts.createLanguageService(languageServiceHost, documentRegistry); | ||
return this.applyWithWalker( | ||
new FileNameValidatorWalker( | ||
sourceFile, | ||
languageService, | ||
this)); | ||
} | ||
} | ||
|
||
class FileNameValidatorWalker extends Lint.RuleWalker { | ||
private typeChecker : ts.TypeChecker; | ||
|
||
constructor(private sourceFile: ts.SourceFile, private languageService : ts.LanguageService, private rule: FileNameRule) { | ||
This comment has been minimized.
Sorry, something went wrong.
mgechev
|
||
super(sourceFile, rule.getOptions()); | ||
this.typeChecker = languageService.getProgram().getTypeChecker(); | ||
} | ||
|
||
visitClassDeclaration(node: ts.ClassDeclaration) { | ||
(node.decorators || []).forEach(this.validateDecorator.bind(this, node.name.text)); | ||
super.visitClassDeclaration(node); | ||
} | ||
private validateDecorator(className: string, decorator: ts.Decorator) { | ||
let baseExpr = <any>decorator.expression || {}; | ||
let expr = baseExpr.expression || {}; | ||
let name = expr.text; | ||
let args = baseExpr.arguments || []; | ||
let arg = args[0]; | ||
if (this.rule.targetType === COMPONENT_TYPE.ANY) { | ||
this.validateFileName(className, COMPONENT_TYPE.ANY, arg); | ||
} else if (this.rule.targetType === COMPONENT_TYPE.COMPONENT && name === 'Component' ) { | ||
this.validateFileName(className, COMPONENT_TYPE.COMPONENT, arg); | ||
} else if (this.rule.targetType === COMPONENT_TYPE.DIRECTIVE && name === 'Directive') { | ||
this.validateFileName(className, COMPONENT_TYPE.DIRECTIVE, arg); | ||
} | ||
} | ||
private validateFileName(className: string, type:COMPONENT_TYPE, arg: ts.Node) { | ||
let error = 'bad name failure'; | ||
this.addFailure(this.createFailure(0, 0, error)); | ||
} | ||
} |
3 comments
on commit 52aee8b
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.
@mgechev I've pushed the changes but i recompiled and attempted to run it and i'm still getting the same strange error:
_super.visitClassDeclaration.call(this, node); ^
TypeError: Cannot read property 'call' of undefined
at FileNameValidatorWalker.visitClassDeclaration (C:\project\node_modules\ng2lint\dist\src\fileNameBase.js:42:37)
Feels like i'm missing something super basic here.
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.
@mgechev I'm comparing the transpiled output of my rule(s) and your rules and there are some differences. I haven't been able to dig up anything outside your tsconfig file that would help me understand how you're transpiling... Would you be able to share the command (and options) you're using to transpile the rules? Thanks!
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.
You can find them here.
The property is
private
and namedtarget
instead oftargetType
as used below.Should be
public
and namedtargetType
.