function declarations

This commit is contained in:
Dmitry Vasilev
2023-05-16 00:04:53 +03:00
parent 096ca5e495
commit 5b5938ee61
6 changed files with 202 additions and 41 deletions

View File

@@ -110,6 +110,49 @@ export const tests = [
return assert_code_evals_to('!false', true)
}),
test('function expr', () => {
assert_code_evals_to(
`
const x = function(){}
x.name
`,
'x'
)
assert_code_evals_to(
`
const x = function foo(){}
x.name
`,
'foo'
)
assert_code_evals_to(
`
(function foo(x) {
return x*2
}).name
`,
'foo'
)
assert_code_evals_to(
`
(function foo(x) {
return x*2
})(1)
`,
2
)
}),
test('function declaration', () => {
assert_code_evals_to(
`
function x() {return 1}
x()
`,
1
)
}),
test('More complex expression', () => {
assert_code_evals_to(
`
@@ -1115,6 +1158,26 @@ export const tests = [
return assert_equal(parse(code).problems[0].message, 'undeclared identifier: x')
}),
test('function hoisting', () => {
assert_code_evals_to(`
function x() {
return 1
}
x()
`,
1
)
assert_code_evals_to(`
const y = x()
function x() {
return 1
}
y
`,
1
)
}),
/*
TODO use before assignment
test('no use before assignment', () => {
@@ -2176,7 +2239,6 @@ const y = x()`
assert_equal(s2.value_explorer.result.error.message, 'boom')
}),
test('frame follows cursor toplevel', () => {
const code = `
const x = () => {