Skip to content

Commit ba96b89

Browse files
committed
Merge branch 'bugfix/release_1.1.2'
2 parents 2c5292b + ef3662f commit ba96b89

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
[Read translated version (en)](./translations/en/CHANGELOG.md)
22

3+
# 1.1.2
4+
5+
- Fix: 関数の引数の初期値内に不正なreturn文がある場合に文法エラーにならない問題を修正
6+
37
# 1.1.1
48

59
- Fix: オブジェクトリテラルのプロパティ名に一部の予約語を記述できなかった問題を修正

src/parser/plugins/validate-jump-statements.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ import { AiScriptSyntaxError } from '../../error.js';
33

44
import type * as Ast from '../../node.js';
55

6+
function getClosestAncestorFunction(node: Ast.Return, ancestors: Ast.Node[]): Ast.Fn | undefined {
7+
let child: Ast.Node = node;
8+
for (let i = ancestors.length - 1; i >= 0; i--) {
9+
const ancestor = ancestors[i]!;
10+
// return文が関数のデフォルト引数の中にある場合は、今見つかった関数ではなくさらに上の関数がこのreturn文に対応する。
11+
if (ancestor.type === 'fn' && !ancestor.params.some((param) => param.default != null && param.default === child)) {
12+
return ancestor;
13+
}
14+
child = ancestor;
15+
}
16+
return;
17+
}
18+
619
function getCorrespondingBlock(ancestors: Ast.Node[], label?: string): Ast.Each | Ast.For | Ast.Loop | Ast.If | Ast.Match | Ast.Block | undefined {
720
for (let i = ancestors.length - 1; i >= 0; i--) {
821
const ancestor = ancestors[i]!;
@@ -33,7 +46,8 @@ function getCorrespondingBlock(ancestors: Ast.Node[], label?: string): Ast.Each
3346
function validateNode(node: Ast.Node, ancestors: Ast.Node[]): Ast.Node {
3447
switch (node.type) {
3548
case 'return': {
36-
if (!ancestors.some(({ type }) => type === 'fn')) {
49+
const closestAncestorFunction = getClosestAncestorFunction(node, ancestors);
50+
if (closestAncestorFunction === undefined) {
3751
throw new AiScriptSyntaxError('return must be inside function', node.loc.start);
3852
}
3953
break;

test/jump-statements.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from 'assert';
22
import { describe, test } from 'vitest';
3-
import { utils } from '../src';
3+
import { errors, utils } from '../src';
44
import { NUM, STR, NULL, ARR, OBJ, BOOL, TRUE, FALSE, ERROR ,FN_NATIVE } from '../src/interpreter/value';
55
import { AiScriptRuntimeError, AiScriptSyntaxError } from '../src/error';
66
import { exe, getMeta, eq } from './testutils';
@@ -481,7 +481,8 @@ describe('return', () => {
481481
<: f()
482482
`);
483483
eq(res, NUM(1));
484-
await assert.rejects(() => exe('<: @(x = eval { return 1 }){}'));
484+
await assert.rejects(() => exe('<: @(x = eval { return 1 }){}'), errors.AiScriptSyntaxError);
485+
await assert.rejects(() => exe('<: @(a = @(b = eval { return 0 }){}){}'), errors.AiScriptSyntaxError);
485486
});
486487

487488
test.concurrent('in template', async () => {

0 commit comments

Comments
 (0)