Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing error message about callables #7365

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion app/javascript/interpreter/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class Executor

if (statement.expression instanceof VariableExpression)
this.error('MissingParenthesesForFunctionCall', statement.location, {
expression: statement.expression,
name: statement.expression.name.lexeme,
})

return result
Expand All @@ -218,6 +218,15 @@ export class Executor
})
}
const value = this.evaluate(statement.initializer).value
if (isCallable(value)) {
this.error(
'MissingParenthesesForFunctionCall',
statement.initializer.location,
{
name: (statement.initializer as VariableExpression).name.lexeme,
}
)
}
this.environment.define(statement.name.lexeme, value)

return {
Expand Down
8 changes: 4 additions & 4 deletions app/javascript/interpreter/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
},
"runtime": {
"CouldNotEvaluateFunction": "Could not evaluate function",
"CouldNotFindFunctionWithName": "Could not find function with name.",
"CouldNotFindFunctionWithNameSuggestion": "We don't know what `{{name}}` means. Maybe you meant to use the `{{suggestion}}` function instead?",
"CouldNotFindValueWithName": "Could not find value with name '{{name}}'.",
"CouldNotFindFunctionWithName": "Jiki couldn't find a function with the name `{{name}}`.",
"CouldNotFindFunctionWithNameSuggestion": "Jiki couldn't find a function with the name `{{name}}`. Maybe you meant to use the `{{suggestion}}` function instead?",
"CouldNotFindValueWithName": "Jiki couldn't find a variable or function with name `{{name}}`.",
"InfiniteLoop": "Your code ran for too long and we stopped it to prevent an infinite loop.",
"InvalidBinaryExpression": "Invalid binary expression",
"InvalidExpression": "Invalid expression",
Expand All @@ -79,7 +79,7 @@
"InvalidNumberOfArgumentsWithOptionalArguments": "Expected between {{minArity} and {{maxArity}} arguments but got {{numberOfArgs}}.",
"InvalidUnaryOperator": "Invalid unary operator",
"LogicError": "{{message}}",
"MissingParenthesesForFunctionCall": "Did you forget the parenthesis when trying to call the function?",
"MissingParenthesesForFunctionCall": "Did you forget the parenthesis when trying to use this function?\n\nDid you mean:\n\n```{{name}}()```",
"NonCallableTarget": "Can only call functions.",
"OperandMustBeBoolean": "Operand must be a boolean.",
"OperandMustBeNumber": "Operand must be a number.",
Expand Down
45 changes: 33 additions & 12 deletions test/javascript/interpreter/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,16 +833,6 @@ describe('frames', () => {
expect(frames[0].error).toBeNil()
expect(frames[0].variables).toBeEmpty()
})

test('variable', () => {
const { frames } = interpret('set x to 1')
expect(frames).toBeArrayOfSize(1)
expect(frames[0].line).toBe(1)
expect(frames[0].status).toBe('SUCCESS')
expect(frames[0].code).toBe('set x to 1')
expect(frames[0].error).toBeNil()
expect(frames[0].variables).toMatchObject({ x: 1 })
})
})

describe('multiple statements', () => {
Expand Down Expand Up @@ -1191,6 +1181,17 @@ describe('errors', () => {
expect(error).toBeNull()
})

test('forgetting brackets', () => {
const echoFunction = (_interpreter: any, _n: any) => {}
const context = {
externalFunctions: [
{ name: 'echo', func: echoFunction, description: '' },
],
}
const { frames } = interpret('set x to echo', context)
expect(frames[0].status).toBe('ERROR')
})

describe('arity', () => {
describe('no optional parameters', () => {
test('too many arguments', () => {
Expand Down Expand Up @@ -1271,7 +1272,7 @@ describe('errors', () => {
expect(frames[0].code).toBe('foobor()')
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.message).toBe(
"We don't know what `foobor` means. Maybe you meant to use the `foobar` function instead?"
"Jiki couldn't find a function with the name `foobor`. Maybe you meant to use the `foobar` function instead?"
)
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe(
Expand All @@ -1295,7 +1296,27 @@ describe('errors', () => {
expect(frames[0].code).toBe('foo')
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.message).toBe(
'Did you forget the parenthesis when trying to call the function?'
'Did you forget the parenthesis when trying to use this function?\n\nDid you mean:\n\n```foo()```'
)
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('MissingParenthesesForFunctionCall')
expect(error).toBeNull()
})

test('missing parentheses within set', () => {
const context = {
externalFunctions: [
{ name: 'y', func: (_: any) => {}, description: '' },
],
}
const { error, frames } = interpret(`set x to y`, context)
expect(frames).toBeArrayOfSize(1)
expect(frames[0].line).toBe(1)
expect(frames[0].status).toBe('ERROR')
expect(frames[0].code).toBe('set x to y')
expect(frames[0].error).not.toBeNull()
expect(frames[0].error!.message).toBe(
'Did you forget the parenthesis when trying to use this function?\n\nDid you mean:\n\n```y()```'
)
expect(frames[0].error!.category).toBe('RuntimeError')
expect(frames[0].error!.type).toBe('MissingParenthesesForFunctionCall')
Expand Down
Loading