Skip to content

Commit

Permalink
feat: allow to disable parser console logs (#2963)
Browse files Browse the repository at this point in the history
Add a new option to prevent `ParserMessageCollector` from displaying
warnings in the browser console.
  • Loading branch information
tbouffard authored Nov 15, 2023
1 parent e87b6d1 commit 421541e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/component/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ export type ParserOptions = {
* @param val the value of the 'name' attribute to be processed.
*/
additionalXmlAttributeProcessor?: (value: string) => string;
/**
* If `true`, disable the console logs produced by the parser.
* @default false
*/
disableConsoleLog?: boolean;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/component/parser/BpmnParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ class BpmnParser {
* @internal
*/
export function newBpmnParser(options?: ParserOptions): BpmnParser {
return new BpmnParser(newBpmnJsonParser(new ParsingMessageCollector()), new BpmnXmlParser(options));
return new BpmnParser(newBpmnJsonParser(new ParsingMessageCollector(options)), new BpmnXmlParser(options));
}
9 changes: 9 additions & 0 deletions src/component/parser/parsing-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import type { ParserOptions } from '../options';

export interface MessageDetails {
template: string;
arguments: string[];
Expand All @@ -23,8 +25,15 @@ export abstract class JsonParsingWarning {
abstract getMessage(): MessageDetails;
}

export type ParsingMessageCollectorOptions = Pick<ParserOptions, 'disableConsoleLog'>;

export class ParsingMessageCollector {
constructor(private options?: ParsingMessageCollectorOptions) {}

warning(warning: JsonParsingWarning): void {
if (this.options?.disableConsoleLog) {
return;
}
const message = warning.getMessage();
console.warn(`[bv-parser] ${message.template}`, ...message.arguments);
}
Expand Down
14 changes: 14 additions & 0 deletions test/unit/component/parser/parsing-messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,18 @@ describe('parsing message collector', () => {
);
});
});

describe('disabled logs: no console.warn when warning is registered', () => {
const parsingMessageCollector = new ParsingMessageCollector({ disableConsoleLog: true });

it('unknown edge bpmn element', () => {
parsingMessageCollector.warning(new EdgeUnknownBpmnElementWarning('edge-bpmnElement-unknown'));
expect(console.warn).not.toHaveBeenCalled();
});

it('missing font in label style', () => {
parsingMessageCollector.warning(new LabelStyleMissingFontWarning('BPMNEdge_id_0', 'non-existing_style_id'));
expect(console.warn).not.toHaveBeenCalled();
});
});
});

0 comments on commit 421541e

Please sign in to comment.