Skip to content

Commit 28dacd6

Browse files
authored
chore(eslint): add linter to forbid bigInt [skip-bc] (algolia#4094)
1 parent 2746e40 commit 28dacd6

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

.eslintrc.cjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,20 @@ module.exports = {
5151
files: ['specs/**/*.yml'],
5252
rules: {
5353
'automation-custom/end-with-dot': 'error',
54+
'automation-custom/no-big-int': 'error',
5455
'automation-custom/no-final-dot': 'error',
5556
'automation-custom/single-quote-ref': 'error',
5657
},
5758
overrides: [
5859
{
5960
files: ['!specs/bundled/*.yml'],
6061
rules: {
61-
'automation-custom/out-of-line-enum': 'error',
62-
'automation-custom/out-of-line-one-of': 'error',
6362
'automation-custom/out-of-line-all-of': 'error',
6463
'automation-custom/out-of-line-any-of': 'error',
65-
'automation-custom/valid-acl': 'error',
64+
'automation-custom/out-of-line-enum': 'error',
65+
'automation-custom/out-of-line-one-of': 'error',
6666
'automation-custom/ref-common': 'error',
67+
'automation-custom/valid-acl': 'error',
6768
'automation-custom/valid-inline-title': 'error',
6869
},
6970
},

eslint/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { endWithDot } from './rules/endWithDot.js';
2+
import { noBigInt } from './rules/noBigInt.js';
23
import { noFinalDot } from './rules/noFinalDot.js';
34
import { noNewLine } from './rules/noNewLine.js';
45
import { createOutOfLineRule } from './rules/outOfLineRule.js';
@@ -9,15 +10,16 @@ import { validInlineTitle } from './rules/validInlineTitle.js';
910

1011
const rules = {
1112
'end-with-dot': endWithDot,
13+
'no-big-int': noBigInt,
1214
'no-final-dot': noFinalDot,
13-
'out-of-line-enum': createOutOfLineRule({ property: 'enum' }),
14-
'out-of-line-one-of': createOutOfLineRule({ property: 'oneOf' }),
15+
'no-new-line': noNewLine,
1516
'out-of-line-all-of': createOutOfLineRule({ property: 'allOf' }),
1617
'out-of-line-any-of': createOutOfLineRule({ property: 'anyOf' }),
18+
'out-of-line-enum': createOutOfLineRule({ property: 'enum' }),
19+
'out-of-line-one-of': createOutOfLineRule({ property: 'oneOf' }),
20+
'ref-common': refCommon,
1721
'single-quote-ref': singleQuoteRef,
1822
'valid-acl': validACL,
19-
'no-new-line': noNewLine,
20-
'ref-common': refCommon,
2123
'valid-inline-title': validInlineTitle,
2224
};
2325

eslint/src/rules/noBigInt.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { createRule } from 'eslint-plugin-yml/lib/utils';
2+
3+
import { isPairWithKey, isScalar } from '../utils.js';
4+
5+
// type: number with format: int64 will generate a BigInteger which is not nice to use, most of the time we only need int, long, float or double.
6+
export const noBigInt = createRule('noBigInt', {
7+
meta: {
8+
docs: {
9+
description: 'type big integer is forbidden, did you mean type: integer ?',
10+
categories: null,
11+
extensionRule: false,
12+
layout: false,
13+
},
14+
messages: {
15+
noBigInt: 'type big integer is forbidden, did you mean type: integer ?',
16+
},
17+
type: 'layout',
18+
schema: [],
19+
},
20+
create(context) {
21+
if (!context.getSourceCode().parserServices.isYAML) {
22+
return {};
23+
}
24+
25+
return {
26+
YAMLPair(node): void {
27+
if (!isPairWithKey(node, 'type')) {
28+
return;
29+
}
30+
31+
if (!isScalar(node.value) || node.value.value !== 'number') {
32+
return;
33+
}
34+
35+
// check the format next to the type
36+
node.parent.pairs.find((pair) => {
37+
if (isPairWithKey(pair, 'format') && isScalar(pair.value) && (pair.value.value === 'int32' || pair.value.value === 'int64')) {
38+
context.report({
39+
node: pair.value as any,
40+
messageId: 'noBigInt',
41+
});
42+
}
43+
});
44+
},
45+
};
46+
},
47+
});

eslint/tests/noBigInt.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { runClassic } from 'eslint-vitest-rule-tester';
2+
import yamlParser from 'yaml-eslint-parser';
3+
import { noBigInt } from '../src/rules/noBigInt.js';
4+
5+
6+
runClassic(
7+
'no-big-int',
8+
noBigInt,
9+
{
10+
valid: [`
11+
type: object
12+
properties:
13+
id:
14+
type: number
15+
format: float
16+
url:
17+
type: string
18+
format: uri
19+
`, `
20+
prop:
21+
type: integer
22+
format: int32
23+
`],
24+
invalid: [
25+
{
26+
code: `
27+
type: object
28+
properties:
29+
id:
30+
type: number
31+
format: int64
32+
url:
33+
type: string
34+
format: uri
35+
`,
36+
errors: [{ messageId: 'noBigInt' }],
37+
},
38+
{
39+
code: `
40+
prop:
41+
type: number
42+
format: int32
43+
`,
44+
errors: [{ messageId: 'noBigInt' }],
45+
},
46+
],
47+
},
48+
{
49+
parser: yamlParser,
50+
},
51+
);

0 commit comments

Comments
 (0)