Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to disable parser console logs #2963

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});