import { AST_NODE_TYPES , TSESTree , } from "@typescript-eslint/experimental-utils" ; import { findTopLevelExpression } from "./utils" ; export function isLoadFunction ( node ) { node = findTopLevelExpression ( node ) ; return ( node . parent ?. type === AST_NODE_TYPES . CallExpression && node . property . type === TSESTree . AST_NODE_TYPES . Identifier && node . property . name === "load" ) ; } export function isLoadReference ( node ) { return ( node . parent && node . parent . type === TSESTree . AST_NODE_TYPES . MemberExpression && isLoadFunction ( node . parent ) ) ; } function parseObjectExpressionProperty ( objectExpression ) { let composedProperties = [ ] ; objectExpression . properties . forEach ( ( property ) => { if ( property . type === AST_NODE_TYPES . Property && property . key . type === AST_NODE_TYPES . Identifier ) { let propertyName = property . key . name ; if ( property . value . type === AST_NODE_TYPES . ObjectExpression ) { const composedProperty = parseObjectExpressionProperty ( property . value ) ; if ( composedProperty . length !== 0 ) { composedProperties = composedProperties . concat ( propertyName + "/" + composedProperty ) ; } } else if ( property . value . type === AST_NODE_TYPES . Literal && property . value . value ) { composedProperties = composedProperties . concat ( propertyName ) ; } } } ) ; return composedProperties ; } function parseLoadStringArgument ( argument ) { let properties = [ ] ; argument . replace ( /\s/g , "" ) . split ( "," ) . forEach ( ( property ) => { properties . push ( property ) ; } ) ; return properties ; } export function parseLoadArguments ( node ) { node = findTopLevelExpression ( node ) ; if ( isLoadFunction ( node ) && node . parent ?. type === TSESTree . AST_NODE_TYPES . CallExpression ) { const argument = node . parent . arguments [ 0 ] ; if ( ! argument ) { return [ ] ; } let properties = [ ] ; if ( argument . type === AST_NODE_TYPES . ArrayExpression ) { argument . elements . forEach ( ( element ) => { if ( element . type === TSESTree . AST_NODE_TYPES . Literal ) { properties = properties . concat ( parseLoadStringArgument ( element . value as string ) ) ; } } ) ; } else if ( argument . type === TSESTree . AST_NODE_TYPES . Literal ) { properties = properties . concat ( parseLoadStringArgument ( argument . value as string ) ) ; } else if ( argument . type === TSESTree . AST_NODE_TYPES . ObjectExpression ) { properties = properties . concat ( parseObjectExpressionProperty ( argument ) ) ; } return properties ; } throw new Error ( "error in getLoadArgument function." ) ; }