1- import { isBoolean , isString } from '@intlify/shared'
1+ import { isString } from '@intlify/shared'
22import { SourceMapGenerator , RawSourceMap } from 'source-map'
33import {
44 ResourceNode ,
@@ -30,6 +30,8 @@ type CodeGenContext = {
3030 indentLevel : number
3131 filename : string
3232 line : number
33+ breakLineCode : string
34+ needIndent : boolean
3335 column : number
3436 offset : number
3537 map ?: SourceMapGenerator
@@ -46,17 +48,23 @@ type CodeGenNode =
4648type CodeGenerator = {
4749 context ( ) : CodeGenContext
4850 push ( code : string , node ?: CodeGenNode ) : void
49- indent ( ) : void
50- deindent ( withoutNewLine ?: boolean ) : void
51+ indent ( withNewLine ?: boolean ) : void
52+ deindent ( withNewLine ?: boolean ) : void
5153 newline ( ) : void
5254 helper ( key : string ) : string
55+ needIndent ( ) : boolean
5356}
5457
5558function createCodeGenerator (
5659 ast : ResourceNode ,
5760 options : CodeGenOptions
5861) : CodeGenerator {
59- const { sourceMap, filename } = options
62+ const {
63+ sourceMap,
64+ filename,
65+ breakLineCode,
66+ needIndent : _needIndent
67+ } = options
6068 const _context = {
6169 source : ast . loc ! . source ,
6270 filename,
@@ -65,6 +73,8 @@ function createCodeGenerator(
6573 line : 1 ,
6674 offset : 0 ,
6775 map : undefined ,
76+ breakLineCode,
77+ needIndent : _needIndent ,
6878 indentLevel : 0
6979 } as CodeGenContext
7080
@@ -80,27 +90,27 @@ function createCodeGenerator(
8090 }
8191 }
8292
83- function _newline ( n : number ) : void {
84- push ( '\n' + ` ` . repeat ( n ) )
93+ function _newline ( n : number , withBreakLine = true ) : void {
94+ const _breakLineCode = withBreakLine ? breakLineCode : ''
95+ push ( _needIndent ? _breakLineCode ! + ` ` . repeat ( n ) : _breakLineCode ! )
8596 }
8697
87- function indent ( ) : void {
88- _newline ( ++ _context . indentLevel )
98+ function indent ( withNewLine = true ) : void {
99+ const level = ++ _context . indentLevel
100+ withNewLine && _newline ( level )
89101 }
90102
91- function deindent ( withoutNewLine ?: boolean ) : void {
92- if ( withoutNewLine ) {
93- -- _context . indentLevel
94- } else {
95- _newline ( -- _context . indentLevel )
96- }
103+ function deindent ( withNewLine = true ) : void {
104+ const level = -- _context . indentLevel
105+ withNewLine && _newline ( level )
97106 }
98107
99108 function newline ( ) : void {
100109 _newline ( _context . indentLevel )
101110 }
102111
103112 const helper = ( key : string ) : string => `_${ key } `
113+ const needIndent = ( ) : boolean => _context . needIndent
104114
105115 function addMapping ( loc : Position , name ?: string ) {
106116 _context . map ! . addMapping ( {
@@ -128,7 +138,8 @@ function createCodeGenerator(
128138 indent,
129139 deindent,
130140 newline,
131- helper
141+ helper,
142+ needIndent
132143 }
133144}
134145
@@ -147,9 +158,9 @@ function generateMessageNode(
147158 generator : CodeGenerator ,
148159 node : MessageNode
149160) : void {
150- const { helper } = generator
161+ const { helper, needIndent } = generator
151162 generator . push ( `${ helper ( HelperNameMap . NORMALIZE ) } ([` )
152- generator . indent ( )
163+ generator . indent ( needIndent ( ) )
153164 const length = node . items . length
154165 for ( let i = 0 ; i < length ; i ++ ) {
155166 generateNode ( generator , node . items [ i ] )
@@ -158,15 +169,15 @@ function generateMessageNode(
158169 }
159170 generator . push ( ', ' )
160171 }
161- generator . deindent ( )
172+ generator . deindent ( needIndent ( ) )
162173 generator . push ( '])' )
163174}
164175
165176function generatePluralNode ( generator : CodeGenerator , node : PluralNode ) : void {
166- const { helper } = generator
177+ const { helper, needIndent } = generator
167178 if ( node . cases . length > 1 ) {
168179 generator . push ( `${ helper ( HelperNameMap . PLURAL ) } ([` )
169- generator . indent ( )
180+ generator . indent ( needIndent ( ) )
170181 const length = node . cases . length
171182 for ( let i = 0 ; i < length ; i ++ ) {
172183 generateNode ( generator , node . cases [ i ] )
@@ -175,7 +186,7 @@ function generatePluralNode(generator: CodeGenerator, node: PluralNode): void {
175186 }
176187 generator . push ( ', ' )
177188 }
178- generator . deindent ( )
189+ generator . deindent ( needIndent ( ) )
179190 generator . push ( `])` )
180191 }
181192}
@@ -256,12 +267,25 @@ export const generate = (
256267 const filename = isString ( options . filename )
257268 ? options . filename
258269 : 'message.intl'
259- const sourceMap = isBoolean ( options . sourceMap ) ? options . sourceMap : false
270+ const sourceMap = ! ! options . sourceMap
271+ // prettier-ignore
272+ const breakLineCode = options . breakLineCode != null
273+ ? options . breakLineCode
274+ : mode === 'arrow'
275+ ? ';'
276+ : '\n'
277+ const needIndent = options . needIndent ? options . needIndent : mode !== 'arrow'
260278 const helpers = ast . helpers || [ ]
261- const generator = createCodeGenerator ( ast , { mode, filename, sourceMap } )
279+ const generator = createCodeGenerator ( ast , {
280+ mode,
281+ filename,
282+ sourceMap,
283+ breakLineCode,
284+ needIndent
285+ } )
262286
263287 generator . push ( mode === 'normal' ? `function __msg__ (ctx) {` : `(ctx) => {` )
264- generator . indent ( )
288+ generator . indent ( needIndent )
265289
266290 if ( helpers . length > 0 ) {
267291 generator . push (
@@ -272,7 +296,7 @@ export const generate = (
272296
273297 generator . push ( `return ` )
274298 generateNode ( generator , ast )
275- generator . deindent ( )
299+ generator . deindent ( needIndent )
276300 generator . push ( `}` )
277301
278302 const { code, map } = generator . context ( )
0 commit comments