This repository was archived by the owner on Jan 19, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtsx.js
151 lines (130 loc) · 4.88 KB
/
tsx.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @fileoverview Tests for TSX-specific constructs
* @author James Henry <https://github.com/JamesHenry>
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const
path = require("path"),
{ Linter } = require("eslint"),
shelljs = require("shelljs"),
parser = require("../../"),
testUtils = require("../../tools/test-utils");
//------------------------------------------------------------------------------
// Setup
//------------------------------------------------------------------------------
const TSX_FIXTURES_DIR = "./tests/fixtures/tsx";
const testFiles = shelljs.find(TSX_FIXTURES_DIR)
.filter(filename => filename.indexOf(".src.tsx") > -1)
// strip off ".src.tsx"
.map(filename => filename.substring(TSX_FIXTURES_DIR.length - 1, filename.length - 8));
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("TSX", () => {
testFiles.forEach(filename => {
const code = shelljs.cat(`${path.resolve(TSX_FIXTURES_DIR, filename)}.src.tsx`);
const config = {
useJSXTextNode: true,
ecmaFeatures: {
jsx: true
}
};
test(`fixtures/${filename}.src`, testUtils.createSnapshotTestBlock(code, config));
});
describe("if the filename ends with '.tsx', enable jsx option automatically.", () => {
const linter = new Linter();
linter.defineParser("typescript-eslint-parser", parser);
test("filePath was not provided", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser"
};
const messages = linter.verify(code, config);
expect(messages).toStrictEqual([{
column: 18,
fatal: true,
line: 1,
message: "Parsing error: '>' expected.",
ruleId: null,
severity: 2,
source: "const element = <T/>"
}]);
});
test("filePath was not provided and 'jsx:true' option", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser",
parserOptions: {
ecmaFeatures: {
jsx: true
}
}
};
const messages = linter.verify(code, config);
expect(messages).toStrictEqual([]);
});
test("test.ts", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser"
};
const messages = linter.verify(code, config, { filename: "test.ts" });
expect(messages).toStrictEqual([{
column: 18,
fatal: true,
line: 1,
message: "Parsing error: '>' expected.",
ruleId: null,
severity: 2,
source: "const element = <T/>"
}]);
});
test("test.ts with 'jsx:true' option", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser",
parserOptions: {
ecmaFeatures: {
jsx: true
}
}
};
const messages = linter.verify(code, config, { filename: "test.ts" });
expect(messages).toStrictEqual([{
column: 18,
fatal: true,
line: 1,
message: "Parsing error: '>' expected.",
ruleId: null,
severity: 2,
source: "const element = <T/>"
}]);
});
test("test.tsx", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser"
};
const messages = linter.verify(code, config, { filename: "test.tsx" });
expect(messages).toStrictEqual([]);
});
test("test.tsx with 'jsx:false' option", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser",
parserOptions: {
ecmaFeatures: {
jsx: false
}
}
};
const messages = linter.verify(code, config, { filename: "test.tsx" });
expect(messages).toStrictEqual([]);
});
});
});