@@ -19,7 +19,30 @@ const RuleTester = require("./fixtures/eslint/lib/testers/rule-tester")
1919
2020const RULES_ROOT = path . join ( __dirname , "fixtures/eslint/tests/lib/rules" )
2121const PARSER_PATH = path . resolve ( __dirname , "../index.js" )
22+ const EXCEPTIONS = new Set ( [
23+ // Those rules check outside `<script>` tag as well.
24+ // It cannot fix the behavior in vue-eslint-parser side.
25+ "eol-last" ,
26+ "max-len" ,
27+ "max-lines" ,
28+
29+ // Wrapper includes line-breaks, so it changed the number of errors.
30+ // It cannot test this rule correctly.
31+ "linebreak-style" ,
32+
33+ // Tests about the head/last of source code failed because "<script>" tokens
34+ // are added.
35+ // It cannot test this rule correctly.
36+ "lines-around-comment" ,
37+ "lines-around-directive" ,
38+ "newline-after-var" ,
39+ "no-multiple-empty-lines" ,
40+
41+ // The inside of "<script>" tags is not related to Unicode BOM.
42+ "unicode-bom" ,
43+ ] )
2244const originalRun = RuleTester . prototype . run
45+ const processed = new Set ( )
2346
2447/**
2548 * Wrap the given code with a `<script>` tag.
@@ -28,7 +51,11 @@ const originalRun = RuleTester.prototype.run
2851 * @returns {string } The wrapped code.
2952 */
3053function wrapCode ( code ) {
31- const eol = code . indexOf ( "\r\n" ) !== - 1 ? "\r\n" : "\n"
54+ const eol = "\n"
55+
56+ if ( code . charCodeAt ( 0 ) === 0xFEFF ) {
57+ return `\uFEFF<script>${ eol } ${ code . slice ( 1 ) } ${ eol } </script>`
58+ }
3259 return `<script>${ eol } ${ code } ${ eol } </script>`
3360}
3461
@@ -40,13 +67,19 @@ function wrapCode(code) {
4067 */
4168function modifyPattern ( pattern ) {
4269 if ( typeof pattern === "string" ) {
70+ if ( pattern . startsWith ( "#!" ) ) {
71+ return null
72+ }
4373 return {
4474 code : wrapCode ( pattern ) ,
4575 filename : "test.vue" ,
4676 parser : PARSER_PATH ,
4777 }
4878 }
49- if ( pattern . parser != null || pattern . filename != null ) {
79+ if ( pattern . parser != null ||
80+ pattern . filename != null ||
81+ pattern . code . startsWith ( "#!" )
82+ ) {
5083 return null
5184 }
5285
@@ -58,12 +91,14 @@ function modifyPattern(pattern) {
5891 }
5992 if ( Array . isArray ( pattern . errors ) ) {
6093 for ( const error of pattern . errors ) {
61- if ( typeof error === "object" ) {
94+ if ( typeof error === "object" && ! processed . has ( error ) ) {
95+ processed . add ( error )
96+
6297 if ( error . line != null ) {
63- error . line += 1
98+ error . line = Number ( error . line ) + 1
6499 }
65100 if ( error . endLine != null ) {
66- error . endLine += 1
101+ error . endLine = Number ( error . endLine ) + 1
67102 }
68103 }
69104 }
@@ -97,10 +132,17 @@ RuleTester.prototype.run = overrideRun
97132try {
98133 describe ( "Tests of ESLint core rules" , ( ) => {
99134 for ( const fileName of fs . readdirSync ( RULES_ROOT ) ) {
135+ if ( path . extname ( fileName ) !== ".js" ||
136+ EXCEPTIONS . has ( path . basename ( fileName , ".js" ) )
137+ ) {
138+ continue
139+ }
140+
100141 require ( path . join ( RULES_ROOT , fileName ) )
101142 }
102143 } )
103144}
104145finally {
105146 RuleTester . prototype . run = originalRun
147+ processed . clear ( )
106148}
0 commit comments