Skip to content

Commit

Permalink
fix: Update to eslint9
Browse files Browse the repository at this point in the history
  • Loading branch information
GZolla committed Nov 5, 2024
1 parent 2d7962d commit 21b2d79
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 336 deletions.
15 changes: 0 additions & 15 deletions .eslintrc.json

This file was deleted.

33 changes: 33 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { fileURLToPath } from 'node:url';
import { FlatCompat } from '@eslint/eslintrc';
import { includeIgnoreFile } from '@eslint/compat';
import js from '@eslint/js';
import path from 'node:path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});
const gitignorePath = path.resolve(__dirname, '.gitignore');

export default [
includeIgnoreFile(gitignorePath),
...compat.extends('brightspace/browser-config').map(config => ({
...config,
files: ['**/*.js'],
})),
...compat.extends('brightspace/testing-config').map(config => ({
...config,
files: ['test/*'],
})),
{
files: ['helpers/getLocalizeResources.js'],

rules: {
'no-console': 0,
},
},
];
2 changes: 1 addition & 1 deletion helpers/getLocalizeResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ async function shouldUseBatchFetch() {
// subsequent page navigation.

return Boolean(documentLocaleSettings.oslo.batch) && 'CacheStorage' in window;
} catch (err) {
} catch {
return false;
}

Expand Down
11 changes: 5 additions & 6 deletions lib/PluralRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ class PluralRules extends Intl.PluralRules {
return super.supportedLocalesOf(l);
}).flat();
}
#localeData;
#locale;
#type;

constructor(locales, options = {}) {
super(locales, options);
this.#locale = PluralRules.supportedLocalesOf(locales)[0];
Expand All @@ -55,18 +51,21 @@ class PluralRules extends Intl.PluralRules {
this.#localeData = localeData[this.#locale];
}
}

resolvedOptions() {
return { ...super.resolvedOptions(), ...this.#localeData?.options[this.#type] };
}

select(n) {
if (this.#localeData) {
return this.#localeData.select(n, this.#type === 'ordinal');
} else {
return super.select(n);
}
}
#localeData;

#locale;

#type;

}

Expand Down
2 changes: 1 addition & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class DocumentLocaleSettings {
if (this._htmlElem.hasAttribute(attrName)) {
try {
return JSON.parse(this._htmlElem.getAttribute(attrName));
} catch (e) {
} catch {
// swallow exception
}
}
Expand Down
101 changes: 47 additions & 54 deletions lib/localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,21 @@ const noAllowedTagsRegex = getDisallowedTagsRegex([]);

export const getLocalizeClass = (superclass = class {}) => class LocalizeClass extends superclass {

static #localizeMarkup;
static documentLocaleSettings = getDocumentLocaleSettings();

static setLocalizeMarkup(localizeMarkup) {
this.#localizeMarkup ??= localizeMarkup;
}

#connected = false;
#localeChangeCallback;
#resolveResourcesLoaded;
#resourcesPromise;
pristine = true;

async #localeChangeHandler() {
if (!this._hasResources()) return;

const resourcesPromise = this.constructor._getAllLocalizeResources(this.config);
this.#resourcesPromise = resourcesPromise;
const localizeResources = (await resourcesPromise).flat(Infinity);
// If the locale changed while resources were being fetched, abort
if (this.#resourcesPromise !== resourcesPromise) return;

const allResources = {};
const resolvedLocales = new Set();
for (const { language, resources } of localizeResources) {
for (const [name, value] of Object.entries(resources)) {
allResources[name] = { language, value };
resolvedLocales.add(language);
}
}
this.localize.resources = allResources;
this.localize.resolvedLocale = [...resolvedLocales][0];
if (resolvedLocales.size > 1) {
console.warn(`Resolved multiple locales in '${this.constructor.name || this.tagName || ''}': ${[...resolvedLocales].join(', ')}`);
}

if (this.pristine) {
this.pristine = false;
this.#resolveResourcesLoaded();
}

this.#onResourcesChange();
}

#onResourcesChange() {
if (this.#connected) {
this.dispatchEvent?.(new CustomEvent('d2l-localize-resources-change'));
this.config?.onResourcesChange?.();
this.onLocalizeResourcesChange?.();
}
}

connect() {
this.#localeChangeCallback = () => this.#localeChangeHandler();
LocalizeClass.documentLocaleSettings.addChangeListener(this.#localeChangeCallback);
this.#connected = true;
this.#localeChangeCallback();
}

disconnect() {
LocalizeClass.documentLocaleSettings.removeChangeListener(this.#localeChangeCallback);
this.#connected = false;
}

localize(name, replacements) {

const { language, value } = this.localize.resources?.[name] ?? {};
Expand Down Expand Up @@ -111,7 +62,6 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e

return formattedMessage;
}

localizeHTML(name, replacements = {}) {

const { language, value } = this.localize.resources?.[name] ?? {};
Expand All @@ -138,9 +88,17 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e

return formattedMessage;
}
static #localizeMarkup;

__resourcesLoadedPromise = new Promise(r => this.#resolveResourcesLoaded = r);
#connected = false;

#localeChangeCallback;

#resolveResourcesLoaded;

#resourcesPromise;

__resourcesLoadedPromise = new Promise(r => this.#resolveResourcesLoaded = r);
static _generatePossibleLanguages(config) {

if (config?.useBrowserLangs) return navigator.languages.map(e => e.toLowerCase()).concat('en');
Expand All @@ -153,7 +111,6 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e

return Array.from(new Set([ ...langs, 'en-us', 'en' ]));
}

static _getAllLocalizeResources(config = this.localizeConfig) {
const resourcesLoadedPromises = [];
const superCtor = Object.getPrototypeOf(this);
Expand All @@ -169,7 +126,6 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e
}
return Promise.all(resourcesLoadedPromises);
}

static async _getLocalizeResources(langs, { importFunc, osloCollection, useBrowserLangs }) {

// in dev, don't request unsupported langpacks
Expand Down Expand Up @@ -198,10 +154,47 @@ export const getLocalizeClass = (superclass = class {}) => class LocalizeClass e
}
}
}

_hasResources() {
return this.constructor.localizeConfig ? Boolean(this.constructor.localizeConfig.importFunc) : this.constructor.getLocalizeResources !== undefined;
}
async #localeChangeHandler() {
if (!this._hasResources()) return;

const resourcesPromise = this.constructor._getAllLocalizeResources(this.config);
this.#resourcesPromise = resourcesPromise;
const localizeResources = (await resourcesPromise).flat(Infinity);
// If the locale changed while resources were being fetched, abort
if (this.#resourcesPromise !== resourcesPromise) return;

const allResources = {};
const resolvedLocales = new Set();
for (const { language, resources } of localizeResources) {
for (const [name, value] of Object.entries(resources)) {
allResources[name] = { language, value };
resolvedLocales.add(language);
}
}
this.localize.resources = allResources;
this.localize.resolvedLocale = [...resolvedLocales][0];
if (resolvedLocales.size > 1) {
console.warn(`Resolved multiple locales in '${this.constructor.name || this.tagName || ''}': ${[...resolvedLocales].join(', ')}`);
}

if (this.pristine) {
this.pristine = false;
this.#resolveResourcesLoaded();
}

this.#onResourcesChange();
}

#onResourcesChange() {
if (this.#connected) {
this.dispatchEvent?.(new CustomEvent('d2l-localize-resources-change'));
this.config?.onResourcesChange?.();
this.onLocalizeResourcesChange?.();
}
}

};

Expand Down
Loading

0 comments on commit 21b2d79

Please sign in to comment.