File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { RuleTester } from "eslint" ;
2
+
3
+ import rule from "./no-new-date" ;
4
+
5
+ const tester = new RuleTester ( { parserOptions : { ecmaVersion : 2015 } } ) ;
6
+
7
+ tester . run ( "no-new-date" , rule , {
8
+ valid : [
9
+ { code : `new Date(1, 2, 3)` } ,
10
+ { code : `hoge.now()` } ,
11
+ ] ,
12
+ invalid : [
13
+ {
14
+ code : `Date.now()` ,
15
+ errors : [ { message : / .* / } ] ,
16
+ } ,
17
+ {
18
+ code : `moment()` ,
19
+ errors : [ { message : / .* / } ] ,
20
+ } ,
21
+ {
22
+ code : `new Date()` ,
23
+ errors : [ { message : / .* / } ] ,
24
+ }
25
+ ] ,
26
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { Rule } from "eslint" ;
2
+ import { Node , MemberExpression , NewExpression } from "estree" ;
3
+
4
+ const rule : Rule . RuleModule = {
5
+ create : ( context ) => {
6
+ return {
7
+ "CallExpression > MemberExpression > Identifier.property[name='now']" : ( node : Node ) => {
8
+ const parent = context . getAncestors ( ) . pop ( ) as MemberExpression ;
9
+ const objectNode = parent . object ;
10
+ if ( objectNode . type === "Identifier" && objectNode . name === "Date" ) {
11
+ context . report ( {
12
+ message : "Don't use 'Date.now()'" ,
13
+ node,
14
+ } ) ;
15
+ }
16
+ } ,
17
+ "CallExpression > Identifier[name='moment']" : ( node : Node ) => {
18
+ context . report ( {
19
+ message : "Don't use 'moment()'" ,
20
+ node,
21
+ } ) ;
22
+ } ,
23
+ "NewExpression > Identifier.callee[name='Date']" : ( node : Node ) => {
24
+ const parent = context . getAncestors ( ) . pop ( ) as NewExpression ;
25
+ if ( ! parent . arguments . length ) {
26
+ context . report ( {
27
+ message : "Don't use 'new Date()'" ,
28
+ node,
29
+ } ) ;
30
+ }
31
+ } ,
32
+ } ;
33
+ }
34
+ } ;
35
+
36
+ export = rule ;
You can’t perform that action at this time.
0 commit comments