-
Notifications
You must be signed in to change notification settings - Fork 2
/
Linter.js
161 lines (127 loc) · 3.77 KB
/
Linter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import BpmnModdle from 'bpmn-moddle';
import { Linter as BpmnLinter } from 'bpmnlint';
import StaticResolver from 'bpmnlint/lib/resolver/static-resolver';
import Resolver from './Resolver';
import { isString } from 'min-dash';
import { resolver as RulesResolver } from './compiled-config';
import modelerModdle from 'modeler-moddle/resources/modeler.json';
import zeebeModdle from 'zeebe-bpmn-moddle/resources/zeebe.json';
import { getErrorMessage } from './utils/error-messages';
import { getEntryIds } from './utils/properties-panel';
import { toSemverMinor } from './utils/version';
import { getDocumentationUrl } from './utils/documentation';
const moddle = new BpmnModdle({
modeler: modelerModdle,
zeebe: zeebeModdle
});
export class Linter {
constructor(options = {}) {
const {
modeler = 'desktop',
plugins = []
} = options;
this._modeler = modeler;
this._plugins = plugins;
}
async lint(contents) {
let rootElement;
if (isString(contents)) {
({ rootElement } = await moddle.fromXML(contents));
} else {
rootElement = contents;
}
const executionPlatform = rootElement.get('modeler:executionPlatform'),
executionPlatformVersion = rootElement.get('modeler:executionPlatformVersion');
if (!executionPlatform || !executionPlatformVersion) {
return [];
}
const configName = getConfigName(executionPlatform, executionPlatformVersion);
const config = this._createConfig(configName);
const resolver = await this._createResolver(configName);
const linter = new BpmnLinter({
config,
resolver
});
const reportsByRule = await linter.lint(rootElement);
return Object.entries(reportsByRule).reduce((allReports, entry) => {
const [ rule, reports ] = entry;
return [
...allReports,
...reports.map(report => {
const entryIds = getEntryIds(report);
return {
...report,
executionPlatform,
executionPlatformVersion,
message: getErrorMessage(
report,
executionPlatform,
executionPlatformVersion,
this._modeler
),
propertiesPanel: {
entryIds
},
documentation: {
url: getDocumentationUrl(rule)
},
rule
};
})
];
}, []);
}
_createConfig(configName) {
const configs = [
{
extends: `plugin:bpmnlint-plugin-camunda-compat/${ configName }`
},
...this._plugins.map(({ config }) => config)
];
return configs.reduce(
(config, _config) => {
let { extends: _extends = [], rules = {} } = _config;
if (isString(_extends)) {
_extends = [ _extends ];
}
return {
extends: [ ...config.extends, ..._extends ],
rules: {
...config.rules,
...rules
}
};
},
{
extends: [],
rules: {}
}
);
}
async _createResolver(configName) {
const { configs } = await import('bpmnlint-plugin-camunda-compat');
let { [ configName ]: config } = configs;
if (!config) {
config = {
rules: {}
};
}
const ConfigResolver = new StaticResolver({
[ `config:bpmnlint-plugin-camunda-compat/${ configName }` ]: config
});
return new Resolver([
ConfigResolver,
RulesResolver,
...this._plugins.map(({ resolver }) => resolver)
]);
}
}
function getConfigName(executionPlatform, executionPlatformVersion) {
return [
...executionPlatform.split(' ').map(toLowerCase),
...toSemverMinor(executionPlatformVersion).split('.')
].join('-');
}
function toLowerCase(string) {
return string.toLowerCase();
}