Skip to content

Commit

Permalink
initial version of the component & base filename rule
Browse files Browse the repository at this point in the history
  • Loading branch information
randyaa committed Mar 25, 2016
1 parent 6e227f2 commit 52aee8b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/componentFileNameRule.ts
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);
}
}
63 changes: 63 additions & 0 deletions src/fileNameBase.ts
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.

Copy link
@mgechev

mgechev Mar 27, 2016

The property is private and named target instead of targetType as used below.

Should be public and named targetType.

This comment has been minimized.

Copy link
@randyaa

randyaa Mar 27, 2016

Author Owner

thanks, fixed... i have no idea why I did this.

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.

Copy link
@mgechev

mgechev Mar 27, 2016

sourceFile and languageService are private, however, in the base class they are not.

This comment has been minimized.

Copy link
@randyaa

randyaa Mar 27, 2016

Author Owner

thanks. i changed this although i'm not really sure what difference it makes.

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

@randyaa
Copy link
Owner Author

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.

@randyaa
Copy link
Owner Author

@randyaa randyaa commented on 52aee8b Apr 2, 2016

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!

@mgechev
Copy link

@mgechev mgechev commented on 52aee8b Apr 2, 2016

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.

Please sign in to comment.