import { Root , AtRule } from "postcss" ; import { deprecatedComponentLibraryColorImport , deprecatedComponentLibraryTypeImport , deprecatedComponentLibraryLayoutImport , } from "../messages" ; import { Options , RuleDefinition } from "../types" ; type ImportToReplace = { oldImport : string ; newImport : string ; message : any ; } ; const importsToReplace = [ { oldImport : "~@kaizen/component-library/styles/type" , newImport : "~@kaizen/deprecated-component-library-helpers/styles/type" , message : deprecatedComponentLibraryTypeImport , } , { oldImport : "~@kaizen/component-library/styles/color" , newImport : "~@kaizen/deprecated-component-library-helpers/styles/color" , message : deprecatedComponentLibraryColorImport , } , { oldImport : "~@kaizen/component-library/styles/layout" , newImport : "~@kaizen/deprecated-component-library-helpers/styles/layout" , message : deprecatedComponentLibraryLayoutImport , } , ] ; export const useDeprecatedComponentLibraryHelpersScssImports = { name : "use-deprecated-component-library-helpers-scss-imports" , ruleFunction : ( styleSheetNode , options ) => { for ( const importToReplace of importsToReplace ) { replaceImport ( styleSheetNode , options , importToReplace ) ; } } , } ; const addQuotes = ( newImport , quoteCharacter ) => `${ quoteCharacter }${ newImport }${ quoteCharacter }` ; const replaceImport = ( styleSheetNode , options , { oldImport , newImport , message } ) => { const importsToProcess = [ ] ; let containsOldImport = false ; let quoteCharacter = "'" ; styleSheetNode . walkAtRules ( ( node ) => { if ( node . name === "import" ) { const line = node . params ; if ( line . includes ( oldImport ) || line . includes ( newImport ) ) { importsToProcess . push ( node ) ; if ( line . includes ( oldImport ) ) { quoteCharacter = line . includes ( "'" ) ? "'" : '"' ; containsOldImport = true ; if ( ! options . fix ) { options . reporter ( { message , node , autofixAvailable : true , } ) ; } } } } } ) ; if ( options . fix && containsOldImport ) { const [ lastImport , ... importsToRemove ] = importsToProcess . reverse ( ) ; lastImport . params = addQuotes ( newImport , quoteCharacter ) ; for ( const importToRemove of importsToRemove ) { importToRemove . remove ( ) ; } } } ;