Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Commit 2303a46

Browse files
committed
fix: fix function name and length property is not fit with standar
1 parent 256fcd8 commit 2303a46

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/evaluate.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import {
4444
isVariableDeclaration
4545
} from "./packages/babel-types";
4646

47+
import { defineFunctionLength, defineFunctionName } from "./utils";
48+
4749
const visitors: EvaluateMap = {
4850
File(path) {
4951
evaluate(path.createChild(path.node.program));
@@ -393,10 +395,8 @@ const visitors: EvaluateMap = {
393395
func = visitors.FunctionExpression(path.createChild(node as any));
394396
}
395397

396-
Object.defineProperties(func, {
397-
length: { value: node.params.length || 0 },
398-
name: { value: functionName }
399-
});
398+
defineFunctionLength(func, node.params.length || 0);
399+
defineFunctionName(func, functionName);
400400

401401
// Function can repeat declaration
402402
scope.var(functionName, func);
@@ -855,10 +855,8 @@ const visitors: EvaluateMap = {
855855
return result.value;
856856
}
857857
};
858-
Object.defineProperties(method, {
859-
length: { value: node.params.length },
860-
name: { value: methodName }
861-
});
858+
defineFunctionLength(method, node.params.length);
859+
defineFunctionName(method, methodName);
862860
switch (node.kind) {
863861
case "get":
864862
Object.defineProperty(path.ctx.object, methodName, { get: method });
@@ -904,10 +902,8 @@ const visitors: EvaluateMap = {
904902
}
905903
};
906904

907-
Object.defineProperties(func, {
908-
length: { value: node.params.length },
909-
name: { value: node.id ? node.id.name : "" } // Anonymous function
910-
});
905+
defineFunctionLength(func, node.params.length);
906+
defineFunctionName(func, node.id ? node.id.name : ""); // Anonymous function
911907

912908
return func;
913909
},
@@ -1170,10 +1166,8 @@ const visitors: EvaluateMap = {
11701166
}
11711167
};
11721168

1173-
Object.defineProperties(func, {
1174-
length: { value: node.params.length },
1175-
name: { value: node.id ? node.id.name : "" }
1176-
});
1169+
defineFunctionLength(func, node.params.length);
1170+
defineFunctionName(func, node.id ? node.id.name : "");
11771171

11781172
return func;
11791173
},
@@ -1266,11 +1260,12 @@ const visitors: EvaluateMap = {
12661260
return this;
12671261
}
12681262

1269-
// define class name
1270-
Object.defineProperties(ClassConstructor, {
1271-
name: { value: parentNode.id.name },
1272-
length: { value: constructor ? constructor.params.length : 0 }
1273-
});
1263+
// define class name and length
1264+
defineFunctionLength(
1265+
ClassConstructor,
1266+
constructor ? constructor.params.length : 0
1267+
);
1268+
defineFunctionName(ClassConstructor, parentNode.id.name);
12741269

12751270
const classMethods = methods
12761271
.map((method: types.ClassMethod) => {
@@ -1299,10 +1294,8 @@ const visitors: EvaluateMap = {
12991294
}
13001295
};
13011296

1302-
Object.defineProperties(func, {
1303-
length: { value: method.params.length },
1304-
name: { value: method.id ? method.id.name : "" }
1305-
});
1297+
defineFunctionLength(func, method.params.length);
1298+
defineFunctionName(func, method.id ? method.id.name : "");
13061299

13071300
return {
13081301
key: (method.key as any).name,

src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function defineFunctionName(func, name: string) {
2+
Object.defineProperty(func, "name", {
3+
value: name || "",
4+
writable: false,
5+
enumerable: false,
6+
configurable: true
7+
});
8+
}
9+
10+
export function defineFunctionLength(func, length: number) {
11+
Object.defineProperty(func, "length", {
12+
value: length || 0,
13+
writable: false,
14+
enumerable: false,
15+
configurable: true
16+
});
17+
}

0 commit comments

Comments
 (0)