fix(validator): it should not warn variables declared in iife in expression

This commit is contained in:
Bowen Tan 2022-07-26 14:32:31 +08:00
parent 41c8267384
commit 7f2fdd5f63
2 changed files with 11 additions and 1 deletions

View File

@ -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!');

View File

@ -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<ComponentId | ModuleId, RefInfo> = {};
@ -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