Skip to content

Commit

Permalink
refactor(rules): share template visitor class
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd committed Jun 27, 2018
1 parent fd20d5e commit ec428a0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
32 changes: 32 additions & 0 deletions src/helpers/directiveToElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as ast from '@angular/compiler';
import { BasicTemplateAstVisitor } from 'codelyzer/angular/templates/basicTemplateAstVisitor';

export function generateDescription(directive: string) {
return `${directive} is now an Element instead of an Angular Directive.`;
}

export function createDirectiveToElementTemplateVisitorClass(directive: string) {
return class extends BasicTemplateAstVisitor {
visitElement(element: ast.ElementAst, context: any): any {
if (element.name) {
const InvalidSyntaxBoxRe = new RegExp(`<\\w+[\\s\\S]+?(${directive})[\\s\\S]*?>`, 'gi');

let error = generateDescription(directive);

const expr = element.sourceSpan.toString();

let matches;

while ((matches = InvalidSyntaxBoxRe.exec(expr)) !== null) {
const index = expr.indexOf(directive);
const start = element.sourceSpan.start.offset + index;
const absolutePosition = this.getSourcePosition(start - 1);

this.addFailure(this.createFailure(start, directive.length, error));
}
}

super.visitElement(element, context);
}
};
}
33 changes: 6 additions & 27 deletions src/ionButtonIsNowAnElementRule.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
import * as ast from '@angular/compiler';
import { NgWalker } from 'codelyzer/angular/ngWalker';
import { BasicTemplateAstVisitor } from 'codelyzer/angular/templates/basicTemplateAstVisitor';
import * as Lint from 'tslint';
import { IOptions } from 'tslint';
import * as ts from 'typescript';

export const ruleName = 'ion-button-is-now-an-element';

class IonButtonIsNowAnElementTemplateVisitor extends BasicTemplateAstVisitor {
visitElement(element: ast.ElementAst, context: any): any {
if (element.name) {
const InvalidSyntaxBoxRe = /<\w+[\s\S]+?(ion-button)[\s\S]*?>/gi;

let error = 'Ion Button is now an Element instead of an attribute.';

const expr: any = (<any>element.sourceSpan).toString();

let matches;
import { createDirectiveToElementTemplateVisitorClass, generateDescription } from './helpers/directiveToElement';

while ((matches = InvalidSyntaxBoxRe.exec(expr)) !== null) {
const index = expr.indexOf('ion-button');
const start = element.sourceSpan.start.offset + index;
const absolutePosition = this.getSourcePosition(start - 1);

this.addFailure(this.createFailure(start, 10, error));
}
}
const directive = 'ion-button';
export const ruleName = 'ion-button-is-now-an-element';

super.visitElement(element, context);
}
}
const IonButtonIsNowAnElementTemplateVisitor = createDirectiveToElementTemplateVisitorClass(directive);

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: ruleName,
type: 'functionality',
description: 'Ion Button is now an Element instead of an attribute.',
description: generateDescription(directive),
options: null,
optionsDescription: 'Not configurable.',
typescriptOnly: false,
hasFix: true
hasFix: false
};

constructor(options: IOptions) {
Expand Down
6 changes: 3 additions & 3 deletions test/ionButtonIsNowAnElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe(ruleName, () => {

assertAnnotated({
ruleName,
message: 'Ion Button is now an Element instead of an attribute.',
message: 'ion-button is now an Element instead of an Angular Directive.',
source
});
});
Expand All @@ -44,7 +44,7 @@ describe(ruleName, () => {

assertAnnotated({
ruleName,
message: 'Ion Button is now an Element instead of an attribute.',
message: 'ion-button is now an Element instead of an Angular Directive.',
source
});
});
Expand All @@ -63,7 +63,7 @@ describe(ruleName, () => {

assertAnnotated({
ruleName,
message: 'Ion Button is now an Element instead of an attribute.',
message: 'ion-button is now an Element instead of an Angular Directive.',
source
});
});
Expand Down

0 comments on commit ec428a0

Please sign in to comment.