Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[Read translated version (en)](./translations/en/CHANGELOG.md)

# 1.1.2

- Fix: 関数の引数の初期値内に不正なreturn文がある場合に文法エラーにならない問題を修正

# 1.1.1

- Fix: オブジェクトリテラルのプロパティ名に一部の予約語を記述できなかった問題を修正
Expand Down
2 changes: 1 addition & 1 deletion etc/aiscript.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type AddAssign = NodeBase & {
};

// @public (undocumented)
export const AISCRIPT_VERSION: "1.1.0";
export const AISCRIPT_VERSION: "1.1.2";

// @public (undocumented)
abstract class AiScriptError extends Error {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "@syuilo/aiscript",
"version": "1.1.1",
"version": "1.1.2",
"description": "AiScript implementation",
"author": "syuilo <syuilotan@yahoo.co.jp>",
"license": "MIT",
Expand Down
16 changes: 15 additions & 1 deletion src/parser/plugins/validate-jump-statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]!;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions test/jump-statements.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 () => {
Expand Down
Loading