diff --git a/.eslintrc.yml b/.eslintrc.yml index 80c14bbfd..b5955c992 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -162,6 +162,9 @@ rules: unicorn/prefer-switch: off unicorn/no-static-only-class: off unicorn/no-process-exit: off + unicorn/no-useless-undefined: + - error + - checkArguments: false '@typescript-eslint/no-empty-function': off '@typescript-eslint/no-unused-vars': off '@typescript-eslint/no-explicit-any': off diff --git a/lib/base-compiler.ts b/lib/base-compiler.ts index 62c567e4a..955ae40b8 100644 --- a/lib/base-compiler.ts +++ b/lib/base-compiler.ts @@ -57,6 +57,7 @@ import {Artifact, ToolResult, ToolTypeKey} from '../types/tool.interfaces'; import {BuildEnvSetupBase, getBuildEnvTypeByKey} from './buildenvsetup'; import {BuildEnvDownloadInfo} from './buildenvsetup/buildenv.interfaces'; import * as cfg from './cfg'; +import {CompilationEnvironment} from './compilation-env'; import {CompilerArguments} from './compiler-arguments'; import {ClangParser, GCCParser} from './compilers/argument-parsers'; import {getDemanglerTypeByKey} from './demangler'; @@ -84,7 +85,7 @@ export class BaseCompiler implements ICompiler { protected compiler: CompilerInfo & Record; // TODO: Some missing types still present in Compiler type public lang: Language; protected compileFilename: string; - protected env: any; + protected env: CompilationEnvironment; protected compilerProps: PropertyGetter; protected alwaysResetLdPath: any; protected delayCleanupTemp: any; @@ -113,7 +114,7 @@ export class BaseCompiler implements ICompiler { labelNames: [], }); - constructor(compilerInfo: CompilerInfo & Record, env) { + constructor(compilerInfo: CompilerInfo & Record, env: CompilationEnvironment) { // Information about our compiler this.compiler = compilerInfo; this.lang = languages[compilerInfo.lang]; @@ -123,7 +124,7 @@ export class BaseCompiler implements ICompiler { this.compileFilename = `example${this.lang.extensions[0]}`; this.env = env; // Partial application of compilerProps with the proper language id applied to it - this.compilerProps = _.partial(this.env.compilerProps, this.lang.id); + this.compilerProps = _.partial(this.env.compilerProps as any, this.lang.id); this.compiler.supportsIntel = !!this.compiler.intelAsm; this.alwaysResetLdPath = this.env.ceProps('alwaysResetLdPath'); @@ -316,12 +317,12 @@ export class BaseCompiler implements ICompiler { } const key = this.getCompilerCacheKey(compiler, args, options); - let result = await this.env.compilerCacheGet(key); + let result = await this.env.compilerCacheGet(key as any); if (!result) { result = await this.env.enqueue(async () => await exec.execute(compiler, args, options)); if (result.okToCache) { this.env - .compilerCachePut(key, result) + .compilerCachePut(key as any, result, undefined) .then(() => { // Do nothing, but we don't await here. }) @@ -2370,7 +2371,7 @@ export class BaseCompiler implements ICompiler { result = this.postCompilationPreCacheHook(result); if (result.okToCache) { - await this.env.cachePut(key, result); + await this.env.cachePut(key, result, undefined); } if (doExecute) { @@ -2624,7 +2625,7 @@ but nothing was dumped. Possible causes are: return result; } - checkOptions(options) { + checkOptions(options: string[]) { const error = this.env.findBadOptions(options); if (error.length > 0) return `Bad options: ${error.join(', ')}`; return null; diff --git a/lib/compilation-env.js b/lib/compilation-env.ts similarity index 82% rename from lib/compilation-env.js rename to lib/compilation-env.ts index ee931adc1..dc5f9155d 100644 --- a/lib/compilation-env.js +++ b/lib/compilation-env.ts @@ -27,12 +27,31 @@ import child_process from 'child_process'; import fs from 'fs-extra'; import _ from 'underscore'; +import {CacheableValue} from '../types/cache.interfaces'; + import {BaseCache} from './cache/base'; +import {Cache} from './cache/base.interfaces'; import {createCacheFromConfig} from './cache/from-config'; +import {CompilationQueue, Job} from './compilation-queue'; import {FormattingHandler} from './handlers/formatting'; import {logger} from './logger'; +import {CompilerProps} from './properties'; +import {PropertyGetter} from './properties.interfaces'; export class CompilationEnvironment { + ceProps: PropertyGetter; + compilationQueue: CompilationQueue; + compilerProps: CompilerProps; + okOptions: RegExp; + badOptions: RegExp; + cache: Cache; + executableCache: Cache; + compilerCache: Cache; + reportCacheEvery: number; + multiarch: string | null; + baseEnv: Record; + formatHandler: FormattingHandler; + constructor(compilerProps, compilationQueue, doCache) { this.ceProps = compilerProps.ceProps; this.compilationQueue = compilationQueue; @@ -79,7 +98,7 @@ export class CompilationEnvironment { this.formatHandler = new FormattingHandler(this.ceProps); } - getEnv(needsMulti) { + getEnv(needsMulti: boolean) { const env = {...this.baseEnv}; if (needsMulti && this.multiarch) { env.LIBRARY_PATH = '/usr/lib/' + this.multiarch; @@ -89,7 +108,7 @@ export class CompilationEnvironment { return env; } - async cacheGet(object) { + async cacheGet(object: CacheableValue) { const result = await this.cache.get(BaseCache.hash(object)); if (this.cache.gets % this.reportCacheEvery === 0) { this.cache.report(); @@ -98,12 +117,12 @@ export class CompilationEnvironment { return JSON.parse(result.data); } - async cachePut(object, result, creator) { + async cachePut(object: CacheableValue, result: object, creator: string | undefined) { const key = BaseCache.hash(object); return this.cache.put(key, JSON.stringify(result), creator); } - async compilerCacheGet(object) { + async compilerCacheGet(object: CacheableValue) { const key = BaseCache.hash(object); const result = await this.compilerCache.get(key); if (this.ceProps('logCompilerCacheAccesses', false)) { @@ -113,12 +132,12 @@ export class CompilationEnvironment { return JSON.parse(result.data); } - async compilerCachePut(object, result, creator) { + async compilerCachePut(object: CacheableValue, result: object, creator: string | undefined) { const key = BaseCache.hash(object); return this.compilerCache.put(key, JSON.stringify(result), creator); } - async executableGet(object, destinationFolder) { + async executableGet(object: CacheableValue, destinationFolder: string) { const key = BaseCache.hash(object) + '_exec'; const result = await this.executableCache.get(key); if (!result.hit) return null; @@ -127,16 +146,16 @@ export class CompilationEnvironment { return filepath; } - executablePut(object, filepath) { + executablePut(object: CacheableValue, filepath: string) { const key = BaseCache.hash(object) + '_exec'; return this.executableCache.put(key, fs.readFileSync(filepath)); } - enqueue(job) { + enqueue(job: Job) { return this.compilationQueue.enqueue(job); } - findBadOptions(options) { - return _.filter(options, option => !this.okOptions.test(option) || option.match(this.badOptions)); + findBadOptions(options: string[]) { + return _.filter(options, option => !this.okOptions.test(option) || this.badOptions.test(option)); } } diff --git a/lib/compilation-queue.ts b/lib/compilation-queue.ts index 933afb67b..e05270557 100644 --- a/lib/compilation-queue.ts +++ b/lib/compilation-queue.ts @@ -42,7 +42,7 @@ const queueCompleted = new PromClient.Counter({ help: 'Total number of jobs completed', }); -type Job = () => PromiseLike; +export type Job = () => PromiseLike; export class CompilationQueue { private readonly _running: Set = new Set(); diff --git a/lib/compilers/dosbox-compiler.ts b/lib/compilers/dosbox-compiler.ts index 8ece53ded..4a19ea442 100644 --- a/lib/compilers/dosbox-compiler.ts +++ b/lib/compilers/dosbox-compiler.ts @@ -108,12 +108,12 @@ export class DosboxCompiler extends BaseCompiler { } const key = this.getCompilerCacheKey(compiler, args, options); - let result = await this.env.compilerCacheGet(key); + let result = await this.env.compilerCacheGet(key as any); if (!result) { result = await this.env.enqueue(async () => this.exec(compiler, args, options)); if (result.okToCache) { this.env - .compilerCachePut(key, result) + .compilerCachePut(key as any, result, undefined) .then(() => { // Do nothing, but we don't await here. }) diff --git a/lib/handlers/compile.ts b/lib/handlers/compile.ts index c96c9a732..517a53256 100644 --- a/lib/handlers/compile.ts +++ b/lib/handlers/compile.ts @@ -123,7 +123,7 @@ export class CompileHandler { constructor(compilationEnvironment: CompilationEnvironment, awsProps: (name: string, def?: string) => string) { this.compilerEnv = compilationEnvironment; - this.textBanner = this.compilerEnv.ceProps('textBanner'); + this.textBanner = this.compilerEnv.ceProps('textBanner'); this.proxy = Server.createProxyServer({}); this.awsProps = awsProps; initialise(this.compilerEnv);