Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Fix remaining type/lint errors and test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelidlessness committed Jan 13, 2023
1 parent 1fe2297 commit 686aed7
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 199 deletions.
23 changes: 22 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
}
],
"import/order": "warn",
"import/prefer-default-export": "off",
"no-param-reassign": "warn",
"no-restricted-syntax": [
"warn",
Expand Down Expand Up @@ -93,7 +94,13 @@
{
"files": ["./**/*.ts"],
"rules": {
"@typescript-eslint/no-unused-expressions": "off",
"no-useless-constructor": "off",
"@typescript-eslint/no-useless-constructor": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"no-empty-function": "off",
"@typescript-eslint/no-empty-function": "error",

"import/extensions": [
"error",
"ignorePackages",
Expand All @@ -107,6 +114,20 @@
}
},

{
"files": ["./test/**/*.ts"],
"rules": {
"@typescript-eslint/no-unused-expressions": "off"
}
},

{
"files": ["./**/*.d.ts"],
"rules": {
"no-unused-vars": "off"
}
},

{
"files": ["./test/**/*.ts"],
"plugins": ["vitest"],
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
"contributors": [],
"scripts": {
"start": "node app.js",
"eslint-check": "eslint app.js src/**/*.js vite.config.ts test/**/*.ts",
"eslint-fix": "eslint app.js src/**/*.js vite.config.ts test/**/*.ts --fix",
"eslint-check": "eslint app.ts src/**/*.ts vite.config.ts test/**/*.ts",
"eslint-fix": "eslint app.ts src/**/*.ts vite.config.ts test/**/*.ts --fix",
"prettier-fix": "prettier --write .",
"test": "vitest run --coverage && npm run prettier-fix && npm run eslint-fix && tsc && node update-readme-with-shield-badge.js",
"test:watch": "vitest",
"build-docs": "rimraf docs && ./node_modules/.bin/jsdoc -c jsdoc.config.js",
"develop": "DEBUG=api,transformer,markdown,language node app.js & http-server test/forms -p 8081",
"tsc": "tsc"
"develop": "DEBUG=api,transformer,markdown,language node app.ts & http-server test/forms -p 8081",
"tsc": "tsc --project . && tsc --project ./test"
},
"repository": {
"type": "git",
Expand Down
42 changes: 23 additions & 19 deletions src/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import stringDirection from 'string-direction';

import type { Tag, Subtag } from 'language-tags';

class Language {
/**
* @package
*/
export class Language {
/**
* Included for backwards compatibility, prefer:
*
Expand Down Expand Up @@ -40,7 +43,6 @@ class Language {
}

export type {
Language,
/**
* Exported for backwards compatibility, prefer:
*
Expand Down Expand Up @@ -68,25 +70,25 @@ export const parseLanguage = (

const parts = sourceLanguage.match(/^([^(]+)\((.*)\)\s*$/);

if (parts && parts.length > 2) {
description = parts[1].trim();
tag = parts[2].trim();
} else {
// First check whether `sourceLanguage` is a known IANA subtag like 'en' or 'en-GB'
const languageFromTag = getLanguageFromSubtag(
sourceLanguage.split('-')[0]
if (parts && parts.length >= 2) {
return new Language(
sourceLanguage,
parts[1].trim(),
parts[2].trim(),
directionality
);
}
// First check whether `sourceLanguage` is a known IANA subtag like 'en' or 'en-GB'
const languageFromTag = getLanguageFromSubtag(sourceLanguage.split('-')[0]);

if (languageFromTag == null) {
const { subtag } =
getLanguageFromDescription(description)?.data ?? {};
if (languageFromTag == null) {
const language = getLanguageFromDescription(description);

if (subtag != null) {
tag = subtag;
}
} else {
description = languageFromTag.descriptions()[0];
if (typeof language === 'object' && language.data.subtag != null) {
tag = language.data.subtag;
}
} else {
description = languageFromTag.descriptions()[0];
}

return new Language(sourceLanguage, description, tag, directionality);
Expand All @@ -96,13 +98,15 @@ export const parseLanguage = (
* Performs IANA search to find language object with provided description.
*/
const getLanguageFromDescription = (description: string) =>
tags.search(description).find(isLanguage);
description.trim() === ''
? ''
: tags.search(description).find(isLanguage) ?? '';

/**
* Performs IANA search to find language object with provided subtag.
*/
const getLanguageFromSubtag = (subtag: string) =>
tags.subtags(subtag).find(isLanguage) ?? null;
subtag.trim() === '' ? null : tags.subtags(subtag).find(isLanguage) ?? null;

const isLanguage = (object: Tag | Subtag) => object.data.type === 'language';

Expand Down
4 changes: 2 additions & 2 deletions test/bad-external.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('for incompatible forms that require preprocessing', () => {
return;
}

const query = q.value();
const query = q.value()!;
const ref = r.value();

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('for incompatible forms that require preprocessing', () => {
if (name !== 'query') {
const value = attr.value();

select1.attr(name, value);
select1.attr(name, value!);
}
});

Expand Down
Loading

0 comments on commit 686aed7

Please sign in to comment.