From 7f2fdd5f6373281a7cb2bdd9d57e8a3d2d765377 Mon Sep 17 00:00:00 2001 From: Bowen Tan Date: Tue, 26 Jul 2022 14:32:31 +0800 Subject: [PATCH] fix(validator): it should not warn variables declared in iife in expression --- packages/editor/__tests__/model/fieldModel.spec.ts | 6 ++++++ packages/editor/src/AppModel/FieldModel.ts | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/editor/__tests__/model/fieldModel.spec.ts b/packages/editor/__tests__/model/fieldModel.spec.ts index 161ad68a..decc956c 100644 --- a/packages/editor/__tests__/model/fieldModel.spec.ts +++ b/packages/editor/__tests__/model/fieldModel.spec.ts @@ -71,6 +71,12 @@ describe('Field test', () => { expect(field.refComponentInfos).toEqual({}); }); + it('should not count variables declared in iife in refs', () => { + const field = new FieldModel('{{(function() {const foo = "bar"})() }}'); + expect(field.isDynamic).toEqual(true); + expect(field.refComponentInfos).toEqual({}); + }); + it('get value by path', () => { const field = new FieldModel({ foo: [{}, { bar: { baz: 'Hello, world!' } }] }); expect(field.getPropertyByPath('foo.1.bar.baz')?.getValue()).toEqual('Hello, world!'); diff --git a/packages/editor/src/AppModel/FieldModel.ts b/packages/editor/src/AppModel/FieldModel.ts index 3dcffda8..b7c207eb 100644 --- a/packages/editor/src/AppModel/FieldModel.ts +++ b/packages/editor/src/AppModel/FieldModel.ts @@ -20,6 +20,7 @@ import escodegen from 'escodegen'; import { JSONSchema7 } from 'json-schema'; export type FunctionNode = ASTNode & { params: ASTNode[] }; +export type DeclaratorNode = ASTNode & { id: ASTNode }; export class FieldModel implements IFieldModel { isDynamic = false; refComponentInfos: Record = {}; @@ -161,7 +162,7 @@ export class FieldModel implements IFieldModel { this.astNodes[exp] = node as ASTNode; - // These are varirables of iife, they should be count in refs. + // These are variables of iife, they should be count in refs. let localVariables: ASTNode[] = []; simpleWalk(node, { @@ -200,6 +201,9 @@ export class FieldModel implements IFieldModel { default: } }, + VariableDeclarator: declarator => { + localVariables.push((declarator as DeclaratorNode).id); + }, }); // remove localVariables from refs