@@ -2,7 +2,7 @@ import { createRule } from '../utils/index.js';
2
2
import { findAttribute , getLangValue } from '../utils/ast-utils.js' ;
3
3
import type { SvelteScriptElement , SvelteStyleElement } from 'svelte-eslint-parser/lib/ast' ;
4
4
import { getSourceCode } from '../utils/compat.js' ;
5
- import type { SuggestionReportDescriptor } from '../types.js' ;
5
+ import type { SuggestionReportDescriptor , SourceCode } from '../types.js' ;
6
6
7
7
export default createRule ( 'block-lang' , {
8
8
meta : {
@@ -91,20 +91,7 @@ export default createRule('block-lang', {
91
91
message : `The <script> block should be present and its lang attribute should be ${ prettyPrintLangs (
92
92
allowedScriptLangs
93
93
) } .`,
94
- suggest : allowedScriptLangs
95
- . filter ( ( lang ) => lang != null && lang !== '' )
96
- . map ( ( lang ) => {
97
- return {
98
- desc : `Add a <script> block with the lang attribute set to "${ lang } ".` ,
99
- fix : ( fixer ) => {
100
- const langAttributeText = getLangAttributeText ( lang ?? '' , true ) ;
101
- return fixer . insertTextAfterRange (
102
- [ 0 , 0 ] ,
103
- `<script${ langAttributeText } >\n</script>\n\n`
104
- ) ;
105
- }
106
- } ;
107
- } )
94
+ suggest : buildAddLangSuggestions ( allowedScriptLangs , 'script' , getSourceCode ( context ) )
108
95
} ) ;
109
96
}
110
97
for ( const scriptNode of scriptNodes ) {
@@ -114,7 +101,7 @@ export default createRule('block-lang', {
114
101
message : `The lang attribute of the <script> block should be ${ prettyPrintLangs (
115
102
allowedScriptLangs
116
103
) } .`,
117
- suggest : buildSuggestions ( allowedScriptLangs , scriptNode )
104
+ suggest : buildReplaceLangSuggestions ( allowedScriptLangs , scriptNode )
118
105
} ) ;
119
106
}
120
107
}
@@ -125,20 +112,7 @@ export default createRule('block-lang', {
125
112
message : `The <style> block should be present and its lang attribute should be ${ prettyPrintLangs (
126
113
allowedStyleLangs
127
114
) } .`,
128
- suggest : allowedStyleLangs
129
- . filter ( ( lang ) => lang != null && lang !== '' )
130
- . map ( ( lang ) => {
131
- return {
132
- desc : `Add a <style> block with the lang attribute set to "${ lang } ".` ,
133
- fix : ( fixer ) => {
134
- const langAttributeText = getLangAttributeText ( lang ?? '' , true ) ;
135
- return fixer . insertTextAfterRange (
136
- [ sourceCode . text . length , sourceCode . text . length ] ,
137
- `<style${ langAttributeText } >\n</style>\n\n`
138
- ) ;
139
- }
140
- } ;
141
- } )
115
+ suggest : buildAddLangSuggestions ( allowedStyleLangs , 'style' , sourceCode )
142
116
} ) ;
143
117
}
144
118
for ( const styleNode of styleNodes ) {
@@ -148,7 +122,7 @@ export default createRule('block-lang', {
148
122
message : `The lang attribute of the <style> block should be ${ prettyPrintLangs (
149
123
allowedStyleLangs
150
124
) } .`,
151
- suggest : buildSuggestions ( allowedStyleLangs , styleNode )
125
+ suggest : buildReplaceLangSuggestions ( allowedStyleLangs , styleNode )
152
126
} ) ;
153
127
}
154
128
}
@@ -157,7 +131,28 @@ export default createRule('block-lang', {
157
131
}
158
132
} ) ;
159
133
160
- function buildSuggestions (
134
+ function buildAddLangSuggestions (
135
+ langs : ( string | null ) [ ] ,
136
+ tagName : 'script' | 'style' ,
137
+ sourceCode : SourceCode
138
+ ) : SuggestionReportDescriptor [ ] {
139
+ return langs
140
+ . filter ( ( lang ) => lang != null && lang !== '' )
141
+ . map ( ( lang ) => {
142
+ return {
143
+ desc : `Add a lang attribute to a <${ tagName } > block with the value "${ lang } ".` ,
144
+ fix : ( fixer ) => {
145
+ const langAttributeText = getLangAttributeText ( lang ?? '' , true ) ;
146
+ return fixer . insertTextAfterRange (
147
+ tagName === 'script' ? [ 0 , 0 ] : [ sourceCode . text . length , sourceCode . text . length ] ,
148
+ `<${ tagName } ${ langAttributeText } >\n</${ tagName } >\n\n`
149
+ ) ;
150
+ }
151
+ } ;
152
+ } ) ;
153
+ }
154
+
155
+ function buildReplaceLangSuggestions (
161
156
langs : ( string | null ) [ ] ,
162
157
node : SvelteScriptElement | SvelteStyleElement
163
158
) : SuggestionReportDescriptor [ ] {
0 commit comments