Skip to content

Commit

Permalink
fix(css): set proper base position
Browse files Browse the repository at this point in the history
Fix #166
  • Loading branch information
mgechev committed Nov 26, 2016
1 parent 5cb2ea0 commit e9575fb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
14 changes: 12 additions & 2 deletions src/angular/ng2Walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', ((<any>metadata.controller || {}).name || {}).text);
Expand All @@ -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', ((<any>metadata.controller || {}).name || {}).text);
}
Expand Down
1 change: 0 additions & 1 deletion test/angular/sourceMappingVisitor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
});
48 changes: 24 additions & 24 deletions test/noUnusedCssRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
});
Expand Down Expand Up @@ -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
}
});
});
Expand Down Expand Up @@ -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
}
});
});
Expand Down Expand Up @@ -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
}
});
});
Expand Down Expand Up @@ -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
}
});
});
Expand Down Expand Up @@ -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
}
});
});
Expand Down Expand Up @@ -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
}
});
});
Expand Down Expand Up @@ -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
}
});
});
Expand Down Expand Up @@ -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
}
});
});
Expand All @@ -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
}
});
});
Expand All @@ -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
}
});
});
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit e9575fb

Please sign in to comment.