MCPcopy Index your code
hub / github.com/nodejs/node / createClassElement

Function createClassElement

test/fixtures/snapshot/typescript.js:151203–151301  ·  view source on GitHub ↗
(symbol, modifiers, members)

Source from the content-addressed store, hash-verified

151201 }
151202 }
151203 function createClassElement(symbol, modifiers, members) {
151204 // Right now the only thing we can convert are function expressions, which are marked as methods
151205 // or { x: y } type prototype assignments, which are marked as ObjectLiteral
151206 if (!(symbol.flags & 8192 /* SymbolFlags.Method */) && !(symbol.flags & 4096 /* SymbolFlags.ObjectLiteral */)) {
151207 return;
151208 }
151209 var memberDeclaration = symbol.valueDeclaration;
151210 var assignmentBinaryExpression = memberDeclaration.parent;
151211 var assignmentExpr = assignmentBinaryExpression.right;
151212 if (!shouldConvertDeclaration(memberDeclaration, assignmentExpr)) {
151213 return;
151214 }
151215 if (ts.some(members, function (m) {
151216 var name = ts.getNameOfDeclaration(m);
151217 if (name && ts.isIdentifier(name) && ts.idText(name) === ts.symbolName(symbol)) {
151218 return true; // class member already made for this name
151219 }
151220 return false;
151221 })) {
151222 return;
151223 }
151224 // delete the entire statement if this expression is the sole expression to take care of the semicolon at the end
151225 var nodeToDelete = assignmentBinaryExpression.parent && assignmentBinaryExpression.parent.kind === 238 /* SyntaxKind.ExpressionStatement */
151226 ? assignmentBinaryExpression.parent : assignmentBinaryExpression;
151227 changes.delete(sourceFile, nodeToDelete);
151228 if (!assignmentExpr) {
151229 members.push(ts.factory.createPropertyDeclaration([], modifiers, symbol.name, /*questionToken*/ undefined,
151230 /*type*/ undefined, /*initializer*/ undefined));
151231 return;
151232 }
151233 // f.x = expr
151234 if (ts.isAccessExpression(memberDeclaration) && (ts.isFunctionExpression(assignmentExpr) || ts.isArrowFunction(assignmentExpr))) {
151235 var quotePreference = ts.getQuotePreference(sourceFile, preferences);
151236 var name = tryGetPropertyName(memberDeclaration, compilerOptions, quotePreference);
151237 if (name) {
151238 createFunctionLikeExpressionMember(members, assignmentExpr, name);
151239 }
151240 return;
151241 }
151242 // f.prototype = { ... }
151243 else if (ts.isObjectLiteralExpression(assignmentExpr)) {
151244 ts.forEach(assignmentExpr.properties, function (property) {
151245 if (ts.isMethodDeclaration(property) || ts.isGetOrSetAccessorDeclaration(property)) {
151246 // MethodDeclaration and AccessorDeclaration can appear in a class directly
151247 members.push(property);
151248 }
151249 if (ts.isPropertyAssignment(property) && ts.isFunctionExpression(property.initializer)) {
151250 createFunctionLikeExpressionMember(members, property.initializer, property.name);
151251 }
151252 // Drop constructor assignments
151253 if (isConstructorAssignment(property))
151254 return;
151255 return;
151256 });
151257 return;
151258 }
151259 else {
151260 // Don't try to declare members in JavaScript files

Callers 1

Calls 8

shouldConvertDeclarationFunction · 0.85
tryGetPropertyNameFunction · 0.85
isConstructorAssignmentFunction · 0.85
someMethod · 0.80
deleteMethod · 0.65
forEachMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected