-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathnumeric-separators-style.js
182 lines (160 loc) · 3.79 KB
/
numeric-separators-style.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
const numeric = require('./utils/numeric.js');
const {isBigIntLiteral} = require('./ast/index.js');
const MESSAGE_ID = 'numeric-separators-style';
const messages = {
[MESSAGE_ID]: 'Invalid group length in numeric value.',
};
function addSeparator(value, {minimumDigits, groupLength}, fromLeft) {
const {length} = value;
if (length < minimumDigits) {
return value;
}
const parts = [];
if (fromLeft) {
for (let start = 0; start < length; start += groupLength) {
const end = Math.min(start + groupLength, length);
parts.push(value.slice(start, end));
}
} else {
for (let end = length; end > 0; end -= groupLength) {
const start = Math.max(end - groupLength, 0);
parts.unshift(value.slice(start, end));
}
}
return parts.join('_');
}
function addSeparatorFromLeft(value, options) {
return addSeparator(value, options, true);
}
function formatNumber(value, options) {
const {integer, dot, fractional} = numeric.parseFloatNumber(value);
return addSeparator(integer, options) + dot + addSeparatorFromLeft(fractional, options);
}
function format(value, {prefix, data}, options) {
const formatOption = options[prefix.toLowerCase()];
if (prefix) {
return prefix + addSeparator(data, formatOption);
}
const {
number,
mark,
sign,
power,
} = numeric.parseNumber(value);
return formatNumber(number, formatOption) + mark + sign + addSeparator(power, options['']);
}
const defaultOptions = {
binary: {minimumDigits: 0, groupLength: 4},
octal: {minimumDigits: 0, groupLength: 4},
hexadecimal: {minimumDigits: 0, groupLength: 2},
number: {minimumDigits: 5, groupLength: 3},
};
const create = context => {
const {
onlyIfContainsSeparator,
binary,
octal,
hexadecimal,
number,
} = {
onlyIfContainsSeparator: false,
...context.options[0],
};
const options = {
'0b': {
onlyIfContainsSeparator,
...defaultOptions.binary,
...binary,
},
'0o': {
onlyIfContainsSeparator,
...defaultOptions.octal,
...octal,
},
'0x': {
onlyIfContainsSeparator,
...defaultOptions.hexadecimal,
...hexadecimal,
},
'': {
onlyIfContainsSeparator,
...defaultOptions.number,
...number,
},
};
return {
Literal(node) {
if (!numeric.isNumeric(node) || numeric.isLegacyOctal(node)) {
return;
}
const {raw} = node;
let number = raw;
let suffix = '';
if (isBigIntLiteral(node)) {
number = raw.slice(0, -1);
suffix = 'n';
}
const strippedNumber = number.replaceAll('_', '');
const {prefix, data} = numeric.getPrefix(strippedNumber);
const {onlyIfContainsSeparator} = options[prefix.toLowerCase()];
if (onlyIfContainsSeparator && !raw.includes('_')) {
return;
}
const formatted = format(strippedNumber, {prefix, data}, options) + suffix;
if (raw !== formatted) {
return {
node,
messageId: MESSAGE_ID,
fix: fixer => fixer.replaceText(node, formatted),
};
}
},
};
};
const formatOptionsSchema = ({minimumDigits, groupLength}) => ({
type: 'object',
additionalProperties: false,
properties: {
onlyIfContainsSeparator: {
type: 'boolean',
},
minimumDigits: {
type: 'integer',
minimum: 0,
default: minimumDigits,
},
groupLength: {
type: 'integer',
minimum: 1,
default: groupLength,
},
},
});
const schema = [{
type: 'object',
additionalProperties: false,
properties: {
...Object.fromEntries(
Object.entries(defaultOptions).map(([type, options]) => [type, formatOptionsSchema(options)]),
),
onlyIfContainsSeparator: {
type: 'boolean',
default: false,
},
},
}];
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Enforce the style of numeric separators by correctly grouping digits.',
recommended: true,
},
fixable: 'code',
schema,
messages,
},
};