33 * @copyright 2016 Toru Nagashima. All rights reserved.
44 * See LICENSE file in root directory for full license.
55 */
6- "use strict"
76
87//------------------------------------------------------------------------------
98// Requirements
109//------------------------------------------------------------------------------
1110
12- const assert = require ( "assert" )
13- const path = require ( "path" )
14- const fs = require ( "fs-extra" )
15- const parse = require ( "../src" ) . parse
16- const parseForESLint = require ( "../src" ) . parseForESLint
17- const eslint = require ( "eslint" )
11+ import type { Rule } from "eslint"
12+ import path from "node:path"
13+ import { describe , it , assert , beforeEach , afterEach } from "vitest"
14+ import * as tsParser from "@typescript-eslint/parser"
15+ import fs from "fs-extra"
16+ import * as eslint from "eslint"
17+ import { parse , parseForESLint } from "../src"
18+ import * as parser from "../src"
19+ import type { Node , VAttribute , VElement , VText } from "../src/ast"
1820
1921//------------------------------------------------------------------------------
2022// Helpers
2123//------------------------------------------------------------------------------
2224
25+ // eslint-disable-next-line no-undef
2326const ORIGINAL_FIXTURE_DIR = path . join ( __dirname , "fixtures" )
27+ // eslint-disable-next-line no-undef
2428const FIXTURE_DIR = path . join ( __dirname , "temp" )
25- const parser = require ( "../src/index.ts" )
2629
2730const BABEL_PARSER_OPTIONS = {
2831 parser : "@babel/eslint-parser" ,
@@ -356,7 +359,7 @@ describe("Basic tests", async () => {
356359 parser,
357360 globals : { } ,
358361 parserOptions : {
359- parser : require ( "@typescript-eslint/parser" ) ,
362+ parser : tsParser ,
360363 } ,
361364 } ,
362365 rules : { semi : [ "error" , "never" ] } ,
@@ -379,7 +382,7 @@ describe("Basic tests", async () => {
379382 globals : { } ,
380383 parserOptions : {
381384 parser : {
382- ts : require ( "@typescript-eslint/parser" ) ,
385+ ts : tsParser ,
383386 } ,
384387 } ,
385388 } ,
@@ -601,8 +604,8 @@ describe("Basic tests", async () => {
601604 describe ( "About unexpected-null-character errors" , ( ) => {
602605 it ( "should keep NULL in DATA state." , ( ) => {
603606 const ast = parse ( "<template>\u0000</template>" )
604- const text = ast . templateBody . children [ 0 ]
605- const errors = ast . templateBody . errors
607+ const text = ast . templateBody ! . children [ 0 ] as VText
608+ const errors = ast . templateBody ! . errors
606609
607610 assert . strictEqual ( text . value , "\u0000" )
608611 assert . strictEqual ( errors . length , 1 )
@@ -613,8 +616,9 @@ describe("Basic tests", async () => {
613616 const ast = parse (
614617 "<template><textarea>\u0000</textarea></template>" ,
615618 )
616- const text = ast . templateBody . children [ 0 ] . children [ 0 ]
617- const errors = ast . templateBody . errors
619+ const text = ( ast . templateBody ! . children [ 0 ] as VElement )
620+ . children [ 0 ] as VText
621+ const errors = ast . templateBody ! . errors
618622
619623 assert . strictEqual ( text . value , "\uFFFD" )
620624 assert . strictEqual ( errors . length , 1 )
@@ -623,8 +627,9 @@ describe("Basic tests", async () => {
623627
624628 it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in RAWTEXT state." , ( ) => {
625629 const ast = parse ( "<template><style>\u0000</style></template>" )
626- const text = ast . templateBody . children [ 0 ] . children [ 0 ]
627- const errors = ast . templateBody . errors
630+ const text = ( ast . templateBody ! . children [ 0 ] as VElement )
631+ . children [ 0 ] as VText
632+ const errors = ast . templateBody ! . errors
628633
629634 assert . strictEqual ( text . value , "\uFFFD" )
630635 assert . strictEqual ( errors . length , 1 )
@@ -633,8 +638,8 @@ describe("Basic tests", async () => {
633638
634639 it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in TAG_NAME state." , ( ) => {
635640 const ast = parse ( "<template><test\u0000></template>" )
636- const element = ast . templateBody . children [ 0 ]
637- const errors = ast . templateBody . errors
641+ const element = ast . templateBody ! . children [ 0 ] as VElement
642+ const errors = ast . templateBody ! . errors
638643
639644 assert . strictEqual ( element . name , "test\uFFFD" )
640645 assert . strictEqual ( errors . length , 1 )
@@ -643,9 +648,9 @@ describe("Basic tests", async () => {
643648
644649 it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in ATTRIBUTE_NAME state." , ( ) => {
645650 const ast = parse ( "<template><div a\u0000></div></template>" )
646- const attribute =
647- ast . templateBody . children [ 0 ] . startTag . attributes [ 0 ]
648- const errors = ast . templateBody . errors
651+ const attribute = ( ast . templateBody ! . children [ 0 ] as VElement )
652+ . startTag . attributes [ 0 ]
653+ const errors = ast . templateBody ! . errors
649654
650655 assert . strictEqual ( attribute . key . name , "a\uFFFD" )
651656 assert . strictEqual ( errors . length , 1 )
@@ -654,41 +659,41 @@ describe("Basic tests", async () => {
654659
655660 it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in ATTRIBUTE_VALUE_DOUBLE_QUOTED state." , ( ) => {
656661 const ast = parse ( '<template><div a="\u0000"></div></template>' )
657- const attribute =
658- ast . templateBody . children [ 0 ] . startTag . attributes [ 0 ]
659- const errors = ast . templateBody . errors
662+ const attribute = ( ast . templateBody ! . children [ 0 ] as VElement )
663+ . startTag . attributes [ 0 ] as VAttribute
664+ const errors = ast . templateBody ! . errors
660665
661- assert . strictEqual ( attribute . value . value , "\uFFFD" )
666+ assert . strictEqual ( attribute . value ! . value , "\uFFFD" )
662667 assert . strictEqual ( errors . length , 1 )
663668 assert . strictEqual ( errors [ 0 ] . code , "unexpected-null-character" )
664669 } )
665670
666671 it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in ATTRIBUTE_VALUE_SINGLE_QUOTED state." , ( ) => {
667672 const ast = parse ( "<template><div a='\u0000'></div></template>" )
668- const attribute =
669- ast . templateBody . children [ 0 ] . startTag . attributes [ 0 ]
670- const errors = ast . templateBody . errors
673+ const attribute = ( ast . templateBody ! . children [ 0 ] as VElement )
674+ . startTag . attributes [ 0 ] as VAttribute
675+ const errors = ast . templateBody ! . errors
671676
672- assert . strictEqual ( attribute . value . value , "\uFFFD" )
677+ assert . strictEqual ( attribute . value ! . value , "\uFFFD" )
673678 assert . strictEqual ( errors . length , 1 )
674679 assert . strictEqual ( errors [ 0 ] . code , "unexpected-null-character" )
675680 } )
676681
677682 it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in ATTRIBUTE_VALUE_UNQUOTED state." , ( ) => {
678683 const ast = parse ( "<template><div a=\u0000></div></template>" )
679- const attribute =
680- ast . templateBody . children [ 0 ] . startTag . attributes [ 0 ]
681- const errors = ast . templateBody . errors
684+ const attribute = ( ast . templateBody ! . children [ 0 ] as VElement )
685+ . startTag . attributes [ 0 ] as VAttribute
686+ const errors = ast . templateBody ! . errors
682687
683- assert . strictEqual ( attribute . value . value , "\uFFFD" )
688+ assert . strictEqual ( attribute . value ! . value , "\uFFFD" )
684689 assert . strictEqual ( errors . length , 1 )
685690 assert . strictEqual ( errors [ 0 ] . code , "unexpected-null-character" )
686691 } )
687692
688693 it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in COMMENT state." , ( ) => {
689694 const ast = parse ( "<template><!-- \u0000 --></template>" )
690- const comment = ast . templateBody . comments [ 0 ]
691- const errors = ast . templateBody . errors
695+ const comment = ast . templateBody ! . comments [ 0 ]
696+ const errors = ast . templateBody ! . errors
692697
693698 assert . strictEqual ( comment . value , " \uFFFD " )
694699 assert . strictEqual ( errors . length , 1 )
@@ -697,8 +702,8 @@ describe("Basic tests", async () => {
697702
698703 it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in BOGUS_COMMENT state." , ( ) => {
699704 const ast = parse ( "<template><? \u0000 ?></template>" )
700- const comment = ast . templateBody . comments [ 0 ]
701- const errors = ast . templateBody . errors
705+ const comment = ast . templateBody ! . comments [ 0 ]
706+ const errors = ast . templateBody ! . errors
702707
703708 assert . strictEqual ( comment . value , "? \uFFFD ?" )
704709 assert . strictEqual ( errors . length , 1 )
@@ -710,8 +715,9 @@ describe("Basic tests", async () => {
710715
711716 it ( "should not error in CDATA section state." , ( ) => {
712717 const ast = parse ( "<template><svg><![CDATA[\u0000]]></template>" )
713- const cdata = ast . templateBody . children [ 0 ] . children [ 0 ]
714- const errors = ast . templateBody . errors
718+ const cdata = ( ast . templateBody ! . children [ 0 ] as VElement )
719+ . children [ 0 ] as VText
720+ const errors = ast . templateBody ! . errors
715721
716722 assert . strictEqual ( cdata . value , "\u0000" )
717723 assert . strictEqual ( errors . length , 0 )
@@ -736,7 +742,7 @@ describe("Basic tests", async () => {
736742
737743 describe ( "https://github.com/vuejs/vue-eslint-parser/issues/21" , ( ) => {
738744 it ( "should make the correct location of decorators" , ( ) => {
739- const code = fs . readFileSync (
745+ const code : string = fs . readFileSync (
740746 path . join ( FIXTURE_DIR , "issue21.vue" ) ,
741747 "utf8" ,
742748 )
@@ -753,7 +759,8 @@ describe("Basic tests", async () => {
753759
754760 assert . strictEqual ( ast . body [ 2 ] . range [ 0 ] , indexOfDecorator )
755761 assert . strictEqual (
756- ast . body [ 2 ] . decorators [ 0 ] . range [ 0 ] ,
762+ // TSESLintClassDeclaration
763+ ( ast . body [ 2 ] as any ) . decorators [ 0 ] . range [ 0 ] ,
757764 indexOfDecorator ,
758765 )
759766 } )
@@ -762,15 +769,15 @@ describe("Basic tests", async () => {
762769 describe ( "parserServices.defineTemplateBodyVisitor" , ( ) => {
763770 it ( "should work even if AST object was reused." , ( ) => {
764771 const code = "<template><div/></template>"
765- const config = {
772+ const config : eslint . Linter . Config = {
766773 languageOptions : {
767774 parser,
768775 } ,
769776 plugins : buildPlugins ( {
770777 create ( context ) {
771778 return context . sourceCode . parserServices . defineTemplateBodyVisitor (
772779 {
773- "VElement[name='div']" ( node ) {
780+ "VElement[name='div']" ( node : VElement ) {
774781 context . report ( { node, message : "OK" } )
775782 } ,
776783 } ,
@@ -793,15 +800,15 @@ describe("Basic tests", async () => {
793800
794801 it ( "should work even if used sibling selector." , ( ) => {
795802 const code = "<template><div/><div/></template>"
796- const config = {
803+ const config : eslint . Linter . Config = {
797804 languageOptions : {
798805 parser,
799806 } ,
800807 plugins : buildPlugins ( {
801808 create ( context ) {
802809 return context . sourceCode . parserServices . defineTemplateBodyVisitor (
803810 {
804- "* ~ *" ( node ) {
811+ "* ~ *" ( node : Node ) {
805812 context . report ( {
806813 node,
807814 message : "OK" ,
@@ -846,7 +853,7 @@ describe("Basic tests", async () => {
846853 } )
847854 it ( "should notify parsing error #2" , ( ) => {
848855 const code = "<script>var a = `</script><script setup>`</script>"
849- const config = {
856+ const config : eslint . Linter . Config = {
850857 languageOptions : {
851858 parser,
852859 parserOptions : {
@@ -882,7 +889,7 @@ describe("Basic tests", async () => {
882889 it ( "should notify 1 no-undef error" , ( ) => {
883890 const code =
884891 "<script>var a = 1, b = 2;</script><script setup>c = a + b</script>"
885- const config = {
892+ const config : eslint . Linter . Config = {
886893 languageOptions : {
887894 parser,
888895 } ,
@@ -917,7 +924,7 @@ export default {}
917924</template>`
918925
919926 const result = parseForESLint ( code , { sourceType : "module" } )
920- const comments = result . ast . comments
927+ const comments = result . ast . comments !
921928
922929 // Should have 2 comments
923930 assert . strictEqual ( comments . length , 2 )
@@ -937,7 +944,7 @@ export default {}
937944 } )
938945} )
939946
940- function buildPlugins ( rule ) {
947+ function buildPlugins ( rule : Rule . RuleModule ) {
941948 return {
942949 test : {
943950 rules : {
0 commit comments