Skip to content

Commit 22f60d6

Browse files
committed
refactor(lsp): use File_Language instead of Linter_Options for test-typescript
TypeScript_Test_Unit returns a Linter_Options object. This will be problematic when we add some more things to Linter_Options which are not related to file type. Refactor TypeScript_Test_Unit to work with File_Language instead. Only create a Linter_Options object at the last minute.
1 parent 1218a41 commit 22f60d6

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

test/test-typescript-test.cpp

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ TEST(Test_TypeScript_Test, json_file_is_not_linted) {
187187
TypeScript_Test_Units units =
188188
extract_units_from_typescript_test(std::move(file), u8"hello.ts");
189189
ASSERT_EQ(units.size(), 2);
190-
std::optional<Linter_Options> options = units[1].get_linter_options();
191-
ASSERT_FALSE(options.has_value());
190+
std::optional<File_Language> language = units[1].get_language();
191+
ASSERT_FALSE(language.has_value());
192192
}
193193

194194
{
@@ -199,8 +199,8 @@ TEST(Test_TypeScript_Test, json_file_is_not_linted) {
199199
TypeScript_Test_Units units =
200200
extract_units_from_typescript_test(std::move(file), u8"hello.json");
201201
ASSERT_EQ(units.size(), 2);
202-
std::optional<Linter_Options> options = units[0].get_linter_options();
203-
ASSERT_FALSE(options.has_value());
202+
std::optional<File_Language> language = units[0].get_language();
203+
ASSERT_FALSE(language.has_value());
204204
}
205205
}
206206

@@ -213,9 +213,9 @@ TEST(Test_TypeScript_Test, typescript_file_is_linted) {
213213
TypeScript_Test_Units units =
214214
extract_units_from_typescript_test(std::move(file), u8"hello.ts");
215215
ASSERT_EQ(units.size(), 2);
216-
std::optional<Linter_Options> options = units[0].get_linter_options();
217-
ASSERT_TRUE(options.has_value());
218-
EXPECT_EQ(options->language, File_Language::typescript);
216+
std::optional<File_Language> language = units[0].get_language();
217+
ASSERT_TRUE(language.has_value());
218+
EXPECT_EQ(*language, File_Language::typescript);
219219
}
220220

221221
{
@@ -226,9 +226,9 @@ TEST(Test_TypeScript_Test, typescript_file_is_linted) {
226226
TypeScript_Test_Units units =
227227
extract_units_from_typescript_test(std::move(file), u8"hello.json");
228228
ASSERT_EQ(units.size(), 2);
229-
std::optional<Linter_Options> options = units[1].get_linter_options();
230-
ASSERT_TRUE(options.has_value());
231-
EXPECT_EQ(options->language, File_Language::typescript);
229+
std::optional<File_Language> language = units[1].get_language();
230+
ASSERT_TRUE(language.has_value());
231+
EXPECT_EQ(*language, File_Language::typescript);
232232
}
233233
}
234234

@@ -241,9 +241,9 @@ TEST(Test_TypeScript_Test, typescript_react_file_is_linted) {
241241
TypeScript_Test_Units units =
242242
extract_units_from_typescript_test(std::move(file), u8"hello.tsx");
243243
ASSERT_EQ(units.size(), 2);
244-
std::optional<Linter_Options> options = units[0].get_linter_options();
245-
ASSERT_TRUE(options.has_value());
246-
EXPECT_EQ(options->language, File_Language::typescript_jsx);
244+
std::optional<File_Language> language = units[0].get_language();
245+
ASSERT_TRUE(language.has_value());
246+
EXPECT_EQ(*language, File_Language::typescript_jsx);
247247
}
248248

249249
{
@@ -254,9 +254,9 @@ TEST(Test_TypeScript_Test, typescript_react_file_is_linted) {
254254
TypeScript_Test_Units units =
255255
extract_units_from_typescript_test(std::move(file), u8"hello.json");
256256
ASSERT_EQ(units.size(), 2);
257-
std::optional<Linter_Options> options = units[1].get_linter_options();
258-
ASSERT_TRUE(options.has_value());
259-
EXPECT_EQ(options->language, File_Language::typescript_jsx);
257+
std::optional<File_Language> language = units[1].get_language();
258+
ASSERT_TRUE(language.has_value());
259+
EXPECT_EQ(*language, File_Language::typescript_jsx);
260260
}
261261
}
262262

@@ -267,9 +267,9 @@ TEST(Test_TypeScript_Test, typescript_definition_file) {
267267
TypeScript_Test_Units units =
268268
extract_units_from_typescript_test(std::move(file), u8"hello.ts");
269269
ASSERT_EQ(units.size(), 1);
270-
std::optional<Linter_Options> options = units[0].get_linter_options();
271-
ASSERT_TRUE(options.has_value());
272-
EXPECT_EQ(options->language, File_Language::typescript_definition);
270+
std::optional<File_Language> language = units[0].get_language();
271+
ASSERT_TRUE(language.has_value());
272+
EXPECT_EQ(*language, File_Language::typescript_definition);
273273
}
274274

275275
TEST(Test_TypeScript_Test, typescript_definition_file_with_weird_extension) {
@@ -279,9 +279,9 @@ TEST(Test_TypeScript_Test, typescript_definition_file_with_weird_extension) {
279279
TypeScript_Test_Units units =
280280
extract_units_from_typescript_test(std::move(file), u8"hello.ts");
281281
ASSERT_EQ(units.size(), 1);
282-
std::optional<Linter_Options> options = units[0].get_linter_options();
283-
ASSERT_TRUE(options.has_value());
284-
EXPECT_EQ(options->language, File_Language::typescript_definition);
282+
std::optional<File_Language> language = units[0].get_language();
283+
ASSERT_TRUE(language.has_value());
284+
EXPECT_EQ(*language, File_Language::typescript_definition);
285285
}
286286

287287
TEST(Test_TypeScript_Test, javascript_file_is_linted) {
@@ -294,15 +294,15 @@ TEST(Test_TypeScript_Test, javascript_file_is_linted) {
294294
extract_units_from_typescript_test(std::move(file), u8"hello.js");
295295
ASSERT_EQ(units.size(), 2);
296296

297-
std::optional<Linter_Options> options = units[0].get_linter_options();
298-
ASSERT_TRUE(options.has_value());
297+
std::optional<File_Language> language = units[0].get_language();
298+
ASSERT_TRUE(language.has_value());
299299
// FIXME(strager): Should we only enable jsx if a @jsx directive is present?
300-
EXPECT_EQ(options->language, File_Language::javascript_jsx);
300+
EXPECT_EQ(*language, File_Language::javascript_jsx);
301301

302-
options = units[1].get_linter_options();
303-
ASSERT_TRUE(options.has_value());
302+
language = units[1].get_language();
303+
ASSERT_TRUE(language.has_value());
304304
// FIXME(strager): Should we only enable jsx if a @jsx directive is present?
305-
EXPECT_EQ(options->language, File_Language::javascript_jsx);
305+
EXPECT_EQ(*language, File_Language::javascript_jsx);
306306
}
307307
}
308308

@@ -316,13 +316,13 @@ TEST(Test_TypeScript_Test, javascript_react_file_is_linted) {
316316
extract_units_from_typescript_test(std::move(file), u8"hello.jsx");
317317
ASSERT_EQ(units.size(), 2);
318318

319-
std::optional<Linter_Options> options = units[0].get_linter_options();
320-
ASSERT_TRUE(options.has_value());
321-
EXPECT_EQ(options->language, File_Language::javascript_jsx);
319+
std::optional<File_Language> language = units[0].get_language();
320+
ASSERT_TRUE(language.has_value());
321+
EXPECT_EQ(*language, File_Language::javascript_jsx);
322322

323-
options = units[1].get_linter_options();
324-
ASSERT_TRUE(options.has_value());
325-
EXPECT_EQ(options->language, File_Language::javascript_jsx);
323+
language = units[1].get_language();
324+
ASSERT_TRUE(language.has_value());
325+
EXPECT_EQ(*language, File_Language::javascript_jsx);
326326
}
327327
}
328328

@@ -337,9 +337,9 @@ TEST(Test_TypeScript_Test, markdown_unit_is_ignored) {
337337
TypeScript_Test_Units units =
338338
extract_units_from_typescript_test(std::move(file), u8"hello.ts");
339339
ASSERT_EQ(units.size(), 3);
340-
EXPECT_TRUE(units[0].get_linter_options().has_value());
341-
EXPECT_FALSE(units[1].get_linter_options().has_value());
342-
EXPECT_TRUE(units[2].get_linter_options().has_value());
340+
EXPECT_TRUE(units[0].get_language().has_value());
341+
EXPECT_FALSE(units[1].get_language().has_value());
342+
EXPECT_TRUE(units[2].get_language().has_value());
343343
}
344344

345345
TEST(Test_TypeScript_Test, files_in_node_modules_are_ignored) {
@@ -352,7 +352,7 @@ TEST(Test_TypeScript_Test, files_in_node_modules_are_ignored) {
352352
TypeScript_Test_Units units =
353353
extract_units_from_typescript_test(std::move(file), u8"hello.js");
354354
ASSERT_EQ(units.size(), 1);
355-
EXPECT_FALSE(units[0].get_linter_options().has_value());
355+
EXPECT_FALSE(units[0].get_language().has_value());
356356
}
357357
}
358358
}

tools/test-typescript/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,12 @@ void process_test_case_file(Expected_Test_Results& expected_results,
260260

261261
for (TypeScript_Test_Unit& unit :
262262
extract_units_from_typescript_test(std::move(*raw_source), path_view)) {
263-
std::optional<Linter_Options> options = unit.get_linter_options();
264-
if (options.has_value()) {
263+
std::optional<File_Language> language = unit.get_language();
264+
if (language.has_value()) {
265265
// TODO(strager): Indicate which unit we are looking at.
266266
text_reporter.set_source(&unit.data, path);
267-
parse_and_lint(&unit.data, text_reporter, globals, *options);
267+
parse_and_lint(&unit.data, text_reporter, globals,
268+
Linter_Options{.language = *language});
268269
}
269270
}
270271
diags.flush();

tools/test-typescript/quick-lint-js/typescript-test.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include <optional>
77
#include <quick-lint-js/container/padded-string.h>
8-
#include <quick-lint-js/fe/linter.h>
8+
#include <quick-lint-js/fe/language.h>
99
#include <quick-lint-js/port/char8.h>
1010
#include <vector>
1111

@@ -15,7 +15,7 @@ struct TypeScript_Test_Unit {
1515
String8 name;
1616

1717
// Returns std::nullopt if this file should not be parsed or linted.
18-
std::optional<Linter_Options> get_linter_options() const;
18+
std::optional<File_Language> get_language() const;
1919
};
2020

2121
using TypeScript_Test_Units = std::vector<TypeScript_Test_Unit>;

tools/test-typescript/typescript-test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,27 @@ find_typescript_test_filename_metadata_directive(
126126
}
127127
}
128128

129-
std::optional<Linter_Options> TypeScript_Test_Unit::get_linter_options() const {
129+
std::optional<File_Language> TypeScript_Test_Unit::get_language() const {
130130
if (starts_with(String8_View(this->name), u8"/node_modules/"_sv)) {
131131
return std::nullopt;
132132
}
133133
if (ends_with(String8_View(this->name), u8".json"_sv)) {
134134
return std::nullopt;
135135
}
136136
if (contains(String8_View(this->name), u8".d."_sv)) {
137-
return Linter_Options{.language = File_Language::typescript_definition};
137+
return File_Language::typescript_definition;
138138
}
139139
if (ends_with(String8_View(this->name), u8".ts"_sv)) {
140-
return Linter_Options{.language = File_Language::typescript};
140+
return File_Language::typescript;
141141
}
142142
if (ends_with(String8_View(this->name), u8".tsx"_sv)) {
143-
return Linter_Options{.language = File_Language::typescript_jsx};
143+
return File_Language::typescript_jsx;
144144
}
145145
if (ends_with(String8_View(this->name), u8".js"_sv)) {
146-
return Linter_Options{.language = File_Language::javascript_jsx};
146+
return File_Language::javascript_jsx;
147147
}
148148
if (ends_with(String8_View(this->name), u8".jsx"_sv)) {
149-
return Linter_Options{.language = File_Language::javascript_jsx};
149+
return File_Language::javascript_jsx;
150150
}
151151
// Don't lint unknown file extensions. See
152152
// TypeScript/tests/cases/conformance/moduleResolution/bundler/bundlerImportTsExtensions.ts

0 commit comments

Comments
 (0)