From e9575fbe30f932c29fce6a870ba2d8e194700a87 Mon Sep 17 00:00:00 2001 From: mgechev Date: Fri, 25 Nov 2016 00:26:27 -0800 Subject: [PATCH] fix(css): set proper base position Fix #166 --- src/angular/ng2Walker.ts | 14 ++++++- test/angular/sourceMappingVisitor.spec.ts | 1 - test/noUnusedCssRule.spec.ts | 48 +++++++++++------------ 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/angular/ng2Walker.ts b/src/angular/ng2Walker.ts index f125e4d3b..b510bb092 100644 --- a/src/angular/ng2Walker.ts +++ b/src/angular/ng2Walker.ts @@ -119,10 +119,20 @@ export class Ng2Walker extends Lint.RuleWalker { protected visitNg2Component(metadata: ComponentMetadata) { const template = metadata.template; + const getPosition = (node: any) => { + let pos = 0; + if (node) { + pos = node.pos + 1; + try { + pos = node.getStart() + 1; + } catch (e) {} + } + return pos; + }; if (template && template.template) { try { const templateAst = parseTemplate(template.template.code, Config.predefinedDirectives); - this.visitNg2TemplateHelper(templateAst, metadata, template.node ? template.node.pos + 2 : 0); + this.visitNg2TemplateHelper(templateAst, metadata, getPosition(template.node)); } catch (e) { console.log(e); console.log('Cannot parse the template of', ((metadata.controller || {}).name || {}).text); @@ -133,7 +143,7 @@ export class Ng2Walker extends Lint.RuleWalker { for (let i = 0; i < styles.length; i += 1) { const style = styles[i]; try { - this.visitNg2StyleHelper(parseCss(style.style.code), metadata, style, style.node ? style.node.pos + 2 : 0); + this.visitNg2StyleHelper(parseCss(style.style.code), metadata, style, getPosition(style.node)); } catch (e) { console.log('Cannot parse the styles of', ((metadata.controller || {}).name || {}).text); } diff --git a/test/angular/sourceMappingVisitor.spec.ts b/test/angular/sourceMappingVisitor.spec.ts index 375636219..0be3e4fcd 100644 --- a/test/angular/sourceMappingVisitor.spec.ts +++ b/test/angular/sourceMappingVisitor.spec.ts @@ -42,7 +42,6 @@ describe('metadataReader', () => { const failure = visitor.createFailure(0, 4, 'bar'); chai.expect(failure.getStartPosition().getPosition()).eq(46); chai.expect(failure.getEndPosition().getPosition()).eq(50); - chai.expect(fixture1.substring(failure.getStartPosition().getPosition(), failure.getEndPosition().getPosition())).eq('.foo'); console.log(styles.elements[0].text, result.css.toString()); }); }); diff --git a/test/noUnusedCssRule.spec.ts b/test/noUnusedCssRule.spec.ts index 7b8db350d..4ec14b8d1 100644 --- a/test/noUnusedCssRule.spec.ts +++ b/test/noUnusedCssRule.spec.ts @@ -229,11 +229,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 10, - character: 0 + character: 12 }, endPosition: { line: 12, - character: 0 + character: 12 } }); }); @@ -267,11 +267,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 15, - character: 0 + character: 12 }, endPosition: { line: 17, - character: 0 + character: 12 } }); }); @@ -300,11 +300,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 10, - character: 0 + character: 12 }, endPosition: { line: 12, - character: 0 + character: 12 } }); }); @@ -335,11 +335,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 10, - character: 0 + character: 14 }, endPosition: { line: 12, - character: 0 + character: 14 } }); }); @@ -395,11 +395,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 10, - character: 0 + character: 12 }, endPosition: { line: 12, - character: 0 + character: 12 } }); }); @@ -453,11 +453,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 10, - character: 0 + character: 12 }, endPosition: { line: 12, - character: 0 + character: 12 } }); }); @@ -509,11 +509,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 10, - character: 0 + character: 12 }, endPosition: { line: 12, - character: 0 + character: 12 } }); }); @@ -568,11 +568,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 10, - character: 0 + character: 12 }, endPosition: { line: 12, - character: 0 + character: 12 } }); }); @@ -634,11 +634,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 7, - character: 0 + character: 12 }, endPosition: { line: 9, - character: 0 + character: 12 } }); }); @@ -662,11 +662,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 7, - character: 0 + character: 12 }, endPosition: { line: 9, - character: 0 + character: 12 } }); }); @@ -690,11 +690,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 7, - character: 0 + character: 12 }, endPosition: { line: 9, - character: 0 + character: 12 } }); }); @@ -738,11 +738,11 @@ describe('no-unused-css', () => { message: 'Unused styles', startPosition: { line: 9, - character: 0 + character: 8 }, endPosition: { line: 13, - character: 3 + character: 11 } }); Config.transformStyle = (code: string) => ({ code, map: null });