@@ -5,6 +5,14 @@ const FACT_LIMITS = {
55 HINT_TOKENS : 80
66}
77
8+ function asArray ( value ) {
9+ return Array . isArray ( value ) ? value : [ ]
10+ }
11+
12+ function asRecord ( value ) {
13+ return value && typeof value === 'object' && ! Array . isArray ( value ) ? value : { }
14+ }
15+
816function limitList ( items , max = FACT_LIMITS . ITEMS ) {
917 if ( ! Array . isArray ( items ) ) {
1018 return [ ]
@@ -123,7 +131,7 @@ function getValueType(value) {
123131 * @returns {Array } 格式化后的数据源
124132 */
125133function formatDataSources ( dataSource , hintContext ) {
126- const candidates = dataSource
134+ const candidates = asArray ( dataSource )
127135 . filter ( ( ds ) => ds ?. name )
128136 . map ( ( ds ) => ( {
129137 name : ds . name ,
@@ -169,7 +177,7 @@ function createCallableAccess(prefix, name, functionCode) {
169177 * @returns {Array } 格式化后的工具类
170178 */
171179function formatUtils ( utils , hintContext ) {
172- const candidates = utils
180+ const candidates = asArray ( utils )
173181 . filter ( ( util ) => util ?. name )
174182 . map ( ( util ) => {
175183 const formatted = {
@@ -202,7 +210,7 @@ function formatUtils(utils, hintContext) {
202210 * @returns {Array } 格式化后的 bridge 信息
203211 */
204212function formatBridge ( bridge , hintContext ) {
205- const candidates = bridge
213+ const candidates = asArray ( bridge )
206214 . filter ( ( item ) => item ?. name )
207215 . map ( ( item ) => ( {
208216 name : item . name ,
@@ -221,7 +229,7 @@ function formatBridge(bridge, hintContext) {
221229 * @returns {Array } 格式化后的全局状态
222230 */
223231function formatGlobalState ( globalState , hintContext ) {
224- const candidates = globalState
232+ const candidates = asArray ( globalState )
225233 . filter ( ( store ) => store ?. id )
226234 . map ( ( store ) => ( {
227235 id : store . id ,
@@ -242,7 +250,7 @@ function formatGlobalState(globalState, hintContext) {
242250 * @returns {Array } 格式化后的状态
243251 */
244252function formatState ( state , hintContext ) {
245- const candidates = Object . entries ( state ) . map ( ( [ key , value ] ) => ( {
253+ const candidates = Object . entries ( asRecord ( state ) ) . map ( ( [ key , value ] ) => ( {
246254 name : key ,
247255 accessPath : `this.state.${ key } ` ,
248256 type : getValueType ( value )
@@ -259,7 +267,7 @@ function formatState(state, hintContext) {
259267 * @returns {Array } 格式化后的方法
260268 */
261269function formatMethods ( methods , hintContext ) {
262- const candidates = Object . entries ( methods ) . map ( ( [ key , value ] ) => ( {
270+ const candidates = Object . entries ( asRecord ( methods ) ) . map ( ( [ key , value ] ) => ( {
263271 name : key ,
264272 accessPath : `this.${ key } ` ,
265273 signature : value ?. type === 'JSFunction' ? createCallableAccess ( 'this.' , key , value . value ) : `this.${ key } ()` ,
@@ -287,7 +295,9 @@ function prioritizeSchemaKeys(keys, max, hintContext) {
287295 * @returns {Object|null } 格式化后的 schema
288296 */
289297function formatCurrentSchema ( schema , hintContext ) {
290- if ( ! schema ) {
298+ const normalizedSchema = schema && typeof schema === 'object' && ! Array . isArray ( schema ) ? schema : null
299+
300+ if ( ! normalizedSchema ) {
291301 return {
292302 schema : null ,
293303 truncated : {
@@ -299,8 +309,8 @@ function formatCurrentSchema(schema, hintContext) {
299309 }
300310
301311 const formatted = {
302- componentName : schema . componentName || 'Unknown' ,
303- ...( schema . ref && { ref : schema . ref , refAccess : `this.$('${ schema . ref } ')` } )
312+ componentName : normalizedSchema . componentName || 'Unknown' ,
313+ ...( normalizedSchema . ref && { ref : normalizedSchema . ref , refAccess : `this.$('${ normalizedSchema . ref } ')` } )
304314 }
305315
306316 const truncated = {
@@ -309,12 +319,14 @@ function formatCurrentSchema(schema, hintContext) {
309319 dynamicProps : 0
310320 }
311321
312- if ( schema . props ) {
322+ const schemaProps = asRecord ( normalizedSchema . props )
323+
324+ if ( Object . keys ( schemaProps ) . length > 0 ) {
313325 const propKeys = [ ]
314326 const eventKeys = [ ]
315327 const dynamicPropKeys = [ ]
316328
317- for ( const [ key , value ] of Object . entries ( schema . props ) ) {
329+ for ( const [ key , value ] of Object . entries ( schemaProps ) ) {
318330 if ( key . startsWith ( 'on' ) ) {
319331 eventKeys . push ( key )
320332 } else {
@@ -350,7 +362,8 @@ function formatCurrentSchema(schema, hintContext) {
350362 * @param {Object } metadata - 低代码平台元数据
351363 * @returns {Object } 格式化的低代码上下文
352364 */
353- export function buildLowcodeContext ( metadata , options = { } ) {
365+ export function buildLowcodeContext ( metadata = { } , options = { } ) {
366+ const normalizedMetadata = metadata && typeof metadata === 'object' ? metadata : { }
354367 const {
355368 dataSource = [ ] ,
356369 utils = [ ] ,
@@ -359,7 +372,7 @@ export function buildLowcodeContext(metadata, options = {}) {
359372 state = { } ,
360373 methods = { } ,
361374 currentSchema = null
362- } = metadata
375+ } = normalizedMetadata
363376 const hintContext = buildHintContext ( options . hintText )
364377 const formattedDataSource = formatDataSources ( dataSource , hintContext )
365378 const formattedUtils = formatUtils ( utils , hintContext )
0 commit comments