Skip to content

Commit ef3662f

Browse files
authored
fix: 関数の引数の初期値内に不正なreturn文がある場合に文法エラーにならない問題を修正 (#977)
* 関数の引数の初期値内に不正なreturn文がある場合に文法エラーにならない問題を修正 * 1.1.2 * npm version patch && npm run pre-release * npm run api
1 parent 0a32f15 commit ef3662f

6 files changed

Lines changed: 26 additions & 7 deletions

File tree

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

etc/aiscript.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type AddAssign = NodeBase & {
2121
};
2222

2323
// @public (undocumented)
24-
export const AISCRIPT_VERSION: "1.1.0";
24+
export const AISCRIPT_VERSION: "1.1.2";
2525

2626
// @public (undocumented)
2727
abstract class AiScriptError extends Error {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "module",
33
"name": "@syuilo/aiscript",
4-
"version": "1.1.1",
4+
"version": "1.1.2",
55
"description": "AiScript implementation",
66
"author": "syuilo <syuilotan@yahoo.co.jp>",
77
"license": "MIT",

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)