Skip to content

Commit

Permalink
Add more runtime guards
Browse files Browse the repository at this point in the history
  • Loading branch information
diskdance committed Jul 26, 2023
1 parent f2d9d87 commit 1e77894
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
23 changes: 16 additions & 7 deletions lib/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ function elect<T>(candidates: Partial<Record<CandidateKey, T>>, locale: string):
/**
* A wrapper around `elect()` to ensure no non-string results are returned.
*/
function electString(candidates: Candidates, locale: string): string {
function safeElect(candidates: Candidates, locale: string): string {
// Guards to ensure types at runtime
if (!isPlainObject(candidates)) {
throw new TypeError('[HanAssist] Invalid parameter. Needs to be an object.');
throw new TypeError('[HanAssist] Invalid parameter. Must be an object.');
}
if (typeof locale !== 'string') {
mw.log.warn('[HanAssist] locale parameter must be a string. Please check your code.');
locale = safelyToString(locale);
}

const result = elect(candidates, locale);
if (result === null) {

if (typeof result !== 'string') {
mw.log.warn('[HanAssist] Non-string conversion result detected. Please check your code.');
}

if (result === null) {
return '';
}

Expand All @@ -56,7 +65,7 @@ function electString(candidates: Candidates, locale: string): string {
* @returns selected value
*/
function conv(candidates: Candidates): string {
return electString(candidates, mw.config.get('wgUserLanguage'));
return safeElect(candidates, mw.config.get('wgUserLanguage'));
}

/**
Expand All @@ -65,7 +74,7 @@ function conv(candidates: Candidates): string {
* @returns selected value
*/
function convByVar(candidates: Candidates): string {
return electString(candidates, mw.config.get('wgUserVariant') ?? mw.user.options.get('variant'));
return safeElect(candidates, mw.config.get('wgUserVariant') ?? mw.user.options.get('variant'));
}

/**
Expand All @@ -79,15 +88,15 @@ function batchConv(
locale = mw.config.get('wgUserLanguage')
): Record<string, string> {
if (!isPlainObject(candidatesDict)) {
throw new TypeError('[HanAssist] Invalid parameter. Needs to be an object.');
throw new TypeError('[HanAssist] Invalid parameter. Must be an object.');
}

const result: Record<string, string> = {};

for (const key in candidatesDict) {
const candidates = candidatesDict[key];
const electionResult = isPlainObject(candidates)
? electString(candidates, locale)
? safeElect(candidates, locale)
: safelyToString(candidates);
result[key] = electionResult;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const __SHIM_UXS__: boolean;
const COMPAT: boolean;
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if (__SHIM_UXS__) {
if (COMPAT) {
// Compatibility: redirect wgULS, wgUVS and wgUXS calls to HanAssist implementation
import('./shims');
}
Expand Down
1 change: 0 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* Safely convert an object to string.
* @private
* @param val value to convert
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"preinstall": "npx only-allow pnpm",
"build": "rollup -c",
"build:compat": "SHIM_UXS=1 rollup -c",
"build:compat": "COMPAT=1 rollup -c",
"lint": "eslint .",
"type-check": "tsc --noEmit",
"test": "jest",
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { readFileSync } from 'fs';
import mwGadget from 'rollup-plugin-mediawiki-gadget';
import replace from '@rollup/plugin-replace';

const shimUXS = process.env.SHIM_UXS !== undefined;
const compat = process.env.COMPAT !== undefined;

export default defineConfig({
input: 'lib/index.ts',
Expand All @@ -22,7 +22,7 @@ export default defineConfig({
typescript(),
replace({
preventAssignment: true,
__SHIM_UXS__: JSON.stringify(shimUXS),
COMPAT: JSON.stringify(compat),
}),
mwGadget({
gadgetDef: './gadget-definition.txt',
Expand Down

0 comments on commit 1e77894

Please sign in to comment.