@@ -62,10 +62,12 @@ function wrapCode(code) {
62
62
/**
63
63
* Modify the given test pattern to test with vue-eslint-parser.
64
64
*
65
+ * @param {string } ruleId The rule ID.
65
66
* @param {string|object } pattern - The test pattern to be modified.
66
67
* @returns {object|null } The modified pattern.
67
68
*/
68
- function modifyPattern ( pattern ) {
69
+ //eslint-disable-next-line complexity
70
+ function modifyPattern ( ruleId , pattern ) {
69
71
if ( typeof pattern === "string" ) {
70
72
if ( pattern . startsWith ( "#!" ) ) {
71
73
return null
@@ -76,6 +78,9 @@ function modifyPattern(pattern) {
76
78
parser : PARSER_PATH ,
77
79
}
78
80
}
81
+
82
+ // If a test case depends on filename or special parser (returns constant AST
83
+ // object), we cannot rewrite the test case with `vue-eslint-parser`.
79
84
if (
80
85
pattern . parser != null ||
81
86
pattern . filename != null ||
@@ -84,22 +89,60 @@ function modifyPattern(pattern) {
84
89
return null
85
90
}
86
91
92
+ // Ignore for now
93
+ if (
94
+ ruleId === "newline-per-chained-call" &&
95
+ pattern . code === "foo.bar()['foo' + \u2029 + 'bar']()"
96
+ ) {
97
+ return null
98
+ }
99
+
100
+ // Wrap the code by `<script>` tag.
87
101
pattern . filename = "test.vue"
88
102
pattern . parser = PARSER_PATH
89
103
pattern . code = wrapCode ( pattern . code )
90
104
if ( pattern . output != null ) {
91
105
pattern . output = wrapCode ( pattern . output )
92
106
}
107
+
93
108
if ( Array . isArray ( pattern . errors ) ) {
94
109
for ( const error of pattern . errors ) {
95
110
if ( typeof error === "object" && ! processed . has ( error ) ) {
96
111
processed . add ( error )
97
112
98
- if ( error . line != null ) {
99
- error . line = Number ( error . line ) + 1
113
+ // The line number +1 because `<script>\n` is inserted.
114
+ if ( typeof error . line === "number" ) {
115
+ error . line += 1
116
+ }
117
+ if ( typeof error . endLine === "number" ) {
118
+ error . endLine += 1
100
119
}
101
- if ( error . endLine != null ) {
102
- error . endLine = Number ( error . endLine ) + 1
120
+
121
+ // `semi` rule is special a bit.
122
+ // The `endLine` is explicitly undefined if it inserts semi into EOF.
123
+ // The `endColumn` is explicitly undefined if it inserts semi into EOF.
124
+ // Those becomes non-EOF because `\n</script>` is inserted.
125
+ if ( ruleId === "semi" ) {
126
+ if (
127
+ Object . hasOwnProperty . call ( error , "endLine" ) &&
128
+ error . endLine === undefined
129
+ ) {
130
+ const lines = pattern . code . split ( / \r \n | [ \r \n ] / gu)
131
+ error . endLine = lines . length
132
+ }
133
+ if (
134
+ Object . hasOwnProperty . call ( error , "endColumn" ) &&
135
+ error . endColumn === undefined
136
+ ) {
137
+ error . endColumn = 1
138
+ }
139
+ }
140
+
141
+ // Wrap the code by `<script>` tag.
142
+ if ( Array . isArray ( error . suggestions ) ) {
143
+ for ( const suggestion of error . suggestions ) {
144
+ suggestion . output = wrapCode ( suggestion . output )
145
+ }
103
146
}
104
147
}
105
148
}
@@ -120,8 +163,12 @@ function modifyPattern(pattern) {
120
163
*/
121
164
function overrideRun ( ruleId , impl , patterns ) {
122
165
return originalRun . call ( this , ruleId , impl , {
123
- valid : patterns . valid . map ( modifyPattern ) . filter ( Boolean ) ,
124
- invalid : patterns . invalid . map ( modifyPattern ) . filter ( Boolean ) ,
166
+ valid : patterns . valid
167
+ . map ( pattern => modifyPattern ( ruleId , pattern ) )
168
+ . filter ( Boolean ) ,
169
+ invalid : patterns . invalid
170
+ . map ( pattern => modifyPattern ( ruleId , pattern ) )
171
+ . filter ( Boolean ) ,
125
172
} )
126
173
}
127
174
0 commit comments