1- var sorter = require ( './sort-json-utils' ) ;
1+ const sorter = require ( './sort-json-utils' ) ;
2+ const babylon = require ( 'babylon' ) ;
3+ const generate = require ( '@babel/generator' ) ;
24
35function getAutoIndent ( selectedText , indent ) {
46 var lines = selectedText . split ( '\n' ) ;
@@ -175,8 +177,6 @@ function jsonToObjectString(json, needTailingComma, autoIndent, isEs6) {
175177}
176178
177179function checkIfNeedTailingComma ( objectString ) {
178- console . log ( 'objectString' ) ;
179- console . log ( objectString ) ;
180180 return objectString . match ( / , [ \s \t \n ] * \} / ) ;
181181}
182182
@@ -188,62 +188,81 @@ function checkIsObjectText(selectedText) {
188188 ) ;
189189}
190190
191+ function addAutoIndent ( text , autoIndent ) {
192+ const autoIndentString = new Array ( autoIndent ) . fill ( ' ' ) . join ( '' ) ;
193+ const lines = text . split ( '\n' ) ;
194+
195+ return [
196+ lines [ 0 ] ,
197+ ...lines
198+ . filter ( ( line , index ) => index > 0 )
199+ . map ( line => `${ autoIndentString } ${ line } ` ) ,
200+ ] . join ( '\n' ) ;
201+ }
202+
191203function selectedTextToSortedText (
192204 selectedText ,
193205 indent ,
194206 jsonParser ,
195207 sortOrder ,
196208 sortOptions
197209) {
198- var isObejct = checkIsObjectText ( selectedText ) ;
199-
200- if ( ! isObejct ) {
201- throw { message : 'Please make sure your selected text is a JS obejct!' } ;
202- }
210+ var needTailingComma = checkIfNeedTailingComma ( selectedText ) ;
203211
204212 var autoIndent = getAutoIndent ( selectedText , indent ) ;
205213
206- var marks = [ ] ;
214+ const declarPrefix = 'const a = ' ;
215+ let code = `${ declarPrefix } ${ selectedText } ` ;
207216
208- var { json, isEs6 } = objectStringToJson ( selectedText , marks , indent ) ;
209-
210- var needTailingComma = checkIfNeedTailingComma ( selectedText ) ;
211-
212- console . log ( 'jsonText' ) ;
213- console . log ( json ) ;
214-
215- console . log ( 'marks' ) ;
216- console . log ( marks ) ;
217+ let ast ;
217218
218219 try {
219- var initialJSON = sorter . textToJSON ( jsonParser , json ) ;
220+ ast = babylon . parse ( code , {
221+ sourceType : 'module' ,
222+ plugins : [ 'objectRestSpread' ] ,
223+ } ) ;
220224 } catch ( e ) {
221- if ( e . name == 'SyntaxError' ) {
222- throw { message : 'Please make sure your selected text is a JS obejct!' } ;
223- } else {
224- throw e ;
225- }
225+ throw { message : 'Please make sure your selected text is a JS obejct!' } ;
226226 }
227- var sortedJSON = sorter . sortJSON ( initialJSON , sortOrder , sortOptions ) ;
228227
229- var sortedJsonText = sorter . jsonToText ( jsonParser , sortedJSON , indent ) ;
228+ const object = ast . program . body [ 0 ] . declarations [ 0 ] . init ;
229+ object . properties = object . properties . sort ( ( a , b ) => {
230+ if ( ! a . key ) {
231+ return 1 ;
232+ }
233+
234+ if ( ! b . key ) {
235+ return - 1 ;
236+ }
230237
231- console . log ( 'sortedJsonText' ) ;
232- console . log ( sortedJsonText ) ;
233- sortedJsonText = decodeMark ( sortedJsonText , marks ) ;
238+ return a . key . name
239+ ? b . key . name
240+ ? a . key . name . localeCompare ( b . key . name )
241+ : 1
242+ : b . key . name
243+ ? - 1
244+ : a . key . value . localeCompare ( b . key . value ) ;
245+ } ) ;
246+
247+ if ( sortOrder && sortOrder . indexOf ( 'desc' ) >= 0 ) {
248+ object . properties = object . properties . reverse ( ) ;
249+ }
234250
235- console . log ( 'decoded text' ) ;
236- console . log ( sortedJsonText ) ;
251+ let sortedText = generate . default ( ast , { } , code ) . code ;
237252
238- var sortedText = jsonToObjectString (
239- sortedJsonText ,
240- needTailingComma ,
241- autoIndent ,
242- isEs6
243- ) ;
253+ sortedText = sortedText . replace ( / / g, new Array ( indent ) . fill ( ' ' ) . join ( '' ) ) ;
254+ sortedText = sortedText . slice ( declarPrefix . length , sortedText . length - 1 ) ;
255+ sortedText = addAutoIndent ( sortedText , autoIndent ) ;
244256
245- console . log ( 'sortedText' ) ;
246- console . log ( sortedText ) ;
257+ if ( needTailingComma ) {
258+ var tailingCommaRegex = / ( [ ^ \{ : , \s \t \n ] + ) ( [ \s \t \n ] * [ \] \} ] ) / g;
259+ while ( sortedText . match ( tailingCommaRegex ) ) {
260+ sortedText = sortedText . replace (
261+ tailingCommaRegex ,
262+ ( match , $1 , $2 ) => `${ $1 } ,${ $2 } `
263+ ) ;
264+ }
265+ }
247266
248267 return sortedText ;
249268}
0 commit comments