Skip to content
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

Fix desugared directive #401

Merged
merged 9 commits into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/angular/ngWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export interface NgWalkerConfig {
}

export class NgWalker extends Lint.RuleWalker {

static prop: any;

constructor(sourceFile: ts.SourceFile,
protected _originalOptions: Lint.IOptions,
private _config?: NgWalkerConfig,
Expand Down
19 changes: 11 additions & 8 deletions src/angular/templates/basicTemplateAstVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ExpTypes } from '../expressionTypes';
import { ComponentMetadata } from '../metadata';
import { RecursiveAngularExpressionVisitor } from './recursiveAngularExpressionVisitor';
import { SourceMappingVisitor } from '../sourceMappingVisitor';
import { NgWalker } from '../ngWalker';


const getExpressionDisplacement = (binding: any) => {
Expand Down Expand Up @@ -49,18 +50,19 @@ const getExpressionDisplacement = (binding: any) => {
// Total length of the attribute value
if (binding instanceof ast.BoundEventAst) {
valLen = binding.handler.span.end;
} else if (binding instanceof ast.BoundDirectivePropertyAst && binding.templateName === 'ngForOf') {
} else if (binding instanceof ast.BoundDirectivePropertyAst &&
( binding.templateName === 'ngForOf' || binding.templateName === 'ngIf')) {
// Handling everything except [ngForOf] differently
// [ngForOf] requires different logic because it gets desugered from *ngFor
// [ngForOf] and [ngIf] require different logic because it gets desugered
const content = binding.sourceSpan.end.file.content;
const ngForSubstr = content.substring(binding.sourceSpan.start.offset, binding.sourceSpan.end.offset);
result = ngForSubstr.lastIndexOf((binding.value as any).source) + '*ngFor'.length;
result = binding.sourceSpan.start.file.content.lastIndexOf((binding.value as any).source);
} else {
valLen = binding.value.span.end;
}

// Handling everything except [ngForOf]
if (!(binding instanceof ast.BoundDirectivePropertyAst) || binding.templateName !== 'ngForOf') {
if (!(binding instanceof ast.BoundDirectivePropertyAst) ||
( binding.templateName !== 'ngForOf' && binding.templateName !== 'ngIf')) {
// Total length of the entire part of the template AST corresponding to this AST.
totalLength = binding.sourceSpan.end.offset - binding.sourceSpan.start.offset;
// The whitespace are possible only in:
Expand Down Expand Up @@ -139,7 +141,7 @@ export class BasicTemplateAstVisitor extends SourceMappingVisitor implements ast
visitElementProperty(prop: ast.BoundElementPropertyAst, context: any): any {
const ast: any = (<e.ASTWithSource>prop.value).ast;
ast.interpolateExpression = (<any>prop.value).source;
this.visitNgTemplateAST(prop.value, this.templateStart + getExpressionDisplacement(prop));
this.visitNgTemplateAST(prop.value, this.templateStart + getExpressionDisplacement(prop), prop);
}

visitAttr(ast: ast.AttrAst, context: any): any {}
Expand All @@ -164,14 +166,15 @@ export class BasicTemplateAstVisitor extends SourceMappingVisitor implements ast

visitDirectiveProperty(prop: ast.BoundDirectivePropertyAst, context: any): any {
if (ExpTypes.ASTWithSource(prop.value)) {
this.visitNgTemplateAST(prop.value, this.templateStart + getExpressionDisplacement(prop));
this.visitNgTemplateAST(prop.value, this.templateStart + getExpressionDisplacement(prop), prop);
}
}

protected visitNgTemplateAST(ast: e.AST, templateStart: number) {
protected visitNgTemplateAST(ast: e.AST, templateStart: number, prop?: any) {
const templateVisitor =
new this.expressionVisitorCtrl(this.getSourceFile(), this._originalOptions, this.context, templateStart);
templateVisitor.preDefinedVariables = this._variables;
NgWalker.prop = prop;
templateVisitor.visit(ast);
templateVisitor.getFailures().forEach(f => this.addFailure(f));
}
Expand Down
20 changes: 15 additions & 5 deletions src/angularWhitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,21 @@ class WhitespaceTemplateVisitor extends BasicTemplateAstVisitor {

class PipeWhitespaceVisitor extends RecursiveAngularExpressionVisitor implements ConfigurableVisitor {
visitPipe(ast: ast.BindingPipe, context: RecursiveAngularExpressionVisitor): any {
const start = ast.span.start;
const exprStart = context.getSourcePosition(ast.exp.span.start);
const exprEnd = context.getSourcePosition(ast.exp.span.end);
const sf = context.getSourceFile().getFullText();
const exprText = sf.substring(exprStart, exprEnd);

let exprStart, exprEnd, exprText, sf;
if (NgWalker.prop && ( NgWalker.prop.templateName === 'ngForOf' || NgWalker.prop.templateName === 'ngIf')) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can preserve the context (prop) from a parent element visitor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is an evil way. After a good night, now, we have our context the parent class sourceMappingVistor. It's better.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally, I modified SourceMappingVisitor.getSourcePosition() method, which is more central. So, this fix the position problem in our tests.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this valid for all different templates? * attribute prefix always desugars to <ng-template...></ng-template>.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a test on prop.templateName should be enough.

exprStart = context.getSourcePosition(ast.exp.span.start) - (ast.span.start); // t pony of ponies |
exprEnd = context.getSourcePosition(ast.exp.span.end) - (ast.span.start); // - (ast.span.end - ast.span.start);
sf = context.getSourceFile().getFullText();
exprText = sf.substring(exprStart, exprEnd);
NgWalker.prop = null;
} else {
exprStart = context.getSourcePosition(ast.exp.span.start);
exprEnd = context.getSourcePosition(ast.exp.span.end);
sf = context.getSourceFile().getFullText();
exprText = sf.substring(exprStart, exprEnd);
}

const replacements = [];
// Handling the right side of the pipe
let leftBeginning = exprEnd + 1; // exprEnd === '|'
Expand Down
6 changes: 3 additions & 3 deletions src/contextualLifeCycleRule.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as Lint from 'tslint';
import * as ts from 'typescript';
import {sprintf} from 'sprintf-js';
import { sprintf } from 'sprintf-js';
import SyntaxKind = require('./util/syntaxKind');
import {NgWalker} from './angular/ngWalker';
import {ComponentMetadata, DirectiveMetadata} from './angular/metadata';
import { NgWalker } from './angular/ngWalker';
import { ComponentMetadata, DirectiveMetadata } from './angular/metadata';


export class Rule extends Lint.Rules.AbstractRule {
Expand Down
50 changes: 18 additions & 32 deletions src/noAccessMissingMemberRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ import * as ts from 'typescript';
import { sprintf } from 'sprintf-js';
import { stringDistance } from './util/utils';
import { NgWalker } from './angular/ngWalker';
import {
RecursiveAngularExpressionVisitor,
FlatSymbolTable
} from './angular/templates/recursiveAngularExpressionVisitor';
import { RecursiveAngularExpressionVisitor, FlatSymbolTable } from './angular/templates/recursiveAngularExpressionVisitor';
import { ExpTypes } from './angular/expressionTypes';
import {
getDeclaredMethodNames,
getDeclaredPropertyNames
} from './util/classDeclarationUtils';
import { getDeclaredMethodNames, getDeclaredPropertyNames } from './util/classDeclarationUtils';
import * as e from '@angular/compiler/src/expression_parser/ast';

import { Config } from './angular/config';
Expand Down Expand Up @@ -50,8 +44,7 @@ class SymbolAccessValidator extends RecursiveAngularExpressionVisitor {
symbolType = 'property';
}

available = Object.assign(
{},
available = Object.assign({},
getDeclaredMethodNames(this.context.controller),
getDeclaredPropertyNames(this.context.controller),
this.preDefinedVariables
Expand Down Expand Up @@ -81,11 +74,7 @@ class SymbolAccessValidator extends RecursiveAngularExpressionVisitor {
}

if (ast.name && !available[ast.name]) {
let failureString = sprintf.apply(this, [
Rule.FAILURE,
symbolType,
ast.name
]);
let failureString = sprintf.apply(this, [Rule.FAILURE, symbolType, ast.name]);
const top = this.getTopSuggestion(Object.keys(available), ast.name);
const getSuggestion = (list: string[]) => {
if (list.length === 1) {
Expand All @@ -99,9 +88,7 @@ class SymbolAccessValidator extends RecursiveAngularExpressionVisitor {
return result;
};
if (top.length && top[0].distance <= 2) {
failureString += ` Probably you mean: ${getSuggestion(
top.map(s => s.element)
)}.`;
failureString += ` Probably you mean: ${getSuggestion(top.map(s => s.element))}.`;
}
const width = ast.name.length;
this.addFailure(this.createFailure(ast.span.start, width, failureString));
Expand All @@ -111,21 +98,19 @@ class SymbolAccessValidator extends RecursiveAngularExpressionVisitor {

private getTopSuggestion(list: string[], current: string) {
const result = [];
const tmp = list
.map(e => {
return {
element: e,
distance: stringDistance(e, current)
};
})
.sort((a, b) => a.distance - b.distance);
const tmp = list.map(e => {
return {
element: e,
distance: stringDistance(e, current)
};
}).sort((a, b) => a.distance - b.distance);
const first = tmp.shift();
if (!first) {
return [];
} else {
result.push(first);
let current: any;
while ((current = tmp.shift())) {
while (current = tmp.shift()) {
if (current.distance !== first.distance) {
return result;
} else {
Expand All @@ -145,16 +130,17 @@ export class Rule extends Lint.Rules.AbstractRule {
rationale: `Such occurances in code are most likely a result of a typo.`,
options: null,
optionsDescription: `Not configurable.`,
typescriptOnly: true
typescriptOnly: true,
};

static FAILURE: string = 'The %s "%s" that you\'re trying to access does not exist in the class declaration.';

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new NgWalker(sourceFile, this.getOptions(), {
expressionVisitorCtrl: SymbolAccessValidator
})
);
new NgWalker(sourceFile,
this.getOptions(), {
expressionVisitorCtrl: SymbolAccessValidator
}));
}
}

Loading