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

Function transformCommonJSModule

test/fixtures/snapshot/typescript.js:104410–104431  ·  view source on GitHub ↗

* Transforms a SourceFile into a CommonJS module. * * @param node The SourceFile node.

(node)

Source from the content-addressed store, hash-verified

104408 * @param node The SourceFile node.
104409 */
104410 function transformCommonJSModule(node) {
104411 startLexicalEnvironment();
104412 var statements = [];
104413 var ensureUseStrict = ts.getStrictOptionValue(compilerOptions, "alwaysStrict") || (!compilerOptions.noImplicitUseStrict && ts.isExternalModule(currentSourceFile));
104414 var statementOffset = factory.copyPrologue(node.statements, statements, ensureUseStrict && !ts.isJsonSourceFile(node), topLevelVisitor);
104415 if (shouldEmitUnderscoreUnderscoreESModule()) {
104416 ts.append(statements, createUnderscoreUnderscoreESModule());
104417 }
104418 if (ts.length(currentModuleInfo.exportedNames)) {
104419 var chunkSize = 50;
104420 for (var i = 0; i < currentModuleInfo.exportedNames.length; i += chunkSize) {
104421 ts.append(statements, factory.createExpressionStatement(ts.reduceLeft(currentModuleInfo.exportedNames.slice(i, i + chunkSize), function (prev, nextId) { return factory.createAssignment(factory.createPropertyAccessExpression(factory.createIdentifier("exports"), factory.createIdentifier(ts.idText(nextId))), prev); }, factory.createVoidZero())));
104422 }
104423 }
104424 ts.append(statements, ts.visitNode(currentModuleInfo.externalHelpersImportDeclaration, topLevelVisitor, ts.isStatement));
104425 ts.addRange(statements, ts.visitNodes(node.statements, topLevelVisitor, ts.isStatement, statementOffset));
104426 addExportEqualsIfNeeded(statements, /*emitAsReturn*/ false);
104427 ts.insertStatementsAfterStandardPrologue(statements, endLexicalEnvironment());
104428 var updated = factory.updateSourceFile(node, ts.setTextRange(factory.createNodeArray(statements), node.statements));
104429 ts.addEmitHelpers(updated, context.readEmitHelpers());
104430 return updated;
104431 }
104432 /**
104433 * Transforms a SourceFile into an AMD module.
104434 *

Callers

nothing calls this directly

Calls 9

startLexicalEnvironmentFunction · 0.85
addExportEqualsIfNeededFunction · 0.85
endLexicalEnvironmentFunction · 0.85
addRangeMethod · 0.80
sliceMethod · 0.65
appendMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected