Refactor code to comply with ESLint rules

Major refactoring using ESLint with rules from AirBnb and Vue.

Enable most of the ESLint rules and do necessary linting in the code.
Also add more information for rules that are disabled to describe what
they are and why they are disabled.

Allow logging (`console.log`) in test files, and in development mode
(e.g. when working with `npm run serve`), but disable it when
environment is production (as pre-configured by Vue). Also add flag
(`--mode production`) in `lint:eslint` command so production linting is
executed earlier in lifecycle.

Disable rules that requires a separate work. Such as ESLint rules that
are broken in TypeScript: no-useless-constructor (eslint/eslint#14118)
and no-shadow (eslint/eslint#13014).
This commit is contained in:
undergroundwires
2022-01-02 18:20:14 +01:00
parent 96265b75de
commit 5b1fbe1e2f
341 changed files with 16126 additions and 15101 deletions

View File

@@ -4,44 +4,47 @@ import { IProjectInformation } from './IProjectInformation';
import { OperatingSystem } from './OperatingSystem';
export class Application implements IApplication {
constructor(public info: IProjectInformation, public collections: readonly ICategoryCollection[]) {
validateInformation(info);
validateCollections(collections);
}
constructor(
public info: IProjectInformation,
public collections: readonly ICategoryCollection[],
) {
validateInformation(info);
validateCollections(collections);
}
public getSupportedOsList(): OperatingSystem[] {
return this.collections.map((collection) => collection.os);
}
public getSupportedOsList(): OperatingSystem[] {
return this.collections.map((collection) => collection.os);
}
public getCollection(operatingSystem: OperatingSystem): ICategoryCollection | undefined {
return this.collections.find((collection) => collection.os === operatingSystem);
}
public getCollection(operatingSystem: OperatingSystem): ICategoryCollection | undefined {
return this.collections.find((collection) => collection.os === operatingSystem);
}
}
function validateInformation(info: IProjectInformation) {
if (!info) {
throw new Error('undefined project information');
}
if (!info) {
throw new Error('undefined project information');
}
}
function validateCollections(collections: readonly ICategoryCollection[]) {
if (!collections) {
throw new Error('undefined collections');
}
if (collections.length === 0) {
throw new Error('no collection in the list');
}
if (collections.filter((c) => !c).length > 0) {
throw new Error('undefined collection in the list');
}
const osList = collections.map((c) => c.os);
const duplicates = getDuplicates(osList);
if (duplicates.length > 0) {
throw new Error('multiple collections with same os: ' +
duplicates.map((os) => OperatingSystem[os].toLowerCase()).join('", "'));
}
if (!collections) {
throw new Error('undefined collections');
}
if (collections.length === 0) {
throw new Error('no collection in the list');
}
if (collections.filter((c) => !c).length > 0) {
throw new Error('undefined collection in the list');
}
const osList = collections.map((c) => c.os);
const duplicates = getDuplicates(osList);
if (duplicates.length > 0) {
throw new Error(`multiple collections with same os: ${
duplicates.map((os) => OperatingSystem[os].toLowerCase()).join('", "')}`);
}
}
function getDuplicates(list: readonly OperatingSystem[]): OperatingSystem[] {
return list.filter((os, index) => list.indexOf(os) !== index);
return list.filter((os, index) => list.indexOf(os) !== index);
}

View File

@@ -3,40 +3,46 @@ import { IScript } from './IScript';
import { ICategory } from './ICategory';
export class Category extends BaseEntity<number> implements ICategory {
private allSubScripts: ReadonlyArray<IScript> = undefined;
private allSubScripts: ReadonlyArray<IScript> = undefined;
constructor(
id: number,
public readonly name: string,
public readonly documentationUrls: ReadonlyArray<string>,
public readonly subCategories?: ReadonlyArray<ICategory>,
public readonly scripts?: ReadonlyArray<IScript>) {
super(id);
validateCategory(this);
}
constructor(
id: number,
public readonly name: string,
public readonly documentationUrls: ReadonlyArray<string>,
public readonly subCategories?: ReadonlyArray<ICategory>,
public readonly scripts?: ReadonlyArray<IScript>,
) {
super(id);
validateCategory(this);
}
public includes(script: IScript): boolean {
return this.getAllScriptsRecursively().some((childScript) => childScript.id === script.id);
}
public includes(script: IScript): boolean {
return this.getAllScriptsRecursively().some((childScript) => childScript.id === script.id);
}
public getAllScriptsRecursively(): readonly IScript[] {
return this.allSubScripts || (this.allSubScripts = parseScriptsRecursively(this));
public getAllScriptsRecursively(): readonly IScript[] {
if (!this.allSubScripts) {
this.allSubScripts = parseScriptsRecursively(this);
}
return this.allSubScripts;
}
}
function parseScriptsRecursively(category: ICategory): ReadonlyArray<IScript> {
return [
...category.scripts,
...category.subCategories.flatMap((c) => c.getAllScriptsRecursively()),
];
return [
...category.scripts,
...category.subCategories.flatMap((c) => c.getAllScriptsRecursively()),
];
}
function validateCategory(category: ICategory) {
if (!category.name) {
throw new Error('undefined or empty name');
}
if ((!category.subCategories || category.subCategories.length === 0) &&
(!category.scripts || category.scripts.length === 0)) {
throw new Error('A category must have at least one sub-category or script');
}
if (!category.name) {
throw new Error('undefined or empty name');
}
if (
(!category.subCategories || category.subCategories.length === 0)
&& (!category.scripts || category.scripts.length === 0)
) {
throw new Error('A category must have at least one sub-category or script');
}
}

View File

@@ -8,149 +8,159 @@ import { IScriptingDefinition } from './IScriptingDefinition';
import { ICategoryCollection } from './ICategoryCollection';
export class CategoryCollection implements ICategoryCollection {
public get totalScripts(): number { return this.queryable.allScripts.length; }
public get totalCategories(): number { return this.queryable.allCategories.length; }
public get totalScripts(): number { return this.queryable.allScripts.length; }
private readonly queryable: IQueryableCollection;
public get totalCategories(): number { return this.queryable.allCategories.length; }
constructor(
public readonly os: OperatingSystem,
public readonly actions: ReadonlyArray<ICategory>,
public readonly scripting: IScriptingDefinition) {
if (!scripting) {
throw new Error('undefined scripting definition');
}
this.queryable = makeQueryable(actions);
assertInRange(os, OperatingSystem);
ensureValid(this.queryable);
ensureNoDuplicates(this.queryable.allCategories);
ensureNoDuplicates(this.queryable.allScripts);
private readonly queryable: IQueryableCollection;
constructor(
public readonly os: OperatingSystem,
public readonly actions: ReadonlyArray<ICategory>,
public readonly scripting: IScriptingDefinition,
) {
if (!scripting) {
throw new Error('undefined scripting definition');
}
this.queryable = makeQueryable(actions);
assertInRange(os, OperatingSystem);
ensureValid(this.queryable);
ensureNoDuplicates(this.queryable.allCategories);
ensureNoDuplicates(this.queryable.allScripts);
}
public findCategory(categoryId: number): ICategory | undefined {
return this.queryable.allCategories.find((category) => category.id === categoryId);
}
public findCategory(categoryId: number): ICategory | undefined {
return this.queryable.allCategories.find((category) => category.id === categoryId);
}
public getScriptsByLevel(level: RecommendationLevel): readonly IScript[] {
if (isNaN(level)) {
throw new Error('undefined level');
}
if (!(level in RecommendationLevel)) {
throw new Error(`invalid level: ${level}`);
}
return this.queryable.scriptsByLevel.get(level);
public getScriptsByLevel(level: RecommendationLevel): readonly IScript[] {
if (level === undefined) {
throw new Error('undefined level');
}
if (!(level in RecommendationLevel)) {
throw new Error(`invalid level: ${level}`);
}
return this.queryable.scriptsByLevel.get(level);
}
public findScript(scriptId: string): IScript | undefined {
return this.queryable.allScripts.find((script) => script.id === scriptId);
}
public findScript(scriptId: string): IScript | undefined {
return this.queryable.allScripts.find((script) => script.id === scriptId);
}
public getAllScripts(): IScript[] {
return this.queryable.allScripts;
}
public getAllScripts(): IScript[] {
return this.queryable.allScripts;
}
public getAllCategories(): ICategory[] {
return this.queryable.allCategories;
}
public getAllCategories(): ICategory[] {
return this.queryable.allCategories;
}
}
function ensureNoDuplicates<TKey>(entities: ReadonlyArray<IEntity<TKey>>) {
const totalOccurrencesById = new Map<TKey, number>();
for (const entity of entities) {
totalOccurrencesById.set(entity.id, (totalOccurrencesById.get(entity.id) || 0) + 1);
}
const duplicatedIds = new Array<TKey>();
totalOccurrencesById.forEach((index, id) => {
if (index > 1) {
duplicatedIds.push(id);
}
});
if (duplicatedIds.length > 0) {
const duplicatedIdsText = duplicatedIds.map((id) => `"${id}"`).join(',');
throw new Error(
`Duplicate entities are detected with following id(s): ${duplicatedIdsText}`);
const totalOccurrencesById = new Map<TKey, number>();
for (const entity of entities) {
totalOccurrencesById.set(entity.id, (totalOccurrencesById.get(entity.id) || 0) + 1);
}
const duplicatedIds = new Array<TKey>();
totalOccurrencesById.forEach((index, id) => {
if (index > 1) {
duplicatedIds.push(id);
}
});
if (duplicatedIds.length > 0) {
const duplicatedIdsText = duplicatedIds.map((id) => `"${id}"`).join(',');
throw new Error(
`Duplicate entities are detected with following id(s): ${duplicatedIdsText}`,
);
}
}
interface IQueryableCollection {
allCategories: ICategory[];
allScripts: IScript[];
scriptsByLevel: Map<RecommendationLevel, readonly IScript[]>;
allCategories: ICategory[];
allScripts: IScript[];
scriptsByLevel: Map<RecommendationLevel, readonly IScript[]>;
}
function ensureValid(application: IQueryableCollection) {
ensureValidCategories(application.allCategories);
ensureValidScripts(application.allScripts);
ensureValidCategories(application.allCategories);
ensureValidScripts(application.allScripts);
}
function ensureValidCategories(allCategories: readonly ICategory[]) {
if (!allCategories || allCategories.length === 0) {
throw new Error('must consist of at least one category');
}
if (!allCategories || allCategories.length === 0) {
throw new Error('must consist of at least one category');
}
}
function ensureValidScripts(allScripts: readonly IScript[]) {
if (!allScripts || allScripts.length === 0) {
throw new Error('must consist of at least one script');
}
for (const level of getEnumValues(RecommendationLevel)) {
if (allScripts.every((script) => script.level !== level)) {
throw new Error(`none of the scripts are recommended as ${RecommendationLevel[level]}`);
}
if (!allScripts || allScripts.length === 0) {
throw new Error('must consist of at least one script');
}
for (const level of getEnumValues(RecommendationLevel)) {
if (allScripts.every((script) => script.level !== level)) {
throw new Error(`none of the scripts are recommended as ${RecommendationLevel[level]}`);
}
}
}
function flattenApplication(categories: ReadonlyArray<ICategory>): [ICategory[], IScript[]] {
const allCategories = new Array<ICategory>();
const allScripts = new Array<IScript>();
flattenCategories(categories, allCategories, allScripts);
return [
allCategories,
allScripts,
];
const allCategories = new Array<ICategory>();
const allScripts = new Array<IScript>();
flattenCategories(categories, allCategories, allScripts);
return [
allCategories,
allScripts,
];
}
function flattenCategories(
categories: ReadonlyArray<ICategory>,
allCategories: ICategory[],
allScripts: IScript[]): IQueryableCollection {
if (!categories || categories.length === 0) {
return;
}
for (const category of categories) {
allCategories.push(category);
flattenScripts(category.scripts, allScripts);
flattenCategories(category.subCategories, allCategories, allScripts);
}
categories: ReadonlyArray<ICategory>,
allCategories: ICategory[],
allScripts: IScript[],
): IQueryableCollection {
if (!categories || categories.length === 0) {
return;
}
for (const category of categories) {
allCategories.push(category);
flattenScripts(category.scripts, allScripts);
flattenCategories(category.subCategories, allCategories, allScripts);
}
}
function flattenScripts(
scripts: ReadonlyArray<IScript>,
allScripts: IScript[]): IScript[] {
if (!scripts) {
return;
}
for (const script of scripts) {
allScripts.push(script);
}
scripts: ReadonlyArray<IScript>,
allScripts: IScript[],
): IScript[] {
if (!scripts) {
return;
}
for (const script of scripts) {
allScripts.push(script);
}
}
function makeQueryable(
actions: ReadonlyArray<ICategory>): IQueryableCollection {
const flattened = flattenApplication(actions);
return {
allCategories: flattened[0],
allScripts: flattened[1],
scriptsByLevel: groupByLevel(flattened[1]),
};
actions: ReadonlyArray<ICategory>,
): IQueryableCollection {
const flattened = flattenApplication(actions);
return {
allCategories: flattened[0],
allScripts: flattened[1],
scriptsByLevel: groupByLevel(flattened[1]),
};
}
function groupByLevel(allScripts: readonly IScript[]): Map<RecommendationLevel, readonly IScript[]> {
const map = new Map<RecommendationLevel, readonly IScript[]>();
for (const levelName of getEnumNames(RecommendationLevel)) {
const level = RecommendationLevel[levelName];
const scripts = allScripts.filter((script) => script.level !== undefined && script.level <= level);
map.set(level, scripts);
}
return map;
function groupByLevel(
allScripts: readonly IScript[],
): Map<RecommendationLevel, readonly IScript[]> {
const map = new Map<RecommendationLevel, readonly IScript[]>();
for (const levelName of getEnumNames(RecommendationLevel)) {
const level = RecommendationLevel[levelName];
const scripts = allScripts.filter(
(script) => script.level !== undefined && script.level <= level,
);
map.set(level, scripts);
}
return map;
}

View File

@@ -3,9 +3,9 @@ import { IProjectInformation } from './IProjectInformation';
import { OperatingSystem } from './OperatingSystem';
export interface IApplication {
readonly info: IProjectInformation;
readonly collections: readonly ICategoryCollection[];
readonly info: IProjectInformation;
readonly collections: readonly ICategoryCollection[];
getSupportedOsList(): OperatingSystem[];
getCollection(operatingSystem: OperatingSystem): ICategoryCollection | undefined;
getSupportedOsList(): OperatingSystem[];
getCollection(operatingSystem: OperatingSystem): ICategoryCollection | undefined;
}

View File

@@ -3,12 +3,12 @@ import { IScript } from './IScript';
import { IDocumentable } from './IDocumentable';
export interface ICategory extends IEntity<number>, IDocumentable {
readonly id: number;
readonly name: string;
readonly subCategories?: ReadonlyArray<ICategory>;
readonly scripts?: ReadonlyArray<IScript>;
includes(script: IScript): boolean;
getAllScriptsRecursively(): ReadonlyArray<IScript>;
readonly id: number;
readonly name: string;
readonly subCategories?: ReadonlyArray<ICategory>;
readonly scripts?: ReadonlyArray<IScript>;
includes(script: IScript): boolean;
getAllScriptsRecursively(): ReadonlyArray<IScript>;
}
export { IEntity } from '../infrastructure/Entity/IEntity';

View File

@@ -5,15 +5,15 @@ import { IScript } from '@/domain/IScript';
import { ICategory } from '@/domain/ICategory';
export interface ICategoryCollection {
readonly scripting: IScriptingDefinition;
readonly os: OperatingSystem;
readonly totalScripts: number;
readonly totalCategories: number;
readonly actions: ReadonlyArray<ICategory>;
readonly scripting: IScriptingDefinition;
readonly os: OperatingSystem;
readonly totalScripts: number;
readonly totalCategories: number;
readonly actions: ReadonlyArray<ICategory>;
getScriptsByLevel(level: RecommendationLevel): ReadonlyArray<IScript>;
findCategory(categoryId: number): ICategory | undefined;
findScript(scriptId: string): IScript | undefined;
getAllScripts(): ReadonlyArray<IScript>;
getAllCategories(): ReadonlyArray<ICategory>;
getScriptsByLevel(level: RecommendationLevel): ReadonlyArray<IScript>;
findCategory(categoryId: number): ICategory | undefined;
findScript(scriptId: string): IScript | undefined;
getAllScripts(): ReadonlyArray<IScript>;
getAllCategories(): ReadonlyArray<ICategory>;
}

View File

@@ -1,3 +1,3 @@
export interface IDocumentable {
readonly documentationUrls: ReadonlyArray<string>;
readonly documentationUrls: ReadonlyArray<string>;
}

View File

@@ -1,11 +1,12 @@
import { OperatingSystem } from './OperatingSystem';
export interface IProjectInformation {
readonly name: string;
readonly version: string;
readonly repositoryUrl: string;
readonly homepage: string;
readonly feedbackUrl: string;
readonly releaseUrl: string;
readonly repositoryWebUrl: string;
getDownloadUrl(os: OperatingSystem): string;
readonly name: string;
readonly version: string;
readonly repositoryUrl: string;
readonly homepage: string;
readonly feedbackUrl: string;
readonly releaseUrl: string;
readonly repositoryWebUrl: string;
getDownloadUrl(os: OperatingSystem): string;
}

View File

@@ -4,9 +4,9 @@ import { RecommendationLevel } from './RecommendationLevel';
import { IScriptCode } from './IScriptCode';
export interface IScript extends IEntity<string>, IDocumentable {
readonly name: string;
readonly level?: RecommendationLevel;
readonly documentationUrls: ReadonlyArray<string>;
readonly code: IScriptCode;
canRevert(): boolean;
readonly name: string;
readonly level?: RecommendationLevel;
readonly documentationUrls: ReadonlyArray<string>;
readonly code: IScriptCode;
canRevert(): boolean;
}

View File

@@ -1,4 +1,4 @@
export interface IScriptCode {
readonly execute: string;
readonly revert: string;
readonly execute: string;
readonly revert: string;
}

View File

@@ -1,8 +1,8 @@
import { ScriptingLanguage } from './ScriptingLanguage';
export interface IScriptingDefinition {
readonly fileExtension: string;
readonly language: ScriptingLanguage;
readonly startCode: string;
readonly endCode: string;
readonly fileExtension: string;
readonly language: ScriptingLanguage;
readonly startCode: string;
readonly endCode: string;
}

View File

@@ -1,13 +1,13 @@
export enum OperatingSystem {
macOS,
Windows,
Linux,
KaiOS,
ChromeOS,
BlackBerryOS,
BlackBerry,
BlackBerryTabletOS,
Android,
iOS,
WindowsPhone,
macOS,
Windows,
Linux,
KaiOS,
ChromeOS,
BlackBerryOS,
BlackBerry,
BlackBerryTabletOS,
Android,
iOS,
WindowsPhone,
}

View File

@@ -1,57 +1,61 @@
import { assertInRange } from '@/application/Common/Enum';
import { IProjectInformation } from './IProjectInformation';
import { OperatingSystem } from './OperatingSystem';
import { assertInRange } from '@/application/Common/Enum';
export class ProjectInformation implements IProjectInformation {
public readonly repositoryWebUrl: string;
constructor(
public readonly name: string,
public readonly version: string,
public readonly repositoryUrl: string,
public readonly homepage: string,
) {
if (!name) {
throw new Error('name is undefined');
}
if (!version || +version <= 0) {
throw new Error('version should be higher than zero');
}
if (!repositoryUrl) {
throw new Error('repositoryUrl is undefined');
}
if (!homepage) {
throw new Error('homepage is undefined');
}
this.repositoryWebUrl = getWebUrl(this.repositoryUrl);
public readonly repositoryWebUrl: string;
constructor(
public readonly name: string,
public readonly version: string,
public readonly repositoryUrl: string,
public readonly homepage: string,
) {
if (!name) {
throw new Error('name is undefined');
}
public getDownloadUrl(os: OperatingSystem): string {
return `${this.repositoryWebUrl}/releases/download/${this.version}/${getFileName(os, this.version)}`;
if (!version || +version <= 0) {
throw new Error('version should be higher than zero');
}
public get feedbackUrl(): string {
return `${this.repositoryWebUrl}/issues`;
if (!repositoryUrl) {
throw new Error('repositoryUrl is undefined');
}
public get releaseUrl(): string {
return `${this.repositoryWebUrl}/releases/tag/${this.version}`;
if (!homepage) {
throw new Error('homepage is undefined');
}
this.repositoryWebUrl = getWebUrl(this.repositoryUrl);
}
public getDownloadUrl(os: OperatingSystem): string {
return `${this.repositoryWebUrl}/releases/download/${this.version}/${getFileName(os, this.version)}`;
}
public get feedbackUrl(): string {
return `${this.repositoryWebUrl}/issues`;
}
public get releaseUrl(): string {
return `${this.repositoryWebUrl}/releases/tag/${this.version}`;
}
}
function getWebUrl(gitUrl: string) {
if (gitUrl.endsWith('.git')) {
return gitUrl.substring(0, gitUrl.length - 4);
}
return gitUrl;
if (gitUrl.endsWith('.git')) {
return gitUrl.substring(0, gitUrl.length - 4);
}
return gitUrl;
}
function getFileName(os: OperatingSystem, version: string): string {
assertInRange(os, OperatingSystem);
switch (os) {
case OperatingSystem.Linux:
return `privacy.sexy-${version}.AppImage`;
case OperatingSystem.macOS:
return `privacy.sexy-${version}.dmg`;
case OperatingSystem.Windows:
return `privacy.sexy-Setup-${version}.exe`;
default:
throw new RangeError(`Unsupported os: ${OperatingSystem[os]}`);
}
assertInRange(os, OperatingSystem);
switch (os) {
case OperatingSystem.Linux:
return `privacy.sexy-${version}.AppImage`;
case OperatingSystem.macOS:
return `privacy.sexy-${version}.dmg`;
case OperatingSystem.Windows:
return `privacy.sexy-Setup-${version}.exe`;
default:
throw new RangeError(`Unsupported os: ${OperatingSystem[os]}`);
}
}

View File

@@ -1,4 +1,4 @@
export enum RecommendationLevel {
Standard = 0,
Strict = 1,
Standard = 0,
Strict = 1,
}

View File

@@ -4,24 +4,26 @@ import { RecommendationLevel } from './RecommendationLevel';
import { IScriptCode } from './IScriptCode';
export class Script extends BaseEntity<string> implements IScript {
constructor(
public readonly name: string,
public readonly code: IScriptCode,
public readonly documentationUrls: ReadonlyArray<string>,
public readonly level?: RecommendationLevel) {
super(name);
if (!code) {
throw new Error(`undefined code (script: ${name})`);
}
validateLevel(level);
}
public canRevert(): boolean {
return Boolean(this.code.revert);
constructor(
public readonly name: string,
public readonly code: IScriptCode,
public readonly documentationUrls: ReadonlyArray<string>,
public readonly level?: RecommendationLevel,
) {
super(name);
if (!code) {
throw new Error(`undefined code (script: ${name})`);
}
validateLevel(level);
}
public canRevert(): boolean {
return Boolean(this.code.revert);
}
}
function validateLevel(level?: RecommendationLevel) {
if (level !== undefined && !(level in RecommendationLevel)) {
throw new Error(`invalid level: ${level}`);
}
if (level !== undefined && !(level in RecommendationLevel)) {
throw new Error(`invalid level: ${level}`);
}
}

View File

@@ -1,81 +1,84 @@
import { IScriptCode } from './IScriptCode';
export class ScriptCode implements IScriptCode {
constructor(
public readonly execute: string,
public readonly revert: string,
syntax: ILanguageSyntax) {
if (!syntax) { throw new Error('undefined syntax'); }
validateCode(execute, syntax);
validateRevertCode(revert, execute, syntax);
}
constructor(
public readonly execute: string,
public readonly revert: string,
syntax: ILanguageSyntax,
) {
if (!syntax) { throw new Error('undefined syntax'); }
validateCode(execute, syntax);
validateRevertCode(revert, execute, syntax);
}
}
export interface ILanguageSyntax {
readonly commentDelimiters: string[];
readonly commonCodeParts: string[];
readonly commentDelimiters: string[];
readonly commonCodeParts: string[];
}
function validateRevertCode(revertCode: string, execute: string, syntax: ILanguageSyntax) {
if (!revertCode) {
return;
}
try {
validateCode(revertCode, syntax);
if (execute === revertCode) {
throw new Error(`Code itself and its reverting code cannot be the same`);
}
} catch (err) {
throw Error(`(revert): ${err.message}`);
if (!revertCode) {
return;
}
try {
validateCode(revertCode, syntax);
if (execute === revertCode) {
throw new Error('Code itself and its reverting code cannot be the same');
}
} catch (err) {
throw Error(`(revert): ${err.message}`);
}
}
function validateCode(code: string, syntax: ILanguageSyntax): void {
if (!code || code.length === 0) {
throw new Error(`code is empty or undefined`);
}
ensureNoEmptyLines(code);
ensureCodeHasUniqueLines(code, syntax);
if (!code || code.length === 0) {
throw new Error('code is empty or undefined');
}
ensureNoEmptyLines(code);
ensureCodeHasUniqueLines(code, syntax);
}
function ensureNoEmptyLines(code: string): void {
const lines = code.split(/\r\n|\r|\n/);
if (lines.some((line) => line.trim().length === 0)) {
throw Error(`Script has empty lines:\n${lines.map((part, index) => `\n (${index}) ${part || '❌'}`).join('')}`);
}
const lines = code.split(/\r\n|\r|\n/);
if (lines.some((line) => line.trim().length === 0)) {
throw Error(`Script has empty lines:\n${lines.map((part, index) => `\n (${index}) ${part || '❌'}`).join('')}`);
}
}
function ensureCodeHasUniqueLines(code: string, syntax: ILanguageSyntax): void {
const allLines = code.split(/\r\n|\r|\n/);
const checkedLines = allLines.filter((line) => !shouldIgnoreLine(line, syntax));
if (checkedLines.length === 0) {
return;
}
const duplicateLines = checkedLines.filter((e, i, a) => a.indexOf(e) !== i);
if (duplicateLines.length !== 0) {
throw Error(`Duplicates detected in script:\n${printDuplicatedLines(allLines)}`);
}
const allLines = code.split(/\r\n|\r|\n/);
const checkedLines = allLines.filter((line) => !shouldIgnoreLine(line, syntax));
if (checkedLines.length === 0) {
return;
}
const duplicateLines = checkedLines.filter((e, i, a) => a.indexOf(e) !== i);
if (duplicateLines.length !== 0) {
throw Error(`Duplicates detected in script:\n${printDuplicatedLines(allLines)}`);
}
}
function printDuplicatedLines(allLines: string[]) {
return allLines
.map((line, index) => {
const occurrenceIndices = allLines
.map((e, i) => e === line ? i : '')
.filter(String);
const isDuplicate = occurrenceIndices.length > 1;
const indicator = isDuplicate ? `❌ (${occurrenceIndices.join(',')})\t` : '✅ ';
return `${indicator}[${index}] ${line}`;
})
.join('\n');
return allLines
.map((line, index) => {
const occurrenceIndices = allLines
.map((e, i) => (e === line ? i : ''))
.filter(String);
const isDuplicate = occurrenceIndices.length > 1;
const indicator = isDuplicate ? `❌ (${occurrenceIndices.join(',')})\t` : '✅ ';
return `${indicator}[${index}] ${line}`;
})
.join('\n');
}
function shouldIgnoreLine(codeLine: string, syntax: ILanguageSyntax): boolean {
codeLine = codeLine.toLowerCase();
const isCommentLine = () => syntax.commentDelimiters.some((delimiter) => codeLine.startsWith(delimiter));
const consistsOfFrequentCommands = () => {
const trimmed = codeLine.trim().split(' ');
return trimmed.every((part) => syntax.commonCodeParts.includes(part));
};
return isCommentLine() || consistsOfFrequentCommands();
const lowerCaseCodeLine = codeLine.toLowerCase();
const isCommentLine = () => syntax.commentDelimiters.some(
(delimiter) => lowerCaseCodeLine.startsWith(delimiter),
);
const consistsOfFrequentCommands = () => {
const trimmed = lowerCaseCodeLine.trim().split(' ');
return trimmed.every((part) => syntax.commonCodeParts.includes(part));
};
return isCommentLine() || consistsOfFrequentCommands();
}

View File

@@ -2,31 +2,32 @@ import { ScriptingLanguage } from './ScriptingLanguage';
import { IScriptingDefinition } from './IScriptingDefinition';
export class ScriptingDefinition implements IScriptingDefinition {
public readonly fileExtension: string;
constructor(
public readonly language: ScriptingLanguage,
public readonly startCode: string,
public readonly endCode: string,
) {
this.fileExtension = findExtension(language);
validateCode(startCode, 'start code');
validateCode(endCode, 'end code');
}
public readonly fileExtension: string;
constructor(
public readonly language: ScriptingLanguage,
public readonly startCode: string,
public readonly endCode: string,
) {
this.fileExtension = findExtension(language);
validateCode(startCode, 'start code');
validateCode(endCode, 'end code');
}
}
function findExtension(language: ScriptingLanguage): string {
switch (language) {
case ScriptingLanguage.shellscript:
return 'sh';
case ScriptingLanguage.batchfile:
return 'bat';
default:
throw new Error(`unsupported language: ${language}`);
}
switch (language) {
case ScriptingLanguage.shellscript:
return 'sh';
case ScriptingLanguage.batchfile:
return 'bat';
default:
throw new Error(`unsupported language: ${language}`);
}
}
function validateCode(code: string, name: string) {
if (!code) {
throw new Error(`undefined ${name}`);
}
if (!code) {
throw new Error(`undefined ${name}`);
}
}

View File

@@ -1,4 +1,4 @@
export enum ScriptingLanguage {
batchfile = 0,
shellscript = 1,
batchfile = 0,
shellscript = 1,
}