Skip to content

Commit 237b11d

Browse files
committed
feat(website): check all supported locales in doc check script
Instead of hard-coding the list of locales, use quick-lint-js' C++ code as the source of truth for what locales should be checked. This means that check-diagnostic-messages.mjs will check new locales as they are added.
1 parent 429440f commit 237b11d

File tree

7 files changed

+63
-3
lines changed

7 files changed

+63
-3
lines changed

src/c-api.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ const qljs_web_demo_diagnostic* qljs_web_demo_lint_as_config_file(
111111
return p->lint_as_config_file();
112112
}
113113

114+
const char* const* qljs_list_locales() {
115+
static const char* const* locale_list = [] {
116+
std::size_t locale_count =
117+
quick_lint_js::translation_table_locale_count + 1;
118+
const char** locales = new const char*[locale_count + 1];
119+
120+
std::size_t i = 0;
121+
const char* l;
122+
for (l = quick_lint_js::translation_data.locale_table; *l != '\0';
123+
l += std::strlen(l) + 1, ++i) {
124+
locales[i] = l;
125+
}
126+
locales[i] = l; // Default locale (empty string).
127+
++i;
128+
QLJS_ASSERT(i == locale_count);
129+
locales[i] = nullptr; // Terminator.
130+
131+
return locales;
132+
}();
133+
return locale_list;
134+
}
135+
114136
// quick-lint-js finds bugs in JavaScript programs.
115137
// Copyright (C) 2020 Matthew "strager" Glazar
116138
//

src/quick-lint-js/c-api.h

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const qljs_web_demo_diagnostic* qljs_web_demo_lint(qljs_web_demo_document*);
3636
const qljs_web_demo_diagnostic* qljs_web_demo_lint_as_config_file(
3737
qljs_web_demo_document*);
3838

39+
// Returns a null-terminated array of null-terminated strings.
40+
const char* const* qljs_list_locales();
41+
3942
#if defined(__cplusplus)
4043
}
4144
#endif

test/test-c-api.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <gtest/gtest.h>
55
#include <quick-lint-js/c-api.h>
66
#include <quick-lint-js/char8.h>
7+
#include <quick-lint-js/translation-table.h>
78

89
namespace quick_lint_js {
910
namespace {
@@ -88,6 +89,25 @@ TEST(test_c_api_web_demo, linting_uses_config) {
8889

8990
qljs_web_demo_destroy_document(p);
9091
}
92+
93+
TEST(test_c_api, locale_list) {
94+
std::vector<std::string> locale_strings;
95+
const char* const* locales = qljs_list_locales();
96+
for (const char* const* l = locales; *l; ++l) {
97+
locale_strings.push_back(*l);
98+
}
99+
std::sort(locale_strings.begin(), locale_strings.end());
100+
101+
std::vector<std::string> expected_locale_strings;
102+
for (const char* l = translation_data.locale_table; *l != '\0';
103+
l += std::strlen(l) + 1) {
104+
expected_locale_strings.push_back(l);
105+
}
106+
expected_locale_strings.push_back("");
107+
std::sort(expected_locale_strings.begin(), expected_locale_strings.end());
108+
109+
EXPECT_EQ(locale_strings, expected_locale_strings);
110+
}
91111
}
92112
}
93113

website/check-diagnostic-messages.mjs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ProblemsError,
88
documentationDirectoryPath,
99
loadErrorDocumentationFilesAsync,
10+
qljsProcessPromise,
1011
} from "./src/error-documentation.mjs";
1112

1213
async function mainAsync() {
@@ -24,7 +25,8 @@ async function mainAsync() {
2425
);
2526
}
2627

27-
let locales = ["", "de", "en@loud", "en_US@snarky", "fr_FR", "sv_SE"];
28+
let locales = (await qljsProcessPromise).listLocales();
29+
console.log(locales); // @@@
2830
let maxLocaleLength = Math.max(...locales.map((l) => l.length));
2931

3032
let cwd = process.cwd();

website/src/error-documentation.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async function makeQLJSProcessAsync() {
3030
let process = await factory.createProcessAsync();
3131
return process;
3232
}
33-
let qljsProcessPromise = makeQLJSProcessAsync();
33+
export let qljsProcessPromise = makeQLJSProcessAsync();
3434

3535
markdownParser.renderer.rules = {
3636
...markdownParser.renderer.rules,

website/wasm/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (EMSCRIPTEN)
1515
quick-lint-js-vscode
1616
PRIVATE
1717
quick-lint-js-lib
18-
"-sEXPORTED_FUNCTIONS=[\"_qljs_web_demo_create_document\",\"_qljs_web_demo_destroy_document\",\"_qljs_web_demo_set_locale\",\"_qljs_web_demo_set_text\",\"_qljs_web_demo_set_config_text\",\"_qljs_web_demo_lint\",\"_qljs_web_demo_lint_as_config_file\",\"_malloc\",\"_free\"]"
18+
"-sEXPORTED_FUNCTIONS=[\"_qljs_list_locales\",\"_qljs_web_demo_create_document\",\"_qljs_web_demo_destroy_document\",\"_qljs_web_demo_set_locale\",\"_qljs_web_demo_set_text\",\"_qljs_web_demo_set_config_text\",\"_qljs_web_demo_lint\",\"_qljs_web_demo_lint_as_config_file\",\"_malloc\",\"_free\"]"
1919
-sASSERTIONS=0
2020
-sSTANDALONE_WASM=1
2121
-sSUPPORT_LONGJMP=1

website/wasm/quick-lint-js.js

+13
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class Process {
202202

203203
this._malloc = wrap("malloc");
204204
this._free = wrap("free");
205+
this._listLocales = wrap("qljs_list_locales");
205206
this._webDemoCreateDocument = wrap("qljs_web_demo_create_document");
206207
this._webDemoDestroyDocument = wrap("qljs_web_demo_destroy_document");
207208
this._webDemoLint = wrap("qljs_web_demo_lint");
@@ -229,6 +230,7 @@ class Process {
229230
// Make future calls crash and also reduce memory usage.
230231
this._malloc = tainted;
231232
this._free = tainted;
233+
this._listLocales = tainted;
232234
this._webDemoCreateDocument = tainted;
233235
this._webDemoDestroyDocument = tainted;
234236
this._webDemoLint = tainted;
@@ -243,6 +245,17 @@ class Process {
243245
async createDocumentForWebDemoAsync() {
244246
return new DocumentForWebDemo(this);
245247
}
248+
249+
listLocales() {
250+
let localePointerList = new Uint32Array(this._heap, this._listLocales());
251+
let locales = [];
252+
for (let i = 0; localePointerList[i] != 0; ++i) {
253+
locales.push(
254+
decodeUTF8CString(new Uint8Array(this._heap, localePointerList[i]))
255+
);
256+
}
257+
return locales;
258+
}
246259
}
247260

248261
class DocumentForWebDemo {

0 commit comments

Comments
 (0)