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

chore(eslint): add unicorn Array rules #2877

Merged
merged 9 commits into from
Sep 25, 2023
Merged
14 changes: 14 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ module.exports = {
'unicorn/prefer-dom-node-append': 'error',
'unicorn/prefer-dom-node-remove': 'error',
'unicorn/no-negated-condition': 'error',
'unicorn/no-array-callback-reference': 'error',
'unicorn/no-array-method-this-argument': 'error',
'unicorn/no-array-push-push': 'error',
'unicorn/no-array-reduce': 'error',
'unicorn/no-instanceof-array': 'error',
'unicorn/no-unreadable-array-destructuring': 'error',
'unicorn/no-useless-length-check': 'error',
'unicorn/prefer-array-find': 'error',
'unicorn/prefer-array-flat': 'error',
'unicorn/prefer-array-flat-map': 'error',
'unicorn/prefer-array-index-of': 'error',
'unicorn/prefer-array-some': 'error',
'unicorn/prefer-includes': 'error',
'unicorn/require-array-join-separator': 'error',
'import/newline-after-import': ['error', { count: 1 }],
'import/first': 'error',
'import/order': [
Expand Down
2 changes: 1 addition & 1 deletion dev/ts/pages/elements-identification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function computeStyleUpdateByKind(bpmnKind: BpmnElementKind): StyleUpdate {
}

function updateStyleByAPI(bpmnIds: string[], bpmnKind: ShapeBpmnElementKind): void {
const subProcessChildrenIds = bpmnIds.filter(isChildOfSubProcess);
const subProcessChildrenIds = bpmnIds.filter(element => isChildOfSubProcess(element));
const otherIds = bpmnIds.filter(bpmnId => !subProcessChildrenIds.includes(bpmnId));

if (subProcessChildrenIds.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion dev/ts/shared/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function configureStyleFromParameters(parameters: URLSearchParams): void {
// Collect style properties to update them later with the bpmn-visualization API
logStartup(`Configuring the "Update Style" API from query parameters`);
// Only create the StyleUpdate object if some parameters are set
if (Array.from(parameters.keys()).filter(key => key.startsWith('style.api.')).length > 0) {
if (Array.from(parameters.keys()).some(key => key.startsWith('style.api.'))) {
style = { stroke: {}, font: {}, fill: {} };

parameters.get('style.api.stroke.color') && (style.stroke.color = parameters.get('style.api.stroke.color'));
Expand Down
2 changes: 1 addition & 1 deletion scripts/utils/parseBpmn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ console.info('Generating BPMN in the "%s" output type', outputType);
if (!bpmnFilePath) {
throw new Error('You must provide file path as 1st parameter for example: test/fixtures/bpmn/simple-start-task-end.bpmn ');
}
if (['json', 'model'].indexOf(outputType) == -1) {
if (!['json', 'model'].includes(outputType)) {
throw new Error('--output parameter must be one of: json | model');
}
// eslint-disable-next-line no-console
Expand Down
3 changes: 1 addition & 2 deletions src/component/mxgraph/renderer/StyleComputer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ export default class StyleComputer {

computeMessageFlowIconStyle(edge: Edge): string {
const styleValues: [string, string][] = [];
styleValues.push(['shape', BpmnStyleIdentifier.MESSAGE_FLOW_ICON]);
styleValues.push([BpmnStyleIdentifier.IS_INITIATING, String(edge.messageVisibleKind === MessageVisibleKind.INITIATING)]);
styleValues.push(['shape', BpmnStyleIdentifier.MESSAGE_FLOW_ICON], [BpmnStyleIdentifier.IS_INITIATING, String(edge.messageVisibleKind === MessageVisibleKind.INITIATING)]);
if (!this.ignoreBpmnColors) {
edge.extensions.strokeColor && styleValues.push([mxConstants.STYLE_STROKECOLOR, edge.extensions.strokeColor]);
}
Expand Down
4 changes: 1 addition & 3 deletions src/component/parser/json/converter/CategoryConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export default class CategoryConverter {
constructor(private convertedElements: ConvertedElements) {}

deserialize(definitions: TDefinitions): void {
const categoryValues = ensureIsArray<TCategory>(definitions.category)
.map(category => ensureIsArray(category.categoryValue))
.flat();
const categoryValues = ensureIsArray<TCategory>(definitions.category).flatMap(category => ensureIsArray(category.categoryValue));
for (const categoryValue of categoryValues) {
this.convertedElements.registerCategoryValue(categoryValue.id, categoryValue.value);
}
Expand Down
6 changes: 2 additions & 4 deletions src/component/registry/bpmn-elements-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,13 @@ export class BpmnElementsRegistry implements CssClassesRegistry, ElementsRegistr

getModelElementsByKinds(bpmnKinds: BpmnElementKind | BpmnElementKind[]): BpmnSemantic[] {
return ensureIsArray<BpmnElementKind>(bpmnKinds)
.map(kind => this.htmlElementRegistry.getBpmnHtmlElements(kind))
.flat()
.flatMap(kind => this.htmlElementRegistry.getBpmnHtmlElements(kind))
.map(htmlElement => this.getRelatedBpmnSemantic(htmlElement));
}

getElementsByKinds(bpmnKinds: BpmnElementKind | BpmnElementKind[]): BpmnElement[] {
return ensureIsArray<BpmnElementKind>(bpmnKinds)
.map(kind => this.htmlElementRegistry.getBpmnHtmlElements(kind))
.flat()
.flatMap(kind => this.htmlElementRegistry.getBpmnHtmlElements(kind))
.map(htmlElement => ({ htmlElement, bpmnSemantic: this.getRelatedBpmnSemantic(htmlElement) }));
}

Expand Down
1 change: 1 addition & 0 deletions src/component/registry/bpmn-model-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class BpmnModelRegistry {
private onLoadCallback: () => void;

load(bpmnModel: BpmnModel, modelFilter?: ModelFilter): RenderedModel {
// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
const filteredModel = new ModelFiltering().filter(bpmnModel, modelFilter);

this.searchableModel = new SearchableModel(filteredModel);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/component/parser/BpmnParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe('parse xml to model', () => {
const model = newBpmnParser().parse(readFileSync('../fixtures/bpmn/xml-parsing/special/simple-start-task-end_truncated.bpmn'));
expect(model.flowNodes).toHaveLength(2);
// This element is truncated in the source xml file
const activities = model.flowNodes.filter(shape => shape.id == 'BPMNShape_Activity_1');
expect(activities[0].bpmnElement.id).toBe('Activity_1');
const activity = model.flowNodes.find(shape => shape.id == 'BPMNShape_Activity_1');
expect(activity.bpmnElement.id).toBe('Activity_1');
});

it('Parse a xml file which is not a BPMN diagram', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Special parsing cases', () => {

const bpmnDiagram = json.definitions.BPMNDiagram as BPMNDiagram;
const shapes = bpmnDiagram.BPMNPlane.BPMNShape as BPMNShape[];
const getShape = (id: string): BPMNShape => shapes.filter(s => s.id == id)[0];
const getShape = (id: string): BPMNShape => shapes.find(s => s.id == id);

// string instead of number
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -57,7 +57,7 @@ describe('Special parsing cases', () => {

const bpmnDiagram = json.definitions.BPMNDiagram as BPMNDiagram;
const shapes = bpmnDiagram.BPMNPlane.BPMNShape as BPMNShape[];
const getShape = (id: string): BPMNShape => shapes.filter(s => s.id == id)[0];
const getShape = (id: string): BPMNShape => shapes.find(s => s.id == id);

// x and y values are string instead of number in the source diagram
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
17 changes: 17 additions & 0 deletions test/unit/component/registry/bpmn-model-filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('Bpmn Model filters', () => {
},
},
});
// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
const bpmnModel = modelFiltering.filter(originalBpmnModel, modelFilter);
expect(bpmnModel).toStrictEqual(originalBpmnModel);
});
Expand Down Expand Up @@ -116,6 +117,7 @@ describe('Bpmn Model filters', () => {
expect(originalBpmnModel.pools).toHaveLength(0);
expect(originalBpmnModel.lanes).toHaveLength(0);
expect(() =>
// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
modelFiltering.filter(originalBpmnModel, {
pools: { id: 'process_id' },
}),
Expand All @@ -137,6 +139,7 @@ describe('Bpmn Model filters', () => {
});

const poolFilter = propertyName === 'id' ? { id: 'participant_id_1' } : { name: 'Participant 1' };
// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
const bpmnModel = modelFiltering.filter(originalBpmnModel, { pools: poolFilter });

expect(bpmnModel).toStrictEqual(originalBpmnModel);
Expand All @@ -160,6 +163,7 @@ describe('Bpmn Model filters', () => {
expect(originalBpmnModel.flowNodes).toHaveLength(2);

const poolFilter = propertyName === 'id' ? { id: 'participant_id_1' } : { name: 'Participant 1' };
// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
const bpmnModel = modelFiltering.filter(originalBpmnModel, { pools: poolFilter });

expect(bpmnModel).toStrictEqual(originalBpmnModel);
Expand Down Expand Up @@ -206,6 +210,7 @@ describe('Bpmn Model filters', () => {
},
],
});
// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
const bpmnModel = modelFiltering.filter(originalBpmnModel, { pools: { id: 'participant_id_2' } });

expect(bpmnModel.pools).toHaveLength(1);
Expand Down Expand Up @@ -278,6 +283,7 @@ describe('Bpmn Model filters', () => {
expect(originalBpmnModel.edges).toHaveLength(2);

const poolFilter = propertyName === 'id' ? [{ id: 'participant_id_1' }, { id: 'participant_id_2' }] : [{ name: 'Participant 1' }, { name: 'Participant 2' }];
// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
const bpmnModel = modelFiltering.filter(originalBpmnModel, { pools: poolFilter });

expect(bpmnModel.pools).toHaveLength(2);
Expand Down Expand Up @@ -307,6 +313,7 @@ describe('Bpmn Model filters', () => {
propertyName === 'id'
? [{ id: 'not exist_0' }, { id: 'participant_id_1' }, { id: 'not exist_1' }, { id: 'not exist_2' }]
: [{ name: 'not exist_0' }, { name: 'Participant 1' }, { name: 'not exist_1' }, { name: 'not exist_2' }];
// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
const bpmnModel = modelFiltering.filter(originalBpmnModel, { pools: poolFilter });

expect(bpmnModel.pools).toHaveLength(1);
Expand All @@ -330,6 +337,7 @@ describe('Bpmn Model filters', () => {
},
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
expect(modelFiltering.filter(originalBpmnModel, { pools: { id: 'participant_id_1', name: 'not exist_1' } })).toStrictEqual(originalBpmnModel);
});

Expand All @@ -350,6 +358,7 @@ describe('Bpmn Model filters', () => {
},
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
expect(modelFiltering.filter(originalBpmnModel, { pools: { id: 'participant_id_1' } })).toStrictEqual(originalBpmnModel);
});

Expand All @@ -369,6 +378,7 @@ describe('Bpmn Model filters', () => {
},
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
expect(modelFiltering.filter(originalBpmnModel, { pools: { id: 'participant_id_1' } })).toStrictEqual(originalBpmnModel);
});

Expand Down Expand Up @@ -400,6 +410,7 @@ describe('Bpmn Model filters', () => {
},
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
expect(modelFiltering.filter(originalBpmnModel, { pools: { id: 'participant_id_1' } })).toStrictEqual(originalBpmnModel);
});

Expand Down Expand Up @@ -433,6 +444,7 @@ describe('Bpmn Model filters', () => {
},
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
expect(modelFiltering.filter(originalBpmnModel, { pools: { id: 'participant_id_1' } })).toStrictEqual(originalBpmnModel);
});

Expand Down Expand Up @@ -461,6 +473,7 @@ describe('Bpmn Model filters', () => {
},
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
expect(modelFiltering.filter(originalBpmnModel, { pools: { id: 'participant_id_1' } })).toStrictEqual(originalBpmnModel);
});
});
Expand All @@ -483,6 +496,7 @@ describe('Bpmn Model filters', () => {
],
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
const bpmnModel = modelFiltering.filter(originalBpmnModel, { pools: [{ id: 'participant_id_1' }, { name: 'Participant 2' }] });
expect(bpmnModel.pools).toHaveLength(2);
verifyShape(bpmnModel.pools[0], {
Expand Down Expand Up @@ -511,6 +525,7 @@ describe('Bpmn Model filters', () => {
],
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
const bpmnModel = modelFiltering.filter(originalBpmnModel, { pools: [{ id: 'participant_id_1' }, { name: 'Participant 1' }] });
expect(bpmnModel.pools).toHaveLength(1);
verifyShape(bpmnModel.pools[0], {
Expand All @@ -529,6 +544,7 @@ describe('Bpmn Model filters', () => {
},
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
expect(modelFiltering.filter(originalBpmnModel, { pools: { id: 'participant_id_1' } })).toStrictEqual(originalBpmnModel);
});

Expand Down Expand Up @@ -556,6 +572,7 @@ describe('Bpmn Model filters', () => {
},
});

// eslint-disable-next-line unicorn/no-array-callback-reference,unicorn/no-array-method-this-argument -- Bad error "Do not use the `this` argument in `Array#filter()`", modelFiltering is not an array.
expect(modelFiltering.filter(originalBpmnModel, { pools: { id: 'participant_id_1' } })).toStrictEqual({
pools: [],
lanes: originalBpmnModel.lanes,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/helpers/JsonBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export function buildDefinitions({ process, messageFlows, globalTask }: BuildDef
},
};

if (Array.isArray(process) && process.filter(p => p.withParticipant).length > 0) {
if (Array.isArray(process) && process.some(p => p.withParticipant)) {
(json.definitions.collaboration as TCollaboration).participant = [];
}
for (const [index, processParameter] of (Array.isArray(process) ? process : [process]).entries()) addParticipantProcessAndElements(processParameter, json, index);
Expand Down