1
1
'use strict' ;
2
2
const getDocumentationUrl = require ( './utils/get-documentation-url' ) ;
3
+ const { isNumber, parseNumber} = require ( './utils/numeric' ) ;
3
4
4
5
const MESSAGE_ZERO_FRACTION = 'zero-fraction' ;
5
6
const MESSAGE_DANGLING_DOT = 'dangling-dot' ;
@@ -8,46 +9,32 @@ const messages = {
8
9
[ MESSAGE_DANGLING_DOT ] : 'Don\'t use a dangling dot in the number.'
9
10
} ;
10
11
11
- // Groups:
12
- // 1. Integer part.
13
- // 2. Dangling dot or dot with zeroes.
14
- // 3. Dot with digits except last zeroes.
15
- // 4. Scientific notation.
16
- const RE_DANGLINGDOT_OR_ZERO_FRACTIONS = / ^ (?< integerPart > [ + - ] ? \d * ) (?: (?< dotAndZeroes > \. 0 * ) | (?< dotAndDigits > \. \d * [ 1 - 9 ] ) 0 + ) (?< scientificNotationSuffix > e [ + - ] ? \d + ) ? $ / ;
12
+ const format = text => {
13
+ // Legacy octal number `0777` and prefixed number `0o1234` can't has dot.
14
+ const { number, mark, sign, power} = parseNumber ( text ) ;
15
+ return {
16
+ hasDanglingDot : number . endsWith ( '.' ) ,
17
+ formatted : number . replace ( / [ 0 _ . ] $ / g, '' ) + mark + sign + power
18
+ } ;
19
+ }
17
20
18
21
const create = context => {
19
22
return {
20
23
Literal : node => {
21
- if ( typeof node . value !== 'number' ) {
24
+ const { raw} = node ;
25
+ if ( ! isNumber ( node ) || ! raw . includes ( '.' ) ) {
22
26
return ;
23
27
}
24
28
25
- const match = RE_DANGLINGDOT_OR_ZERO_FRACTIONS . exec ( node . raw ) ;
26
- if ( match === null ) {
29
+ const { hasDanglingDot , formatted } = format ( raw ) ;
30
+ if ( formatted === raw ) {
27
31
return ;
28
32
}
29
33
30
- const {
31
- integerPart,
32
- dotAndZeroes,
33
- dotAndDigits,
34
- scientificNotationSuffix
35
- } = match . groups ;
36
-
37
- const isDanglingDot = dotAndZeroes === '.' ;
38
-
39
34
context . report ( {
40
35
node,
41
- messageId : isDanglingDot ? MESSAGE_DANGLING_DOT : MESSAGE_ZERO_FRACTION ,
42
- fix : fixer => {
43
- let wantedString = dotAndZeroes === undefined ? integerPart + dotAndDigits : integerPart ;
44
-
45
- if ( scientificNotationSuffix !== undefined ) {
46
- wantedString += scientificNotationSuffix ;
47
- }
48
-
49
- return fixer . replaceText ( node , wantedString ) ;
50
- }
36
+ messageId : hasDanglingDot ? MESSAGE_DANGLING_DOT : MESSAGE_ZERO_FRACTION ,
37
+ fix : fixer => fixer . replaceText ( node , formatted )
51
38
} ) ;
52
39
}
53
40
} ;
0 commit comments