@@ -34,52 +34,29 @@ module.exports = {
34
34
// ----------------------------------------------------------------------
35
35
const sourceCode = context . getSourceCode ( ) ;
36
36
37
- // ----------------------------------------------------------------------
38
- // Helpers
39
- // ----------------------------------------------------------------------
40
37
/**
41
- *compare two test cases despite of properties order.
42
- *@returns {boolean } if eq, return true, else return false.
38
+ * Create a unique cache key
39
+ * @param {object } test
40
+ * @returns {string }
43
41
*/
44
- function eq ( testA , testB ) {
45
- if ( testA . type !== testB . type ) {
46
- return false ;
47
- }
48
-
49
- if ( testA . type !== 'ObjectExpression' ) {
50
- return sourceCode . getText ( testA ) === sourceCode . getText ( testB ) ;
51
- }
52
-
53
- const propertiesA = testA . properties ;
54
- const propertiesB = testB . properties ;
55
-
56
- // if properties length not eq; return false;
57
- if ( propertiesA . length !== propertiesB . length ) {
58
- return false ;
59
- }
60
-
61
- const propertiesSetA = new Set ( ) ;
62
- propertiesA . forEach ( ( item ) => {
63
- const code = sourceCode . getText ( item ) ;
64
- propertiesSetA . add ( code ) ;
65
- } ) ;
66
-
67
- for ( const element of propertiesB ) {
68
- const code = sourceCode . getText ( element ) ;
69
- if ( ! propertiesSetA . has ( code ) ) {
70
- return false ;
71
- }
42
+ function toKey ( test ) {
43
+ if ( test . type !== 'ObjectExpression' ) {
44
+ return JSON . stringify ( [ test . type , sourceCode . getText ( test ) ] ) ;
72
45
}
73
- return true ;
46
+ return JSON . stringify ( [
47
+ test . type ,
48
+ ...test . properties . map ( ( p ) => sourceCode . getText ( p ) ) . sort ( ) ,
49
+ ] ) ;
74
50
}
75
51
76
52
return {
77
53
Program ( ast ) {
78
54
utils . getTestInfo ( context , ast ) . forEach ( ( testRun ) => {
79
55
[ testRun . valid , testRun . invalid ] . forEach ( ( tests ) => {
80
- const cache = [ ] ;
56
+ const cache = new Set ( ) ;
81
57
tests . forEach ( ( test ) => {
82
- if ( cache . some ( ( item ) => eq ( item , test ) ) ) {
58
+ const key = toKey ( test ) ;
59
+ if ( cache . has ( key ) ) {
83
60
context . report ( {
84
61
node : test ,
85
62
messageId : 'identical' ,
@@ -96,7 +73,7 @@ module.exports = {
96
73
} ,
97
74
} ) ;
98
75
} else {
99
- cache . push ( test ) ;
76
+ cache . add ( key ) ;
100
77
}
101
78
} ) ;
102
79
} ) ;
0 commit comments