@@ -55,6 +55,7 @@ import {
5555 extractReturns ,
5656 extractType ,
5757} from "./extract-metadata" ;
58+ import type { DataType } from "@webdoc/types" ;
5859import { createSimpleKeywordType } from "@webdoc/model" ;
5960
6061// + Extract the symbol name, type from the Node
@@ -115,32 +116,13 @@ export default function extractSymbol(
115116 nodeSymbol . meta . scope = node . static ? "static" : "instance" ;
116117 nodeSymbol . meta . type = "PropertyDoc" ;
117118
118- if ( isLiteral ( node . value ) ) {
119- if ( isStringLiteral ( node . value ) ) {
120- // Quotes for strings
121- nodeSymbol . meta . defaultValue = `"${ node . value . value } "` ;
119+ const [ defaultValue , dataType ] = resolveDefaultValue ( node . value ) ;
122120
123- if ( ! nodeSymbol . meta . dataType ) {
124- nodeSymbol . meta . dataType = createSimpleKeywordType ( "string" ) ;
125- }
126- } else {
127- nodeSymbol . meta. defaultValue = `${ node . value . value } ` ;
121+ if ( typeof defaultValue === "string" ) {
122+ nodeSymbol . meta . defaultValue = defaultValue ;
128123
129- if ( ! nodeSymbol . meta . dataType && isNumericLiteral ( node . value ) ) {
130- nodeSymbol . meta . dataType = createSimpleKeywordType ( "number" ) ;
131- } else if ( ! nodeSymbol . meta . dataType && isBooleanLiteral ( node . value ) ) {
132- nodeSymbol . meta . dataType = createSimpleKeywordType ( "boolean" ) ;
133- }
134- }
135- } else if ( isExpression ( node . value ) ) {
136- const defaultValue = resolveExpression ( node . value ) ;
137-
138- if ( typeof defaultValue === "string" ) {
139- nodeSymbol . meta . defaultValue = defaultValue ;
140-
141- if ( ! isNaN ( parseFloat ( defaultValue ) ) ) {
142- nodeSymbol . meta . dataType = createSimpleKeywordType ( "number" ) ;
143- }
124+ if ( ! nodeSymbol . meta . dataType ) {
125+ nodeSymbol . meta . dataType = dataType ;
144126 }
145127 }
146128 } else if ( isClassDeclaration ( node ) || isClassExpression ( node ) ) {
@@ -229,6 +211,14 @@ export default function extractSymbol(
229211 nodeSymbol . meta . type = "PropertyDoc" ;
230212 }
231213
214+ // Resolve property default
215+ if ( ! isInit && init ) {
216+ const [ defaultValue , dataType ] = resolveDefaultValue ( init ) ;
217+
218+ nodeSymbol . meta . defaultValue = defaultValue ;
219+ nodeSymbol . meta . dataType = dataType ;
220+ }
221+
232222 let isExpression = false ;
233223
234224 if ( isExpressionStatement ( node ) ) {
@@ -424,6 +414,38 @@ function resolveExpression(expression: BabelNodeExpression): string | void {
424414 }
425415}
426416
417+ function resolveDefaultValue ( node : BabelNodeExpression ) : [ ?string , ?DataType ] {
418+ let defaultValue : ?string ;
419+ let dataType : ?DataType ;
420+
421+ if ( isLiteral ( node ) ) {
422+ if ( isStringLiteral ( node ) ) {
423+ // Quotes for strings
424+ defaultValue = `"${ node . value } "` ;
425+
426+ dataType = createSimpleKeywordType ( "string" ) ;
427+ } else {
428+ defaultValue = `${ node . value } ` ;
429+
430+ if ( isNumericLiteral ( node ) ) {
431+ dataType = createSimpleKeywordType ( "number" ) ;
432+ } else if ( isBooleanLiteral ( node ) ) {
433+ dataType = createSimpleKeywordType ( "boolean" ) ;
434+ }
435+ }
436+ } else if ( isExpression ( node ) ) {
437+ defaultValue = resolveExpression ( node ) ;
438+
439+ if ( typeof defaultValue === "string" ) {
440+ if ( ! isNaN ( parseFloat ( defaultValue ) ) ) {
441+ dataType = createSimpleKeywordType ( "number" ) ;
442+ }
443+ }
444+ }
445+
446+ return [ defaultValue , dataType ] ;
447+ }
448+
427449// Whether the member expression assigns to this, e.g.
428450// this.member.inside.deep -> true
429451// top.inside -> false
0 commit comments