import { Plugin, CallExpressionShape, StringShape, ObjectShape } from '@servicenow/sdk-build-core' import { CallExpressionPlugin } from './call-expression-plugin' export const NowRefPlugin = Plugin.create({ name: 'NowRefPlugin', noTelemetry: true, nodes: [ { node: 'CallExpression', async toShape(node, { transform, factory }) { const result = await transform.toShape(node, CallExpressionPlugin) if ( !result.success || !result.value.is(CallExpressionShape) || result.value.getCallee() !== 'Now.ref' ) { return { success: false } } const callExpression = result.value const table = callExpression .getArgument(0) .asString('The first argument to Now.ref() must be a table name') const guidOrKeys = callExpression .getArgument(1) .as( [StringShape, ObjectShape], 'The second argument to Now.ref() must be a GUID, a Now ID string, or a coalesce keys object' ) const maybeKeys = callExpression.hasArgument(2) ? callExpression .getArgument(2) .asObject('The third argument to Now.ref() must be a coalesce keys object') : undefined const [guid, keys] = maybeKeys ? [guidOrKeys.asString(), maybeKeys] // Now.ref(table, guid, keys) : guidOrKeys.isString() ? [guidOrKeys.asString(), undefined] // Now.ref(table, guid) : [undefined, guidOrKeys.asObject()] // Now.ref(table, keys) return { success: true, value: await factory.createReference({ source: node, table, guid, keys }), } }, }, ], })