forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no_template_variable_assignment_rule_spec.ts
110 lines (86 loc) · 3.9 KB
/
no_template_variable_assignment_rule_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {writeFileSync} from 'fs';
import {dirname, join} from 'path';
import * as shx from 'shelljs';
import {Configuration, Linter} from 'tslint';
describe('Google3 noTemplateVariableAssignment TSLint rule', () => {
const rulesDirectory =
dirname(require.resolve('../../migrations/google3/noTemplateVariableAssignmentRule'));
let tmpDir: string;
beforeEach(() => {
tmpDir = join(process.env['TEST_TMPDIR'] !, 'google3-test');
shx.mkdir('-p', tmpDir);
writeFile('tsconfig.json', JSON.stringify({compilerOptions: {module: 'es2015'}}));
});
afterEach(() => shx.rm('-r', tmpDir));
/** Runs TSLint with the no-template-variable TSLint rule.*/
function runTSLint() {
const program = Linter.createProgram(join(tmpDir, 'tsconfig.json'));
const linter = new Linter({fix: false, rulesDirectory: [rulesDirectory]}, program);
const config =
Configuration.parseConfigFile({rules: {'no-template-variable-assignment': true}});
program.getRootFileNames().forEach(fileName => {
linter.lint(fileName, program.getSourceFile(fileName) !.getFullText(), config);
});
return linter;
}
/** Writes a file to the current temporary directory. */
function writeFile(fileName: string, content: string) {
writeFileSync(join(tmpDir, fileName), content);
}
it('should create failure for detected two-way data binding assignment', () => {
writeFile('index.ts', `
import {Component} from '@angular/core';
@Component({template: '<span *ngFor="let i of options" [(a)]="i"></span>'})
export class MyComp {}
`);
const linter = runTSLint();
const failures = linter.getResult().failures;
expect(failures.length).toBe(1);
expect(failures[0].getFileName()).toContain('index.ts');
expect(failures[0].getStartPosition().getLineAndCharacter()).toEqual({line: 3, character: 68});
expect(failures[0].getEndPosition().getLineAndCharacter()).toEqual({line: 3, character: 69});
expect(failures[0].getFailure()).toMatch(/^Found assignment to template variable./);
});
it('should create failure with correct offsets for external templates', () => {
writeFile('index.ts', `
import {Component} from '@angular/core';
@Component({templateUrl: './my-tmpl.html'})
export class MyComp {}
`);
writeFile(`my-tmpl.html`, `
<span *ngFor="let option of options" [(a)]="option"></span>
`);
const linter = runTSLint();
const failures = linter.getResult().failures;
expect(failures.length).toBe(1);
expect(failures[0].getFileName()).toContain('my-tmpl.html');
expect(failures[0].getStartPosition().getLineAndCharacter()).toEqual({line: 1, character: 50});
expect(failures[0].getEndPosition().getLineAndCharacter()).toEqual({line: 1, character: 56});
expect(failures[0].getFailure()).toMatch(/^Found assignment to template variable./);
});
it('should create failure for template variable assignment within output', () => {
writeFile('index.ts', `
import {Component} from '@angular/core';
@Component({templateUrl: './my-tmpl.html'})
export class MyComp {}
`);
writeFile(`my-tmpl.html`, `
<!-- Comment -->
<span *ngFor="let option of options" (click)="option = true"></span>
`);
const linter = runTSLint();
const failures = linter.getResult().failures;
expect(failures.length).toBe(1);
expect(failures[0].getFileName()).toContain('my-tmpl.html');
expect(failures[0].getStartPosition().getLineAndCharacter()).toEqual({line: 2, character: 52});
expect(failures[0].getEndPosition().getLineAndCharacter()).toEqual({line: 2, character: 65});
expect(failures[0].getFailure()).toMatch(/^Found assignment to template variable./);
});
});