3
3
* @copyright 2016 Toru Nagashima. All rights reserved.
4
4
* See LICENSE file in root directory for full license.
5
5
*/
6
- "use strict"
7
6
8
7
//------------------------------------------------------------------------------
9
8
// Requirements
10
9
//------------------------------------------------------------------------------
11
10
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"
18
20
19
21
//------------------------------------------------------------------------------
20
22
// Helpers
21
23
//------------------------------------------------------------------------------
22
24
25
+ // eslint-disable-next-line no-undef
23
26
const ORIGINAL_FIXTURE_DIR = path . join ( __dirname , "fixtures" )
27
+ // eslint-disable-next-line no-undef
24
28
const FIXTURE_DIR = path . join ( __dirname , "temp" )
25
- const parser = require ( "../src/index.ts" )
26
29
27
30
const BABEL_PARSER_OPTIONS = {
28
31
parser : "@babel/eslint-parser" ,
@@ -356,7 +359,7 @@ describe("Basic tests", async () => {
356
359
parser,
357
360
globals : { } ,
358
361
parserOptions : {
359
- parser : require ( "@typescript-eslint/parser" ) ,
362
+ parser : tsParser ,
360
363
} ,
361
364
} ,
362
365
rules : { semi : [ "error" , "never" ] } ,
@@ -379,7 +382,7 @@ describe("Basic tests", async () => {
379
382
globals : { } ,
380
383
parserOptions : {
381
384
parser : {
382
- ts : require ( "@typescript-eslint/parser" ) ,
385
+ ts : tsParser ,
383
386
} ,
384
387
} ,
385
388
} ,
@@ -601,8 +604,8 @@ describe("Basic tests", async () => {
601
604
describe ( "About unexpected-null-character errors" , ( ) => {
602
605
it ( "should keep NULL in DATA state." , ( ) => {
603
606
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
606
609
607
610
assert . strictEqual ( text . value , "\u0000" )
608
611
assert . strictEqual ( errors . length , 1 )
@@ -613,8 +616,9 @@ describe("Basic tests", async () => {
613
616
const ast = parse (
614
617
"<template><textarea>\u0000</textarea></template>" ,
615
618
)
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
618
622
619
623
assert . strictEqual ( text . value , "\uFFFD" )
620
624
assert . strictEqual ( errors . length , 1 )
@@ -623,8 +627,9 @@ describe("Basic tests", async () => {
623
627
624
628
it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in RAWTEXT state." , ( ) => {
625
629
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
628
633
629
634
assert . strictEqual ( text . value , "\uFFFD" )
630
635
assert . strictEqual ( errors . length , 1 )
@@ -633,8 +638,8 @@ describe("Basic tests", async () => {
633
638
634
639
it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in TAG_NAME state." , ( ) => {
635
640
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
638
643
639
644
assert . strictEqual ( element . name , "test\uFFFD" )
640
645
assert . strictEqual ( errors . length , 1 )
@@ -643,9 +648,9 @@ describe("Basic tests", async () => {
643
648
644
649
it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in ATTRIBUTE_NAME state." , ( ) => {
645
650
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
649
654
650
655
assert . strictEqual ( attribute . key . name , "a\uFFFD" )
651
656
assert . strictEqual ( errors . length , 1 )
@@ -654,41 +659,41 @@ describe("Basic tests", async () => {
654
659
655
660
it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in ATTRIBUTE_VALUE_DOUBLE_QUOTED state." , ( ) => {
656
661
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
660
665
661
- assert . strictEqual ( attribute . value . value , "\uFFFD" )
666
+ assert . strictEqual ( attribute . value ! . value , "\uFFFD" )
662
667
assert . strictEqual ( errors . length , 1 )
663
668
assert . strictEqual ( errors [ 0 ] . code , "unexpected-null-character" )
664
669
} )
665
670
666
671
it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in ATTRIBUTE_VALUE_SINGLE_QUOTED state." , ( ) => {
667
672
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
671
676
672
- assert . strictEqual ( attribute . value . value , "\uFFFD" )
677
+ assert . strictEqual ( attribute . value ! . value , "\uFFFD" )
673
678
assert . strictEqual ( errors . length , 1 )
674
679
assert . strictEqual ( errors [ 0 ] . code , "unexpected-null-character" )
675
680
} )
676
681
677
682
it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in ATTRIBUTE_VALUE_UNQUOTED state." , ( ) => {
678
683
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
682
687
683
- assert . strictEqual ( attribute . value . value , "\uFFFD" )
688
+ assert . strictEqual ( attribute . value ! . value , "\uFFFD" )
684
689
assert . strictEqual ( errors . length , 1 )
685
690
assert . strictEqual ( errors [ 0 ] . code , "unexpected-null-character" )
686
691
} )
687
692
688
693
it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in COMMENT state." , ( ) => {
689
694
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
692
697
693
698
assert . strictEqual ( comment . value , " \uFFFD " )
694
699
assert . strictEqual ( errors . length , 1 )
@@ -697,8 +702,8 @@ describe("Basic tests", async () => {
697
702
698
703
it ( "should replace NULL by U+FFFD REPLACEMENT CHARACTER in BOGUS_COMMENT state." , ( ) => {
699
704
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
702
707
703
708
assert . strictEqual ( comment . value , "? \uFFFD ?" )
704
709
assert . strictEqual ( errors . length , 1 )
@@ -710,8 +715,9 @@ describe("Basic tests", async () => {
710
715
711
716
it ( "should not error in CDATA section state." , ( ) => {
712
717
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
715
721
716
722
assert . strictEqual ( cdata . value , "\u0000" )
717
723
assert . strictEqual ( errors . length , 0 )
@@ -736,7 +742,7 @@ describe("Basic tests", async () => {
736
742
737
743
describe ( "https://github.com/vuejs/vue-eslint-parser/issues/21" , ( ) => {
738
744
it ( "should make the correct location of decorators" , ( ) => {
739
- const code = fs . readFileSync (
745
+ const code : string = fs . readFileSync (
740
746
path . join ( FIXTURE_DIR , "issue21.vue" ) ,
741
747
"utf8" ,
742
748
)
@@ -753,7 +759,8 @@ describe("Basic tests", async () => {
753
759
754
760
assert . strictEqual ( ast . body [ 2 ] . range [ 0 ] , indexOfDecorator )
755
761
assert . strictEqual (
756
- ast . body [ 2 ] . decorators [ 0 ] . range [ 0 ] ,
762
+ // TSESLintClassDeclaration
763
+ ( ast . body [ 2 ] as any ) . decorators [ 0 ] . range [ 0 ] ,
757
764
indexOfDecorator ,
758
765
)
759
766
} )
@@ -762,15 +769,15 @@ describe("Basic tests", async () => {
762
769
describe ( "parserServices.defineTemplateBodyVisitor" , ( ) => {
763
770
it ( "should work even if AST object was reused." , ( ) => {
764
771
const code = "<template><div/></template>"
765
- const config = {
772
+ const config : eslint . Linter . Config = {
766
773
languageOptions : {
767
774
parser,
768
775
} ,
769
776
plugins : buildPlugins ( {
770
777
create ( context ) {
771
778
return context . sourceCode . parserServices . defineTemplateBodyVisitor (
772
779
{
773
- "VElement[name='div']" ( node ) {
780
+ "VElement[name='div']" ( node : VElement ) {
774
781
context . report ( { node, message : "OK" } )
775
782
} ,
776
783
} ,
@@ -793,15 +800,15 @@ describe("Basic tests", async () => {
793
800
794
801
it ( "should work even if used sibling selector." , ( ) => {
795
802
const code = "<template><div/><div/></template>"
796
- const config = {
803
+ const config : eslint . Linter . Config = {
797
804
languageOptions : {
798
805
parser,
799
806
} ,
800
807
plugins : buildPlugins ( {
801
808
create ( context ) {
802
809
return context . sourceCode . parserServices . defineTemplateBodyVisitor (
803
810
{
804
- "* ~ *" ( node ) {
811
+ "* ~ *" ( node : Node ) {
805
812
context . report ( {
806
813
node,
807
814
message : "OK" ,
@@ -846,7 +853,7 @@ describe("Basic tests", async () => {
846
853
} )
847
854
it ( "should notify parsing error #2" , ( ) => {
848
855
const code = "<script>var a = `</script><script setup>`</script>"
849
- const config = {
856
+ const config : eslint . Linter . Config = {
850
857
languageOptions : {
851
858
parser,
852
859
parserOptions : {
@@ -882,7 +889,7 @@ describe("Basic tests", async () => {
882
889
it ( "should notify 1 no-undef error" , ( ) => {
883
890
const code =
884
891
"<script>var a = 1, b = 2;</script><script setup>c = a + b</script>"
885
- const config = {
892
+ const config : eslint . Linter . Config = {
886
893
languageOptions : {
887
894
parser,
888
895
} ,
@@ -917,7 +924,7 @@ export default {}
917
924
</template>`
918
925
919
926
const result = parseForESLint ( code , { sourceType : "module" } )
920
- const comments = result . ast . comments
927
+ const comments = result . ast . comments !
921
928
922
929
// Should have 2 comments
923
930
assert . strictEqual ( comments . length , 2 )
@@ -937,7 +944,7 @@ export default {}
937
944
} )
938
945
} )
939
946
940
- function buildPlugins ( rule ) {
947
+ function buildPlugins ( rule : Rule . RuleModule ) {
941
948
return {
942
949
test : {
943
950
rules : {
0 commit comments