Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/uni-mp-compiler/src/transforms/transformIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,57 @@ export const transformIdentifier: NodeTransform = (node, context) => {
!isSymbol(firstChild) &&
firstChild.type === NodeTypes.SIMPLE_EXPRESSION &&
context.filters.includes(firstChild.content)

if (isFilter) {
const contentStr = content.children
.map((child) => {
if (typeof child === 'string') {
return child
} else if (isSymbol(child)) {
throw new Error('Symbol not supported')
} else if (
'type' in child &&
child.type === NodeTypes.SIMPLE_EXPRESSION
) {
return child.content
} else {
throw new Error('not supported argument')
}
})
.join('')
const lhs = contentStr.substring(0, contentStr.indexOf('('))
Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that contentStr contains '(' before calling substring to avoid potential index errors if the character is missing.

Suggested change
const lhs = contentStr.substring(0, contentStr.indexOf('('))
const openParenIndex = contentStr.indexOf('(');
if (openParenIndex === -1) {
throw new Error("Expected '(' in contentStr but not found.");
}
const lhs = contentStr.substring(0, openParenIndex);

Copilot uses AI. Check for mistakes.
let isLiteral = false
Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a comment to explain the purpose of this loop and why only the first non-space character after '(' is checked.

Suggested change
let isLiteral = false
let isLiteral = false
// Check the first non-space character after '(' to determine if it is a literal object or array.
// This is used to decide how to rewrite the parameters for the function call.

Copilot uses AI. Check for mistakes.
for (let i = lhs.length + 1; i < contentStr.length; i++) {
if (contentStr[i] === ' ') {
continue
}
if (contentStr[i] === '{' || contentStr[i] === '[') {
isLiteral = true
break
}
break
}

const paramStr = contentStr.substring(
contentStr.indexOf('(') + 1,
contentStr.indexOf(')')
)
const rewriteParams = isLiteral
? [rewriteExpression(createCompoundExpression([paramStr]), context)]
: paramStr
.split(',')
.filter((p) => p.trim().length != 0)
.map((param) => {
return rewriteExpression(
createCompoundExpression([param]),
context
)
})
node.content = rewriteExpression(
createCompoundExpression([lhs, '(', ...rewriteParams, ')']),
context
)
}
}
if (!isFilter) {
node.content = rewriteExpression(
Expand Down