MCPcopy Create free account
hub / github.com/andywer/postguard / parseQuery

Function parseQuery

src/babel/parse-query.ts:79–135  ·  view source on GitHub ↗
(path: NodePath<types.TemplateLiteral>, sourceFile: SourceFile)

Source from the content-addressed store, hash-verified

77}
78
79export function parseQuery(path: NodePath<types.TemplateLiteral>, sourceFile: SourceFile): Query {
80 const expressions = path.get("expressions")
81 const textPartials = path.get("quasis").map(quasi => quasi.node)
82
83 const expressionSpreadTypes: ExpressionSpreadTypes = {}
84 const sourceMap: QuerySourceMapSpan[] = []
85 let templatedQueryString: string = ""
86
87 const addToQueryString = (
88 node: types.Node,
89 queryStringPartial: string,
90 isTemplateExpression?: boolean
91 ) => {
92 if (node.loc) {
93 sourceMap.push({
94 isTemplateExpression,
95 sourceLocation: node.loc,
96 queryStartIndex: templatedQueryString.length,
97 queryEndIndex: templatedQueryString.length + queryStringPartial.length
98 })
99 }
100
101 return templatedQueryString + queryStringPartial
102 }
103
104 templatedQueryString = addToQueryString(textPartials[0], textPartials[0].value.cooked)
105
106 for (let index = 0; index < expressions.length; index++) {
107 const expression = expressions[index]
108 const paramNumber = index + 1
109
110 const placeholder = createExpressionPlaceholder(expression, paramNumber)
111 templatedQueryString = addToQueryString(expression.node, placeholder, true)
112
113 expressionSpreadTypes[paramNumber] = resolveSpreadExpressionType(expression, sourceFile)
114
115 if (expressionSpreadTypes[paramNumber] === spreadTypeAny && sourceFile.ts) {
116 const lineHint = path.node.loc ? `:${path.node.loc.start.line}` : ``
117 console.warn(
118 format.warning(
119 `Warning: Cannot infer properties of spread expression in SQL template at ${
120 sourceFile.filePath
121 }${lineHint}`
122 )
123 )
124 }
125
126 if (textPartials[index + 1]) {
127 templatedQueryString = addToQueryString(
128 textPartials[index + 1],
129 textPartials[index + 1].value.cooked
130 )
131 }
132 }
133
134 return parsePostgresQuery(templatedQueryString, sourceFile, sourceMap, expressionSpreadTypes)
135}

Callers 1

TaggedTemplateExpressionFunction · 0.90

Calls 4

parsePostgresQueryFunction · 0.90
addToQueryStringFunction · 0.85

Tested by

no test coverage detected