1
1
'use strict' ;
2
2
const {
3
- matches,
4
- methodCallSelector,
5
- newExpressionSelector,
6
- callExpressionSelector,
7
- } = require ( './selectors/index.js' ) ;
3
+ isMethodCall,
4
+ isCallExpression,
5
+ isNewExpression,
6
+ } = require ( './ast/index.js' ) ;
8
7
const { fixSpaceAroundKeyword} = require ( './fix/index.js' ) ;
9
8
10
9
const MESSAGE_ID_DEFAULT = 'prefer-date' ;
@@ -16,42 +15,7 @@ const messages = {
16
15
[ MESSAGE_ID_NUMBER ] : 'Prefer `Date.now()` over `Number(new Date())`.' ,
17
16
} ;
18
17
19
- const createNewDateSelector = path => newExpressionSelector ( { path, name : 'Date' , argumentsLength : 0 } ) ;
20
- const operatorsSelector = ( ...operators ) => matches ( operators . map ( operator => `[operator="${ operator } "]` ) ) ;
21
- // `new Date()`
22
- const newDateSelector = createNewDateSelector ( ) ;
23
- // `new Date().{getTime,valueOf}()`
24
- const methodsSelector = [
25
- methodCallSelector ( {
26
- methods : [ 'getTime' , 'valueOf' ] ,
27
- argumentsLength : 0 ,
28
- } ) ,
29
- createNewDateSelector ( 'callee.object' ) ,
30
- ] . join ( '' ) ;
31
- // `{Number,BigInt}(new Date())`
32
- const builtinObjectSelector = [
33
- callExpressionSelector ( { names : [ 'Number' , 'BigInt' ] , argumentsLength : 1 } ) ,
34
- createNewDateSelector ( 'arguments.0' ) ,
35
- ] . join ( '' ) ;
36
- // https://github.com/estree/estree/blob/master/es5.md#unaryoperator
37
- const unaryExpressionsSelector = [
38
- 'UnaryExpression' ,
39
- operatorsSelector ( '+' , '-' ) ,
40
- createNewDateSelector ( 'argument' ) ,
41
- ] . join ( '' ) ;
42
- const assignmentExpressionSelector = [
43
- 'AssignmentExpression' ,
44
- operatorsSelector ( '-=' , '*=' , '/=' , '%=' , '**=' ) ,
45
- '>' ,
46
- `${ newDateSelector } .right` ,
47
- ] . join ( '' ) ;
48
- const binaryExpressionSelector = [
49
- 'BinaryExpression' ,
50
- operatorsSelector ( '-' , '*' , '/' , '%' , '**' ) ,
51
- // Both `left` and `right` properties
52
- '>' ,
53
- newDateSelector ,
54
- ] . join ( '' ) ;
18
+ const isNewDate = node => isNewExpression ( node , { name : 'Date' , argumentsLength : 0 } ) ;
55
19
56
20
const getProblem = ( node , problem , sourceCode ) => ( {
57
21
node,
@@ -68,36 +32,92 @@ const getProblem = (node, problem, sourceCode) => ({
68
32
69
33
/** @param {import('eslint').Rule.RuleContext } context */
70
34
const create = context => ( {
71
- [ methodsSelector ] ( node ) {
72
- const method = node . callee . property ;
73
- return getProblem ( node , {
74
- node : method ,
75
- messageId : MESSAGE_ID_METHOD ,
76
- data : { method : method . name } ,
77
- } ) ;
78
- } ,
79
- [ builtinObjectSelector ] ( node ) {
80
- const { name} = node . callee ;
81
- if ( name === 'Number' ) {
82
- return getProblem ( node , {
83
- messageId : MESSAGE_ID_NUMBER ,
35
+ CallExpression ( callExpression ) {
36
+ // `new Date().{getTime,valueOf}()`
37
+ if (
38
+ isMethodCall ( callExpression , {
39
+ methods : [ 'getTime' , 'valueOf' ] ,
40
+ argumentsLength : 0 ,
41
+ optionalCall : false ,
42
+ optionalMember : false ,
43
+ } )
44
+ && isNewDate ( callExpression . callee . object )
45
+ ) {
46
+ const method = callExpression . callee . property ;
47
+ return getProblem ( callExpression , {
48
+ node : method ,
49
+ messageId : MESSAGE_ID_METHOD ,
50
+ data : { method : method . name } ,
84
51
} ) ;
85
52
}
86
53
87
- return getProblem ( node . arguments [ 0 ] ) ;
54
+ // `{Number,BigInt}(new Date())`
55
+ if (
56
+ isCallExpression ( callExpression , {
57
+ names : [ 'Number' , 'BigInt' ] ,
58
+ argumentsLength : 1 ,
59
+ optional : false ,
60
+ } )
61
+ && isNewDate ( callExpression . arguments [ 0 ] )
62
+ ) {
63
+ const { name} = callExpression . callee ;
64
+ if ( name === 'Number' ) {
65
+ return getProblem ( callExpression , {
66
+ messageId : MESSAGE_ID_NUMBER ,
67
+ } ) ;
68
+ }
69
+
70
+ return getProblem ( callExpression . arguments [ 0 ] ) ;
71
+ }
88
72
} ,
89
- [ unaryExpressionsSelector ] ( node ) {
90
- return getProblem (
91
- node . operator === '-' ? node . argument : node ,
92
- { } ,
93
- context . sourceCode ,
94
- ) ;
73
+ UnaryExpression ( unaryExpression ) {
74
+ // https://github.com/estree/estree/blob/master/es5.md#unaryoperator
75
+ if (
76
+ unaryExpression . operator !== '+'
77
+ && unaryExpression . operator !== '-'
78
+ ) {
79
+ return ;
80
+ }
81
+
82
+ if ( isNewDate ( unaryExpression . argument ) ) {
83
+ return getProblem (
84
+ unaryExpression . operator === '-' ? unaryExpression . argument : unaryExpression ,
85
+ { } ,
86
+ context . sourceCode ,
87
+ ) ;
88
+ }
95
89
} ,
96
- [ assignmentExpressionSelector ] ( node ) {
97
- return getProblem ( node ) ;
90
+ AssignmentExpression ( assignmentExpression ) {
91
+ if (
92
+ assignmentExpression . operator !== '-='
93
+ && assignmentExpression . operator !== '*='
94
+ && assignmentExpression . operator !== '/='
95
+ && assignmentExpression . operator !== '%='
96
+ && assignmentExpression . operator !== '**='
97
+ ) {
98
+ return ;
99
+ }
100
+
101
+ if ( isNewDate ( assignmentExpression . right ) ) {
102
+ return getProblem ( assignmentExpression . right ) ;
103
+ }
98
104
} ,
99
- [ binaryExpressionSelector ] ( node ) {
100
- return getProblem ( node ) ;
105
+ * BinaryExpression ( binaryExpression ) {
106
+ if (
107
+ binaryExpression . operator !== '-'
108
+ && binaryExpression . operator !== '*'
109
+ && binaryExpression . operator !== '/'
110
+ && binaryExpression . operator !== '%'
111
+ && binaryExpression . operator !== '**'
112
+ ) {
113
+ return ;
114
+ }
115
+
116
+ for ( const node of [ binaryExpression . left , binaryExpression . right ] ) {
117
+ if ( isNewDate ( node ) ) {
118
+ yield getProblem ( node ) ;
119
+ }
120
+ }
101
121
} ,
102
122
} ) ;
103
123
0 commit comments