-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapi-version.js
More file actions
55 lines (48 loc) · 1.5 KB
/
api-version.js
File metadata and controls
55 lines (48 loc) · 1.5 KB
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
'use strict';
const VERSION_TAG = '@NApiVersion';
const VERSION_TAG_REGEX = /@NApiVersion(?: (\S+))?/;
const VERSIONS = ['1.0', '2.x', '2.0', '2.1'];
module.exports = {
meta: {
type: 'problem',
messages: {
invalidValue: "Invalid @NApiVersion value '{{ value }}'",
noValue: 'No @NApiVersion value provided',
},
},
create: (context) => ({
Program: () => {
const sourceCode = context.getSourceCode();
const comment = sourceCode
.getAllComments()
.find((node) => VERSION_TAG_REGEX.test(node.value));
if (!comment || comment.type !== 'Block') {
return;
}
const version = comment.value.match(VERSION_TAG_REGEX)[1];
const commentIndex = sourceCode.getIndexFromLoc(comment.loc.start) + 1;
const tagIndex = commentIndex + comment.value.indexOf(VERSION_TAG) + 1;
if (!version) {
context.report({
messageId: 'noValue',
loc: {
start: sourceCode.getLocFromIndex(tagIndex),
end: sourceCode.getLocFromIndex(tagIndex + VERSION_TAG.length),
},
});
} else if (!VERSIONS.includes(version)) {
const typeIndex = tagIndex + VERSION_TAG.length + 1;
context.report({
messageId: 'invalidValue',
data: {
value: version,
},
loc: {
start: sourceCode.getLocFromIndex(typeIndex),
end: sourceCode.getLocFromIndex(typeIndex + version.length),
},
});
}
},
}),
};