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