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

Function visitModuleDeclaration

test/fixtures/snapshot/typescript.js:93668–93728  ·  view source on GitHub ↗

* Visits a module declaration node. * * This function will be called any time a TypeScript namespace (ModuleDeclaration) is encountered. * * @param node The module declaration node.

(node)

Source from the content-addressed store, hash-verified

93666 * @param node The module declaration node.
93667 */
93668 function visitModuleDeclaration(node) {
93669 if (!shouldEmitModuleDeclaration(node)) {
93670 return factory.createNotEmittedStatement(node);
93671 }
93672 ts.Debug.assertNode(node.name, ts.isIdentifier, "A TypeScript namespace should have an Identifier name.");
93673 enableSubstitutionForNamespaceExports();
93674 var statements = [];
93675 // We request to be advised when the printer is about to print this node. This allows
93676 // us to set up the correct state for later substitutions.
93677 var emitFlags = 2 /* EmitFlags.AdviseOnEmitNode */;
93678 // If needed, we should emit a variable declaration for the module. If we emit
93679 // a leading variable declaration, we should not emit leading comments for the
93680 // module body.
93681 var varAdded = addVarForEnumOrModuleDeclaration(statements, node);
93682 if (varAdded) {
93683 // We should still emit the comments if we are emitting a system module.
93684 if (moduleKind !== ts.ModuleKind.System || currentLexicalScope !== currentSourceFile) {
93685 emitFlags |= 512 /* EmitFlags.NoLeadingComments */;
93686 }
93687 }
93688 // `parameterName` is the declaration name used inside of the namespace.
93689 var parameterName = getNamespaceParameterName(node);
93690 // `containerName` is the expression used inside of the namespace for exports.
93691 var containerName = getNamespaceContainerName(node);
93692 // `exportName` is the expression used within this node's container for any exported references.
93693 var exportName = ts.hasSyntacticModifier(node, 1 /* ModifierFlags.Export */)
93694 ? factory.getExternalModuleOrNamespaceExportName(currentNamespaceContainerName, node, /*allowComments*/ false, /*allowSourceMaps*/ true)
93695 : factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
93696 // x || (x = {})
93697 // exports.x || (exports.x = {})
93698 var moduleArg = factory.createLogicalOr(exportName, factory.createAssignment(exportName, factory.createObjectLiteralExpression()));
93699 if (hasNamespaceQualifiedExportName(node)) {
93700 // `localName` is the expression used within this node's containing scope for any local references.
93701 var localName = factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
93702 // x = (exports.x || (exports.x = {}))
93703 moduleArg = factory.createAssignment(localName, moduleArg);
93704 }
93705 // (function (x_1) {
93706 // x_1.y = ...;
93707 // })(x || (x = {}));
93708 var moduleStatement = factory.createExpressionStatement(factory.createCallExpression(factory.createFunctionExpression(
93709 /*modifiers*/ undefined,
93710 /*asteriskToken*/ undefined,
93711 /*name*/ undefined,
93712 /*typeParameters*/ undefined, [factory.createParameterDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, parameterName)],
93713 /*type*/ undefined, transformModuleBody(node, containerName)),
93714 /*typeArguments*/ undefined, [moduleArg]));
93715 ts.setOriginalNode(moduleStatement, node);
93716 if (varAdded) {
93717 // If a variable was added, synthetic comments are emitted on it, not on the moduleStatement.
93718 ts.setSyntheticLeadingComments(moduleStatement, undefined);
93719 ts.setSyntheticTrailingComments(moduleStatement, undefined);
93720 }
93721 ts.setTextRange(moduleStatement, node);
93722 ts.addEmitFlags(moduleStatement, emitFlags);
93723 statements.push(moduleStatement);
93724 // Add a DeclarationMarker for the namespace to preserve trailing comments and mark
93725 // the end of the declaration.

Callers 2

visitTypeScriptFunction · 0.85
transformModuleBodyFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…