From db7f4874a6cbf4a82502a206df24e2c99652c710 Mon Sep 17 00:00:00 2001 From: takejohn Date: Fri, 19 Sep 2025 23:04:04 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E9=96=A2=E6=95=B0=E3=81=AE=E5=BC=95?= =?UTF-8?q?=E6=95=B0=E3=81=AE=E5=88=9D=E6=9C=9F=E5=80=A4=E5=86=85=E3=81=AB?= =?UTF-8?q?=E4=B8=8D=E6=AD=A3=E3=81=AAreturn=E6=96=87=E3=81=8C=E3=81=82?= =?UTF-8?q?=E3=82=8B=E5=A0=B4=E5=90=88=E3=81=AB=E6=96=87=E6=B3=95=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=81=AB=E3=81=AA=E3=82=89=E3=81=AA=E3=81=84?= =?UTF-8?q?=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etc/aiscript.api.md | 2 +- src/parser/plugins/validate-jump-statements.ts | 16 +++++++++++++++- test/jump-statements.ts | 5 +++-- unreleased/invalid-return-in-default-argument.md | 1 + 4 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 unreleased/invalid-return-in-default-argument.md diff --git a/etc/aiscript.api.md b/etc/aiscript.api.md index 18d82220..f708a297 100644 --- a/etc/aiscript.api.md +++ b/etc/aiscript.api.md @@ -21,7 +21,7 @@ type AddAssign = NodeBase & { }; // @public (undocumented) -export const AISCRIPT_VERSION: "1.1.0"; +export const AISCRIPT_VERSION: "1.1.1"; // @public (undocumented) abstract class AiScriptError extends Error { diff --git a/src/parser/plugins/validate-jump-statements.ts b/src/parser/plugins/validate-jump-statements.ts index 9828bca9..64fbe267 100644 --- a/src/parser/plugins/validate-jump-statements.ts +++ b/src/parser/plugins/validate-jump-statements.ts @@ -3,6 +3,19 @@ import { AiScriptSyntaxError } from '../../error.js'; import type * as Ast from '../../node.js'; +function getClosestAncestorFunction(node: Ast.Return, ancestors: Ast.Node[]): Ast.Fn | undefined { + let child: Ast.Node = node; + for (let i = ancestors.length - 1; i >= 0; i--) { + const ancestor = ancestors[i]!; + // return文が関数のデフォルト引数の中にある場合は、今見つかった関数ではなくさらに上の関数がこのreturn文に対応する。 + if (ancestor.type === 'fn' && !ancestor.params.some((param) => param.default != null && param.default === child)) { + return ancestor; + } + child = ancestor; + } + return; +} + function getCorrespondingBlock(ancestors: Ast.Node[], label?: string): Ast.Each | Ast.For | Ast.Loop | Ast.If | Ast.Match | Ast.Block | undefined { for (let i = ancestors.length - 1; i >= 0; i--) { const ancestor = ancestors[i]!; @@ -33,7 +46,8 @@ function getCorrespondingBlock(ancestors: Ast.Node[], label?: string): Ast.Each function validateNode(node: Ast.Node, ancestors: Ast.Node[]): Ast.Node { switch (node.type) { case 'return': { - if (!ancestors.some(({ type }) => type === 'fn')) { + const closestAncestorFunction = getClosestAncestorFunction(node, ancestors); + if (closestAncestorFunction === undefined) { throw new AiScriptSyntaxError('return must be inside function', node.loc.start); } break; diff --git a/test/jump-statements.ts b/test/jump-statements.ts index 905898dd..633474b6 100644 --- a/test/jump-statements.ts +++ b/test/jump-statements.ts @@ -1,6 +1,6 @@ import * as assert from 'assert'; import { describe, test } from 'vitest'; -import { utils } from '../src'; +import { errors, utils } from '../src'; import { NUM, STR, NULL, ARR, OBJ, BOOL, TRUE, FALSE, ERROR ,FN_NATIVE } from '../src/interpreter/value'; import { AiScriptRuntimeError, AiScriptSyntaxError } from '../src/error'; import { exe, getMeta, eq } from './testutils'; @@ -481,7 +481,8 @@ describe('return', () => { <: f() `); eq(res, NUM(1)); - await assert.rejects(() => exe('<: @(x = eval { return 1 }){}')); + await assert.rejects(() => exe('<: @(x = eval { return 1 }){}'), errors.AiScriptSyntaxError); + await assert.rejects(() => exe('<: @(a = @(b = eval { return 0 }){}){}'), errors.AiScriptSyntaxError); }); test.concurrent('in template', async () => { diff --git a/unreleased/invalid-return-in-default-argument.md b/unreleased/invalid-return-in-default-argument.md new file mode 100644 index 00000000..46654321 --- /dev/null +++ b/unreleased/invalid-return-in-default-argument.md @@ -0,0 +1 @@ +- Fix: 関数の引数の初期値内に不正なreturn文がある場合に文法エラーにならない問題を修正 From b0318742a9ad1e43256288bfaf1090a6dbfdb1f4 Mon Sep 17 00:00:00 2001 From: takejohn Date: Fri, 19 Sep 2025 23:16:21 +0900 Subject: [PATCH 2/4] 1.1.2 --- package-lock.json | 4 ++-- package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index c0c9ef54..b837f9ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@syuilo/aiscript", - "version": "1.1.1", + "version": "1.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@syuilo/aiscript", - "version": "1.1.1", + "version": "1.1.2", "license": "MIT", "dependencies": { "seedrandom": "3.0.5", diff --git a/package.json b/package.json index b7c0cb0d..74219b7c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "@syuilo/aiscript", - "version": "1.1.1", + "version": "1.1.2", "description": "AiScript implementation", "author": "syuilo ", "license": "MIT", @@ -53,4 +53,4 @@ "stringz": "2.1.0", "uuid": "11.1.0" } -} \ No newline at end of file +} From 947edcb5685fb95d46fbd08cbd47249b64f4cd3f Mon Sep 17 00:00:00 2001 From: takejohn Date: Fri, 19 Sep 2025 23:17:25 +0900 Subject: [PATCH 3/4] npm version patch && npm run pre-release --- CHANGELOG.md | 4 ++++ package.json | 2 +- unreleased/invalid-return-in-default-argument.md | 1 - 3 files changed, 5 insertions(+), 2 deletions(-) delete mode 100644 unreleased/invalid-return-in-default-argument.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 2758d8c0..c0abc1e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ [Read translated version (en)](./translations/en/CHANGELOG.md) +# 1.1.2 + +- Fix: 関数の引数の初期値内に不正なreturn文がある場合に文法エラーにならない問題を修正 + # 1.1.1 - Fix: オブジェクトリテラルのプロパティ名に一部の予約語を記述できなかった問題を修正 diff --git a/package.json b/package.json index 74219b7c..f81b5a34 100644 --- a/package.json +++ b/package.json @@ -53,4 +53,4 @@ "stringz": "2.1.0", "uuid": "11.1.0" } -} +} \ No newline at end of file diff --git a/unreleased/invalid-return-in-default-argument.md b/unreleased/invalid-return-in-default-argument.md deleted file mode 100644 index 46654321..00000000 --- a/unreleased/invalid-return-in-default-argument.md +++ /dev/null @@ -1 +0,0 @@ -- Fix: 関数の引数の初期値内に不正なreturn文がある場合に文法エラーにならない問題を修正 From d93ce88e9f1d6b4328d08b4815b1cb7dc630fb20 Mon Sep 17 00:00:00 2001 From: takejohn Date: Fri, 19 Sep 2025 23:18:40 +0900 Subject: [PATCH 4/4] npm run api --- etc/aiscript.api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/etc/aiscript.api.md b/etc/aiscript.api.md index f708a297..3b9cb869 100644 --- a/etc/aiscript.api.md +++ b/etc/aiscript.api.md @@ -21,7 +21,7 @@ type AddAssign = NodeBase & { }; // @public (undocumented) -export const AISCRIPT_VERSION: "1.1.1"; +export const AISCRIPT_VERSION: "1.1.2"; // @public (undocumented) abstract class AiScriptError extends Error {