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

Commit

Permalink
fix: fix function name and length property is not fit with standar
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 29, 2018
1 parent 256fcd8 commit 2303a46
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import {
isVariableDeclaration
} from "./packages/babel-types";

import { defineFunctionLength, defineFunctionName } from "./utils";

const visitors: EvaluateMap = {
File(path) {
evaluate(path.createChild(path.node.program));
Expand Down Expand Up @@ -393,10 +395,8 @@ const visitors: EvaluateMap = {
func = visitors.FunctionExpression(path.createChild(node as any));
}

Object.defineProperties(func, {
length: { value: node.params.length || 0 },
name: { value: functionName }
});
defineFunctionLength(func, node.params.length || 0);
defineFunctionName(func, functionName);

// Function can repeat declaration
scope.var(functionName, func);
Expand Down Expand Up @@ -855,10 +855,8 @@ const visitors: EvaluateMap = {
return result.value;
}
};
Object.defineProperties(method, {
length: { value: node.params.length },
name: { value: methodName }
});
defineFunctionLength(method, node.params.length);
defineFunctionName(method, methodName);
switch (node.kind) {
case "get":
Object.defineProperty(path.ctx.object, methodName, { get: method });
Expand Down Expand Up @@ -904,10 +902,8 @@ const visitors: EvaluateMap = {
}
};

Object.defineProperties(func, {
length: { value: node.params.length },
name: { value: node.id ? node.id.name : "" } // Anonymous function
});
defineFunctionLength(func, node.params.length);
defineFunctionName(func, node.id ? node.id.name : ""); // Anonymous function

return func;
},
Expand Down Expand Up @@ -1170,10 +1166,8 @@ const visitors: EvaluateMap = {
}
};

Object.defineProperties(func, {
length: { value: node.params.length },
name: { value: node.id ? node.id.name : "" }
});
defineFunctionLength(func, node.params.length);
defineFunctionName(func, node.id ? node.id.name : "");

return func;
},
Expand Down Expand Up @@ -1266,11 +1260,12 @@ const visitors: EvaluateMap = {
return this;
}

// define class name
Object.defineProperties(ClassConstructor, {
name: { value: parentNode.id.name },
length: { value: constructor ? constructor.params.length : 0 }
});
// define class name and length
defineFunctionLength(
ClassConstructor,
constructor ? constructor.params.length : 0
);
defineFunctionName(ClassConstructor, parentNode.id.name);

const classMethods = methods
.map((method: types.ClassMethod) => {
Expand Down Expand Up @@ -1299,10 +1294,8 @@ const visitors: EvaluateMap = {
}
};

Object.defineProperties(func, {
length: { value: method.params.length },
name: { value: method.id ? method.id.name : "" }
});
defineFunctionLength(func, method.params.length);
defineFunctionName(func, method.id ? method.id.name : "");

return {
key: (method.key as any).name,
Expand Down
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function defineFunctionName(func, name: string) {
Object.defineProperty(func, "name", {
value: name || "",
writable: false,
enumerable: false,
configurable: true
});
}

export function defineFunctionLength(func, length: number) {
Object.defineProperty(func, "length", {
value: length || 0,
writable: false,
enumerable: false,
configurable: true
});
}

0 comments on commit 2303a46

Please sign in to comment.