diff --git a/package.json b/package.json index aa31143..61c16a3 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "build": "npm run clean && tsc", "watch": "npm run clean && tsc --watch", "clean": "rm -rf ./dist", - "lint": "tslint --force './src/**/*.ts'" + "lint": "tslint --force './src/**/*.ts'", + "test": "mocha -r ts-node/register tests/**/*.spec.ts" }, "repository": { "type": "git", @@ -41,9 +42,14 @@ }, "config": {}, "devDependencies": { + "@types/chai": "^3.4.34", "@types/cheerio": "^0.17.31", "@types/glob": "^5.0.30", "@types/lodash": "^4.14.41", + "@types/mocha": "^2.2.33", + "chai": "^3.5.0", + "mocha": "^3.2.0", + "ts-node": "^1.7.0", "tslint": "^4.0.2", "tslint-eslint-rules": "^3.1.0", "typescript": "^2.0.10" diff --git a/src/parsers/abstract-template.parser.ts b/src/parsers/abstract-template.parser.ts index acecfff..5b66fe9 100644 --- a/src/parsers/abstract-template.parser.ts +++ b/src/parsers/abstract-template.parser.ts @@ -12,9 +12,9 @@ export abstract class AbstractTemplateParser { * Extracts inline template from components */ protected _extractInlineTemplate(contents: string): string { - const match = new RegExp('template\\s?:\\s?(("|\'|`)(.|[\\r\\n])+?[^\\\\]\\2)').exec(contents); + const match = new RegExp('template\\s?:\\s?("|\'|`)((.|[\\r\\n])+?[^\\\\])\\1').exec(contents); if (match !== null) { - return match[1]; + return match[2]; } return ''; diff --git a/src/parsers/directive.parser.ts b/src/parsers/directive.parser.ts index bbb4baf..a70a6f4 100644 --- a/src/parsers/directive.parser.ts +++ b/src/parsers/directive.parser.ts @@ -18,7 +18,8 @@ export class DirectiveParser extends AbstractTemplateParser implements ParserInt template = this._normalizeTemplateAttributes(template); $(template) - .find('[translate]') + .find('[translate],[ng2-translate]') + .addBack() .each((i: number, element: CheerioElement) => { const $element = $(element); const attr = $element.attr('translate'); diff --git a/tests/abstract-template.parser.spec.ts b/tests/abstract-template.parser.spec.ts new file mode 100644 index 0000000..2ae2b92 --- /dev/null +++ b/tests/abstract-template.parser.spec.ts @@ -0,0 +1,62 @@ +import { expect } from 'chai'; + +import { AbstractTemplateParser } from '../src/parsers/abstract-template.parser'; + +class TestTemplateParser extends AbstractTemplateParser { + + public isAngularComponent(filePath: string): boolean { + return this._isAngularComponent(filePath); + } + + public normalizeTemplateAttributes(template: string): string { + return this._normalizeTemplateAttributes(template); + } + + public extractInlineTemplate(contents: string): string { + return this._extractInlineTemplate(contents); + } + +} + +describe('AbstractTemplateParser', () => { + + let parser: TestTemplateParser; + + beforeEach(() => { + parser = new TestTemplateParser(); + }); + + it('should recognize js extension as angular component', () => { + const result = parser.isAngularComponent('test.js'); + expect(result).to.equal(true); + }); + + it('should recognize ts extension as angular component', () => { + const result = parser.isAngularComponent('test.ts'); + expect(result).to.equal(true); + }); + + it('should not recognize html extension as angular component', () => { + const result = parser.isAngularComponent('test.html'); + expect(result).to.equal(false); + }); + + it('should extract inline template', () => { + const contents = ` + @Component({ + selector: 'test', + template: '
Hello World
' + }) + export class TestComponent { } + `; + const template = parser.extractInlineTemplate(contents); + expect(template).to.equal('Hello World
'); + }); + + it('should normalize bound attributes', () => { + const contents = `Hello World
`; + const template = parser.normalizeTemplateAttributes(contents); + expect(template).to.equal('Hello World
'); + }); + +}); diff --git a/tests/directive.parser.spec.ts b/tests/directive.parser.spec.ts new file mode 100644 index 0000000..0ca51de --- /dev/null +++ b/tests/directive.parser.spec.ts @@ -0,0 +1,83 @@ +import { expect } from 'chai'; + +import { DirectiveParser } from '../src/parsers/directive.parser'; + +describe('DirectiveParser', () => { + + const templateFilename: string = 'test.template.html'; + const componentFilename: string = 'test.component.ts'; + + let parser: DirectiveParser; + + beforeEach(() => { + parser = new DirectiveParser(); + }); + + it('should extract contents when no translate attribute value is provided', () => { + const contents = 'Hi there
+Lorem Ipsum
+Hello World
' + }) + export class TestComponent { } + `; + const messages = parser.process(componentFilename, contents); + expect(messages).to.deep.equal(['Hello World']); + }); + +});