@@ -16,13 +16,23 @@ import (
16
16
"gotest.tools/v3/assert"
17
17
)
18
18
19
+ type LanguageOptions struct {
20
+ ParserOptions * ParserOptions `json:"parserOptions,omitempty"`
21
+ }
22
+
23
+ type ParserOptions struct {
24
+ Project string `json:"project,omitempty"`
25
+ ProjectService bool `json:"projectService,omitempty"`
26
+ }
27
+
19
28
type ValidTestCase struct {
20
- Code string
21
- Only bool
22
- Skip bool
23
- Options any
24
- TSConfig string
25
- Tsx bool
29
+ Code string
30
+ Only bool
31
+ Skip bool
32
+ Options any
33
+ TSConfig string
34
+ Tsx bool
35
+ LanguageOptions * LanguageOptions
26
36
}
27
37
28
38
type InvalidTestCaseError struct {
@@ -40,14 +50,15 @@ type InvalidTestCaseSuggestion struct {
40
50
}
41
51
42
52
type InvalidTestCase struct {
43
- Code string
44
- Only bool
45
- Skip bool
46
- Output []string
47
- Errors []InvalidTestCaseError
48
- TSConfig string
49
- Options any
50
- Tsx bool
53
+ Code string
54
+ Only bool
55
+ Skip bool
56
+ Output []string
57
+ Errors []InvalidTestCaseError
58
+ TSConfig string
59
+ Options any
60
+ Tsx bool
61
+ LanguageOptions * LanguageOptions
51
62
}
52
63
53
64
func RunRuleTester (rootDir string , tsconfigPath string , t * testing.T , r * rule.Rule , validTestCases []ValidTestCase , invalidTestCases []InvalidTestCase ) {
@@ -56,7 +67,7 @@ func RunRuleTester(rootDir string, tsconfigPath string, t *testing.T, r *rule.Ru
56
67
onlyMode := slices .ContainsFunc (validTestCases , func (c ValidTestCase ) bool { return c .Only }) ||
57
68
slices .ContainsFunc (invalidTestCases , func (c InvalidTestCase ) bool { return c .Only })
58
69
59
- runLinter := func (t * testing.T , code string , options any , tsconfigPathOverride string , tsx bool ) []rule.RuleDiagnostic {
70
+ runLinter := func (t * testing.T , code string , options any , tsconfigPathOverride string , tsx bool , languageOptions * LanguageOptions ) []rule.RuleDiagnostic {
60
71
var diagnosticsMu sync.Mutex
61
72
diagnostics := make ([]rule.RuleDiagnostic , 0 , 3 )
62
73
@@ -72,6 +83,10 @@ func RunRuleTester(rootDir string, tsconfigPath string, t *testing.T, r *rule.Ru
72
83
if tsconfigPathOverride != "" {
73
84
tsconfigPath = tsconfigPathOverride
74
85
}
86
+ // Override with languageOptions.parserOptions.project if provided
87
+ if languageOptions != nil && languageOptions .ParserOptions != nil && languageOptions .ParserOptions .Project != "" {
88
+ tsconfigPath = tspath .ResolvePath (rootDir , languageOptions .ParserOptions .Project )
89
+ }
75
90
76
91
program , err := utils .CreateProgram (true , fs , rootDir , tsconfigPath , host )
77
92
assert .NilError (t , err , "couldn't create program. code: " + code )
@@ -86,7 +101,7 @@ func RunRuleTester(rootDir string, tsconfigPath string, t *testing.T, r *rule.Ru
86
101
func (sourceFile * ast.SourceFile ) []linter.ConfiguredRule {
87
102
return []linter.ConfiguredRule {
88
103
{
89
- Name : "test" ,
104
+ Name : r . Name ,
90
105
Severity : rule .SeverityError ,
91
106
Run : func (ctx rule.RuleContext ) rule.RuleListeners {
92
107
return r .Run (ctx , options )
@@ -114,7 +129,7 @@ func RunRuleTester(rootDir string, tsconfigPath string, t *testing.T, r *rule.Ru
114
129
t .SkipNow ()
115
130
}
116
131
117
- diagnostics := runLinter (t , testCase .Code , testCase .Options , testCase .TSConfig , testCase .Tsx )
132
+ diagnostics := runLinter (t , testCase .Code , testCase .Options , testCase .TSConfig , testCase .Tsx , testCase . LanguageOptions )
118
133
if len (diagnostics ) != 0 {
119
134
// TODO: pretty errors
120
135
t .Errorf ("Expected valid test case not to contain errors. Code:\n %v" , testCase .Code )
@@ -139,7 +154,7 @@ func RunRuleTester(rootDir string, tsconfigPath string, t *testing.T, r *rule.Ru
139
154
code := testCase .Code
140
155
141
156
for i := range 10 {
142
- diagnostics := runLinter (t , code , testCase .Options , testCase .TSConfig , testCase .Tsx )
157
+ diagnostics := runLinter (t , code , testCase .Options , testCase .TSConfig , testCase .Tsx , testCase . LanguageOptions )
143
158
if i == 0 {
144
159
initialDiagnostics = diagnostics
145
160
}
0 commit comments