import type { AstPath , Doc , Options , Parser , ParserOptions , Plugin , Printer , RequiredOptions , SupportLanguage , SupportOptions , } from 'prettier' ; import type { Token } from 'pug-lexer' ; import lex from 'pug-lexer' ; import type { Logger } from './logger' ; import { createLogger , LogLevel } from './logger' ; import type { PugParserOptions } from './options' ; import { options as pugOptions } from './options' ; import { convergeOptions } from './options/converge' ; import type { PugPrinterOptions } from './printer' ; import { PugPrinter } from './printer' ; const logger = createLogger ( console ) ; if ( process . env . NODE_ENV === 'test' ) { logger . setLogLevel ( LogLevel . DEBUG ) ; } /** Ast path stack entry. */ interface AstPathStackEntry { content : string ; tokens : Token [ ] ; } /** The plugin object that is picked up by prettier. */ export const plugin = { languages : [ { name : 'Pug' , parsers : [ 'pug' ] , tmScope : 'text.jade' , aceMode : 'jade' , codemirrorMode : 'pug' , codemirrorMimeType : 'text/x-pug' , extensions : [ '.jade' , '.pug' ] , linguistLanguageId : 179 , vscodeLanguageIds : [ 'jade' , 'pug' ] , } , ] , parsers : { pug : { parse ( text , parsers , options ) { logger . debug ( '[parsers:pug:parse]:' , { text } ) ; let trimmedAndAlignedContent = text . replace ( /^\s*\n/ , '' ) ; const contentIndentation = /^\s*/ . exec ( trimmedAndAlignedContent ) ; if ( contentIndentation ?. [ 0 ] ) { const contentIndentationRegex = new RegExp ( `(^|\\n)${ contentIndentation [ 0 ] }` , 'g' ) ; trimmedAndAlignedContent = trimmedAndAlignedContent . replace ( contentIndentationRegex , '$1' ) ; } const content = trimmedAndAlignedContent ; const tokens = lex ( content ) ; return { content , tokens } ; } , astFormat : 'pug-ast' , hasPragma ( text ) { return ( text . startsWith ( '//- @prettier\n' ) || text . startsWith ( '//- @format\n' ) ) ; } , locStart ( node ) { logger . debug ( '[parsers:pug:locStart]:' , { node } ) ; return 0 ; } , locEnd ( node ) { logger . debug ( '[parsers:pug:locEnd]:' , { node } ) ; return 0 ; } , preprocess ( text , options ) { logger . debug ( '[parsers:pug:preprocess]:' , { text } ) ; return text ; } , } , } , printers : { 'pug-ast' : { print ( path , options , print ) { const entry = path . stack [ 0 ] ; const { content , tokens } = entry ; const pugOptions = convergeOptions ( options ) ; const printer = new PugPrinter ( content , tokens , pugOptions ) ; const result = printer . build ( ) ; logger . debug ( '[printers:pug-ast:print]:' , result ) ; return result ; } , embed ( path , print , textToDoc , options ) { return null ; } , insertPragma ( text ) { return `//- @prettier\n${ text }` ; } , } , } , options : pugOptions , defaultOptions : { } , } ; /** The languages that are picked up by prettier. */ export const languages = plugin . languages ; /** The parsers object that is picked up by prettier. */ export const parsers = plugin . parsers ; /** The printers object that is picked up by prettier. */ export const printers = plugin . printers ; /** The options object that is picked up by prettier. */ export const options = plugin . options ; /** The default options object that is picked up by prettier. */ export const defaultOptions = plugin . defaultOptions ;