Skip to content

Commit

Permalink
Major TS cleanup and upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
grassick committed Nov 3, 2022
1 parent f236a83 commit 30dc743
Show file tree
Hide file tree
Showing 12 changed files with 1,705 additions and 5,413 deletions.
34 changes: 18 additions & 16 deletions lib/extractor/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
Expand Down Expand Up @@ -43,9 +47,9 @@ function updateLocalizationFile(rootDirs, dataFile, options, callback) {
localizations = {};
}
// Update localizations
return exports.updateLocalizations(rootDirs, localizations, options, function () {
updateLocalizations(rootDirs, localizations, options, function () {
fs_1.default.writeFileSync(dataFile, JSON.stringify(localizations, null, 2), "utf-8");
return callback();
callback();
});
}
exports.updateLocalizationFile = updateLocalizationFile;
Expand All @@ -65,22 +69,21 @@ function updateLocalizations(rootDirs, data, options, callback) {
data.strings = [];
}
// Get strings
return stringExtractor.findFromRootDirs(rootDirs, function (strs) {
stringExtractor.findFromRootDirs(rootDirs, function (strs) {
// Add extra strings
let loc, str;
if (options.extraStrings) {
strs = strs.concat(options.extraStrings);
}
// Create map of english
const map = {};
for (loc of data.strings) {
for (const loc of data.strings) {
map[loc.en] = loc;
}
for (str of strs) {
for (const str of strs) {
// Create item if doesn't exist
if (!map[str]) {
const string = { _base: "en", en: str };
for (loc of data.locales) {
for (const loc of data.locales) {
if (loc.code !== "en") {
string[loc.code] = "";
}
Expand All @@ -94,27 +97,26 @@ function updateLocalizations(rootDirs, data, options, callback) {
map[str]._base = "en";
}
// Just add missing languages
for (loc of data.locales) {
for (const loc of data.locales) {
if (loc.code !== "en" && map[str][loc.code] == null) {
map[str][loc.code] = "";
}
}
}
}
// Mark unused
// Gather unused
const known = {};
for (str of strs) {
for (const str of strs) {
known[str] = true;
}
const unused = [];
for (let item of data.strings) {
if (!known[item.en]) {
item._unused = true;
}
else {
delete item._unused;
unused.push(item.en);
}
}
return callback();
data.unused = unused;
callback();
});
}
exports.updateLocalizations = updateLocalizations;
8 changes: 4 additions & 4 deletions lib/extractor/stringExtractor.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export declare function findFromRootDirs(rootDirs: any, callback: any): any;
export declare function findFromRootDirs(rootDirs: string[], callback: (strs: string[]) => void): void;
export declare function findInJs(this: any, js: any): any;
export declare function findInHbs(hbs: any): string[];
export declare function findInTs(ts: any): any;
export declare function findInTsx(tsx: any): any;
export declare function findInHbs(hbs: string): string[];
export declare function findInTs(ts: string): any;
export declare function findInTsx(tsx: string): any;
42 changes: 25 additions & 17 deletions lib/extractor/stringExtractor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
Expand Down Expand Up @@ -59,26 +63,26 @@ function findFromRootDirs(rootDirs, callback) {
const ext = path_1.default.extname(fullFilename);
switch (ext) {
case ".js":
strings = strings.concat(exports.findInJs(contents));
strings = strings.concat(findInJs(contents));
break;
case ".hbs":
strings = strings.concat(exports.findInHbs(contents));
strings = strings.concat(findInHbs(contents));
break;
case ".ts":
strings = strings.concat(exports.findInTs(contents));
strings = strings.concat(findInTs(contents));
break;
case ".tsx":
strings = strings.concat(exports.findInTsx(contents));
strings = strings.concat(findInTsx(contents));
break;
}
}
}
return callback(strings);
callback(strings);
}
exports.findFromRootDirs = findFromRootDirs;
function findInJs(js) {
const items = [];
walk.simple(acorn.parse(js), {
walk.simple(acorn.parse(js, { ecmaVersion: "latest" }), {
CallExpression: function (node) {
var _a, _b, _c, _d, _e, _f, _g;
if (((_a = node.callee) === null || _a === void 0 ? void 0 : _a.name) === "T" && typeof ((_b = node.arguments[0]) === null || _b === void 0 ? void 0 : _b.value) === "string") {
Expand All @@ -94,16 +98,20 @@ function findInJs(js) {
exports.findInJs = findInJs;
function findInHbsProgramNode(node) {
let items = [];
for (let stat of node.statements) {
if (stat.type === "mustache" && stat.id.string === "T") {
items.push(stat.params[0].string);
for (let stat of node.body) {
if (stat.type === "MustacheStatement") {
const mushStat = stat;
if (mushStat.path.type == "PathExpression" && mushStat.path.original == "T") {
items.push(mushStat.params[0].value);
}
}
if (stat.type === "block") {
if (stat.program) {
items = items.concat(findInHbsProgramNode(stat.program));
if (stat.type === "BlockStatement") {
const blockStat = stat;
if (blockStat.program) {
items = items.concat(findInHbsProgramNode(blockStat.program));
}
if (stat.inverse) {
items = items.concat(findInHbsProgramNode(stat.inverse));
if (blockStat.inverse) {
items = items.concat(findInHbsProgramNode(blockStat.inverse));
}
}
}
Expand All @@ -119,7 +127,7 @@ function findInTs(ts) {
const js = typescript_1.default.transpileModule(ts, {
compilerOptions: { module: typescript_1.default.ModuleKind.CommonJS }
});
return exports.findInJs(js.outputText);
return findInJs(js.outputText);
}
exports.findInTs = findInTs;
function findInTsx(tsx) {
Expand All @@ -129,6 +137,6 @@ function findInTsx(tsx) {
jsx: typescript_1.default.JsxEmit.ReactJSX
}
});
return exports.findInJs(js.outputText);
return findInJs(js.outputText);
}
exports.findInTsx = findInTsx;
2 changes: 2 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export { LocalizedString, Locale };
export interface LocalizerData {
locales: Locale[];
strings: LocalizedString[];
/** Base strings that are unused. They are still included in strings */
unused?: string[];
}
export { default as Localizer } from './Localizer';
/** Function to localize a string */
Expand Down
Loading

0 comments on commit 30dc743

Please sign in to comment.