Skip to content

Commit

Permalink
Merge pull request #10258 from keymanapp/feat/developer/10150-control…
Browse files Browse the repository at this point in the history
…-message-severity

feat(developer): add `-m` parameter to kmc to control message severity
  • Loading branch information
mcdurdin authored Jan 16, 2024
2 parents 6b79624 + 50d248f commit d2580c6
Show file tree
Hide file tree
Showing 42 changed files with 949 additions and 631 deletions.
2 changes: 2 additions & 0 deletions common/web/types/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export { defaultCompilerOptions, CompilerBaseOptions, CompilerCallbacks, Compile
compilerExceptionToString, compilerErrorFormatCode,
compilerLogLevelToSeverity, CompilerLogLevel, compilerEventFormat, ALL_COMPILER_LOG_LEVELS,
ALL_COMPILER_LOG_FORMATS, CompilerLogFormat,
CompilerMessageOverride,
CompilerMessageOverrideMap,

KeymanCompilerArtifact,
KeymanCompilerArtifactOptional,
Expand Down
68 changes: 67 additions & 1 deletion common/web/types/src/util/compiler-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ export enum CompilerErrorSeverity {
Fatal = 0x400000, // OOM or should-not-happen internal problem
};

export const CompilerErrorSeverityValues = [
CompilerErrorSeverity.Info,
CompilerErrorSeverity.Hint,
CompilerErrorSeverity.Warn,
CompilerErrorSeverity.Error,
CompilerErrorSeverity.Fatal,
]

/**
* Mask values for mapping compiler errors
*/
Expand Down Expand Up @@ -50,6 +58,9 @@ export class CompilerError {
static baseError(code: number): number {
return code & CompilerErrorMask.BaseError;
}
static namespace(code: number): CompilerErrorNamespace {
return code & CompilerErrorMask.Namespace;
}
static formatSeverity(code: number): string {
return errorSeverityName[CompilerError.severity(code)] ?? 'UNKNOWN';
}
Expand Down Expand Up @@ -156,6 +167,22 @@ export class CompilerError {
static exceptionToString(e?: any) : string {
return `${(e ?? 'unknown error').toString()}\n\nCall stack:\n${(e instanceof Error ? e.stack : (new Error()).stack)}`;
}

/**
* Returns the corresponding error severity value from a partial name match,
* e.g. 'inf' returns CompilerErrorSeverity.Info, or returns null if not found
* @param name
* @returns
*/
static severityNameToValue(name: string): CompilerErrorSeverity {
name = name.toLowerCase();
for(let level of CompilerErrorSeverityValues) {
if(errorSeverityName[level].startsWith(name)) {
return level;
}
}
return null;
}
};

/** @deprecated use `CompilerError.severity` instead */
Expand Down Expand Up @@ -183,6 +210,9 @@ export function compilerEventFormat(e : CompilerEvent | CompilerEvent[]) : strin
* ranges must not be changed as external modules may depend on specific error
* codes. Individual errors are defined at a compiler level, for example,
* kmc-ldml/src/compiler/messages.ts.
*
* kmc defines a mapping between each namespace and the corresponding compiler's
* error reporting class in kmc/src/messages/messageNamespaces.ts
*/
export enum CompilerErrorNamespace {
/**
Expand Down Expand Up @@ -259,11 +289,22 @@ export interface CompilerFileSystemCallbacks {
existsSync(name: string): boolean;
}

type CompilerErrorSeverityOverride = CompilerErrorSeverity | 'disable';
export interface CompilerMessageOverrideMap {
[code:number]: CompilerErrorSeverityOverride;
};

export interface CompilerMessageOverride {
code: number;
level: CompilerErrorSeverityOverride;
};

export interface CompilerCallbackOptions {
logLevel?: CompilerLogLevel;
logFormat?: CompilerLogFormat;
color?: boolean; // null or undefined == use console default
compilerWarningsAsErrors?: boolean;
messageOverrides?: CompilerMessageOverrideMap;
};

export interface KeymanCompilerArtifact {
Expand Down Expand Up @@ -352,6 +393,28 @@ export class CompilerFileCallbacks implements CompilerCallbacks {
return messages.find(m => failureCodes.includes(CompilerError.severity(m.code))) != undefined;
}

/**
*
* @param event
* @param overrides
* @returns true if event has been suppressed
*/
static applyMessageOverridesToEvent(event: CompilerEvent, overrides: CompilerMessageOverrideMap) {
// Override event severity from user preference -- this will not override
// fatal or error events
const severity = overrides?.[CompilerError.error(event.code)] ??
CompilerError.severity(event.code);

if(severity == 'disable') {
return true;
}

// Override the default event severity with the command line option
event.code = severity | (event.code & ~CompilerErrorMask.Severity);

return false;
}

/**
* Returns `true` if any message in the `messages` array is a Fatal or Error
* message, and if `compilerWarningsAsErrors` is `true`, then also returns
Expand Down Expand Up @@ -393,8 +456,11 @@ export class CompilerFileCallbacks implements CompilerCallbacks {
}

reportMessage(event: CompilerEvent): void {
const disable = CompilerFileCallbacks.applyMessageOverridesToEvent(event, this.options.messageOverrides);
this.messages.push(event);
this.parent.reportMessage({filename: this.filename, ...event});
if(!disable) {
this.parent.reportMessage({filename: this.filename, ...event});
}
}

debug(msg: string): void {
Expand Down
4 changes: 3 additions & 1 deletion developer/src/common/web/test-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export class TestCompilerCallbacks implements CompilerCallbacks {
}

printMessages() {
process.stdout.write(CompilerError.formatEvent(this.messages));
if(this.messages.length) {
process.stdout.write(CompilerError.formatEvent(this.messages));
}
}

hasMessage(code: number): boolean {
Expand Down
8 changes: 4 additions & 4 deletions developer/src/kmc-analyze/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
"bugs": {
"url": "https://github.com/keymanapp/keyman/issues"
},
"main": "build/src/index.js",
"exports": {
".": "./build/src/index.js"
},
"files": [
"build/src/"
],
"dependencies": {
"@keymanapp/common-types": "*",
"@keymanapp/kmc-kmn": "*",
"commander": "^10.0.0"
"@keymanapp/kmc-kmn": "*"
},
"devDependencies": {
"@keymanapp/resources-gosh": "*",
Expand All @@ -33,7 +34,6 @@
"c8": "^7.12.0",
"chai": "^4.3.4",
"chalk": "^2.4.2",
"esbuild": "^0.15.8",
"mocha": "^8.4.0",
"ts-node": "^9.1.1",
"typescript": "^4.9.5"
Expand Down
3 changes: 2 additions & 1 deletion developer/src/kmc-analyze/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*/

export { AnalyzeOskCharacterUse } from './osk-character-use/index.js';
export { AnalyzeOskRewritePua } from './osk-rewrite-pua/index.js';
export { AnalyzeOskRewritePua } from './osk-rewrite-pua/index.js';
export { AnalyzerMessages } from './messages.js';
2 changes: 1 addition & 1 deletion developer/src/kmc-analyze/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../../tsconfig.base.json",
"extends": "../../../tsconfig.base.json",

"compilerOptions": {
"outDir": "build/src/",
Expand Down
Loading

0 comments on commit d2580c6

Please sign in to comment.