@@ -14,12 +14,22 @@ import {
14
14
import type ts from "typescript" ;
15
15
import type ESTree from "estree" ;
16
16
import * as tsESLintParser from "@typescript-eslint/parser" ;
17
+ import type { SourceLocation } from "../src/ast" ;
17
18
18
19
const ERROR_FIXTURE_ROOT = path . resolve (
19
20
__dirname ,
20
21
"../tests/fixtures/parser/error" ,
21
22
) ;
22
23
24
+ const STYLE_CONTEXT_FIXTURE_ROOT = path . resolve (
25
+ __dirname ,
26
+ "../tests/fixtures/parser/style-context" ,
27
+ ) ;
28
+ const STYLE_LOCATION_FIXTURE_ROOT = path . resolve (
29
+ __dirname ,
30
+ "../tests/fixtures/parser/style-location-converter" ,
31
+ ) ;
32
+
23
33
const RULES = [
24
34
"no-unused-labels" ,
25
35
"no-extra-label" ,
@@ -128,6 +138,61 @@ for (const { input, inputFileName, outputFileName, config } of listupFixtures(
128
138
}
129
139
}
130
140
141
+ for ( const { input, inputFileName, outputFileName, config } of listupFixtures (
142
+ STYLE_CONTEXT_FIXTURE_ROOT ,
143
+ ) ) {
144
+ const result = parse ( input , inputFileName , config ) ;
145
+ const styleContext = result . services . getStyleContext ( ) ;
146
+ fs . writeFileSync (
147
+ outputFileName ,
148
+ `${ styleContextToJson ( styleContext ) } \n` ,
149
+ "utf8" ,
150
+ ) ;
151
+ }
152
+
153
+ /** StyleContext to JSON string */
154
+ function styleContextToJson ( styleContext : parser . StyleContext ) : string {
155
+ return JSON . stringify ( styleContext , nodeReplacer , 2 ) ;
156
+
157
+ /** JSON string replacer for StyleContext */
158
+ function nodeReplacer ( key : string , value : any ) : any {
159
+ if ( key === "file" || key === "url" ) {
160
+ return undefined ;
161
+ }
162
+ return value ;
163
+ }
164
+ }
165
+
166
+ for ( const { input, inputFileName, outputFileName, config } of listupFixtures (
167
+ STYLE_LOCATION_FIXTURE_ROOT ,
168
+ ) ) {
169
+ const services = parse ( input , inputFileName , config ) . services ;
170
+ const styleContext = services . getStyleContext ( ) ;
171
+ if ( styleContext . status !== "success" ) {
172
+ continue ;
173
+ }
174
+ const locations : [
175
+ Partial < SourceLocation > ,
176
+ [ number | undefined , number | undefined ] ,
177
+ ] [ ] = [
178
+ [
179
+ services . styleNodeLoc ( styleContext . sourceAst ) ,
180
+ services . styleNodeRange ( styleContext . sourceAst ) ,
181
+ ] ,
182
+ ] ;
183
+ styleContext . sourceAst . walk ( ( node ) => {
184
+ locations . push ( [
185
+ services . styleNodeLoc ( node ) ,
186
+ services . styleNodeRange ( node ) ,
187
+ ] ) ;
188
+ } ) ;
189
+ fs . writeFileSync (
190
+ outputFileName ,
191
+ `${ JSON . stringify ( locations , undefined , 2 ) } \n` ,
192
+ "utf8" ,
193
+ ) ;
194
+ }
195
+
131
196
// eslint-disable-next-line require-jsdoc -- X
132
197
function createLinter ( ) {
133
198
const linter = new Linter ( ) ;
0 commit comments