22
33const { compile, walk } = require ( 'svelte/compiler' ) ;
44
5- let compiler_options , messages , transformed_code , ignore_warnings , lint_template , translations ;
5+ let compiler_options , messages , transformed_code , ignore_warnings , ignore_styles , translations ;
66
77// get the total length, number of lines, and length of the last line of a string
88const get_offsets = str => {
@@ -134,6 +134,17 @@ const transform_message = (message, { unoffsets, dedent, offsets, range }) => {
134134
135135// extract scripts to lint from component definition
136136const preprocess = text => {
137+ if ( ignore_styles ) {
138+ // wipe the appropriate <style> tags in the file
139+ text = text . replace ( / < s t y l e ( \s [ ^ ] * ?) ? > [ ^ ] * ?< \/ s t y l e > / gi, ( match , attributes = '' ) => {
140+ const attrs = { } ;
141+ attributes . split ( / \s + / ) . filter ( Boolean ) . forEach ( attr => {
142+ const [ name , value ] = attr . split ( '=' ) ;
143+ attrs [ name ] = value ? / ^ [ ' " ] / . test ( value ) ? value . slice ( 1 , - 1 ) : value : true ;
144+ } ) ;
145+ return ignore_styles ( attrs ) ? match . replace ( / \S / g, ' ' ) : match ;
146+ } ) ;
147+ }
137148 // get information about the component
138149 let result ;
139150 try {
@@ -159,7 +170,7 @@ const preprocess = text => {
159170 const reassigned_vars = vars . filter ( v => v . reassigned || v . export_name ) ;
160171
161172 // convert warnings to linting messages
162- messages = ( ignore_warnings ? warnings . filter ( warning => ! ignore_warnings ( warning . code , warning ) ) : warnings ) . map ( ( { code, message, start, end } ) => ( {
173+ messages = ( ignore_warnings ? warnings . filter ( warning => ! ignore_warnings ( warning ) ) : warnings ) . map ( ( { code, message, start, end } ) => ( {
163174 ruleId : code ,
164175 severity : 1 ,
165176 message,
@@ -206,7 +217,7 @@ const preprocess = text => {
206217 }
207218
208219 // add expressions from template to the constructed string
209- if ( lint_template && ast . html ) {
220+ if ( ast . html ) {
210221 transformed_code += '\n/* eslint-enable *//* eslint indent: 0, quotes: 0, semi: 0 */' ;
211222 // find all expressions in the AST
212223 walk ( ast . html , {
@@ -287,53 +298,27 @@ const postprocess = ([raw_messages]) => {
287298 return messages . sort ( ( a , b ) => a . line - b . line || a . column - b . column ) ;
288299} ;
289300
290- /// PATCH THE LINTER - THE PLUGIN PART OF THE PLUGIN ///
301+ /// PATCH THE LINTER - HACK TO GET ACCESS TO SETTINGS ///
291302
292303// find Linter instance
293- const linter_path = Object . keys ( require . cache ) . find ( path => path . endsWith ( '/eslint/lib/linter.js' ) || path . endsWith ( '\\eslint\\lib\\linter.js' ) ) ;
304+ const linter_path = Object . keys ( require . cache ) . find ( path => path . endsWith ( '/eslint/lib/linter/linter .js' ) || path . endsWith ( '\\eslint\\lib\\linter \\linter.js' ) ) ;
294305if ( ! linter_path ) {
295306 throw new Error ( 'Could not find ESLint Linter in require cache' ) ;
296307}
297- const Linter = require ( linter_path ) ;
298-
299- // get a setting from the ESLint config
300- const get_setting_function = ( config , key , default_value ) => {
301- if ( ! config || ! config . settings || ! ( key in config . settings ) ) {
302- return default_value ;
303- }
304- const value = config . settings [ key ] ;
305- return typeof value === 'function' ? value : Array . isArray ( value ) ? Array . prototype . includes . bind ( value ) : ( ) => value ;
306- } ;
308+ const { Linter } = require ( linter_path ) ;
307309
308310// patch Linter#verify
309311const { verify } = Linter . prototype ;
310312Linter . prototype . verify = function ( code , config , options ) {
311- if ( typeof options === 'string' ) {
312- options = { filename : options } ;
313- }
314- if ( options && options . filename ) {
315- if ( get_setting_function ( config , 'svelte3/enabled' , n => n . endsWith ( '.svelte' ) ) ( options . filename ) ) {
316- // lint this Svelte file
317- options = Object . assign ( { } , options , { preprocess, postprocess } ) ;
318- ignore_warnings = get_setting_function ( config , 'svelte3/ignore-warnings' , false ) ;
319- lint_template = get_setting_function ( config , 'svelte3/lint-template' , ( ) => false ) ( options . filename ) ;
320- const ignore_styles = get_setting_function ( config , 'svelte3/ignore-styles' , false ) ;
321- if ( ignore_styles ) {
322- // wipe the appropriate <style> tags in the file
323- code = code . replace ( / < s t y l e ( \s [ ^ ] * ?) ? > [ ^ ] * ?< \/ s t y l e > / gi, ( match , attributes = '' ) => {
324- const attrs = { } ;
325- attributes . split ( / \s + / ) . filter ( Boolean ) . forEach ( attr => {
326- const [ name , value ] = attr . split ( '=' ) ;
327- attrs [ name ] = value ? / ^ [ ' " ] / . test ( value ) ? value . slice ( 1 , - 1 ) : value : true ;
328- } ) ;
329- return ignore_styles ( attrs ) ? match . replace ( / \S / g, ' ' ) : match ;
330- } ) ;
331- }
332- const compiler_options_setting = get_setting_function ( config , 'svelte3/compiler-options' , false ) ;
333- compiler_options = compiler_options_setting ? Object . assign ( { generate : false } , compiler_options_setting ( options . filename ) ) : { generate : false } ;
334- }
335- }
336-
313+ // fetch settings
314+ const settings = config ? ( typeof config . extractConfig === 'function' ? config . extractConfig ( options . filename || options ) . settings : config . settings ) || { } : { } ;
315+ ignore_warnings = settings [ 'svelte3/ignore-warnings' ] ;
316+ ignore_styles = settings [ 'svelte3/ignore-styles' ] ;
317+ compiler_options = Object . assign ( { generate : false } , settings [ 'svelte3/compiler-options' ] ) ;
337318 // call original Linter#verify
338319 return verify . call ( this , code , config , options ) ;
339320} ;
321+
322+ /// EXPORT THE PROCESSOR ///
323+
324+ exports . processors = { svelte3 : { preprocess, postprocess, supportsAutofix : true } } ;
0 commit comments