MCPcopy
hub / github.com/emberjs/ember.js / rewriteModule

Function rewriteModule

types/publish.mjs:269–390  ·  view source on GitHub ↗
(code, moduleName)

Source from the content-addressed store, hash-verified

267 @returns {string}
268 */
269export function rewriteModule(code, moduleName) {
270 let ast = parse(code, { parser });
271
272 /** @type {Array<import("ast-types/gen/namedTypes").namedTypes.TSModuleDeclaration>} */
273 let otherModuleDeclarations = [];
274
275 visit(ast, {
276 // We need to preserve existing `declare module { ... }` blocks so that
277 // things which rely on declaration merging can work, but they need to be
278 // emitted *outside* the `declare module` we are introducing.
279 visitTSModuleDeclaration(path) {
280 // ...but we need to *avoid* doing this for namespace declarations! So we
281 // *only* do it for cases where we are sure, since `declare module` will
282 // always have a string literal, while `declare namespace` will have an
283 // actual identifier instead.
284 if (path.node.id.type == 'StringLiteral') {
285 otherModuleDeclarations.push(path.node);
286 path.prune(path.node);
287 } else {
288 // Where we have a `declare namespace` type, we need to emit it without
289 // the `declare`, as with other items.
290 path.node.declare = false;
291 }
292 this.traverse(path);
293 },
294
295 // Remove `declare` from `declare (let|const|var)` in the top-level module.
296 visitVariableDeclaration(path) {
297 if (isVariableDeclaration(path.node) && !hasParentModuleDeclarationBlock(path)) {
298 path.node.declare = false;
299 }
300 this.traverse(path);
301 },
302
303 // Remove `declare` from `declare class` in the top-level module.
304 visitClassDeclaration(path) {
305 if (isClassDeclaration(path.node) && !hasParentModuleDeclarationBlock(path)) {
306 path.node.declare = false;
307 }
308 this.traverse(path);
309 },
310
311 // Remove `declare` from `declare function` in the top-level module.
312 visitTSDeclareFunction(path) {
313 if (!hasParentModuleDeclarationBlock(path)) {
314 path.node.declare = false;
315 }
316 this.traverse(path);
317 },
318
319 visitTSInterfaceDeclaration(path) {
320 if (!hasParentModuleDeclarationBlock(path)) {
321 path.node.declare = false;
322 }
323 this.traverse(path);
324 },
325
326 // Remove `declare` from `declare enum` in the top-level module.

Callers 1

processModuleFunction · 0.85

Calls 2

replaceMethod · 0.65
visitFunction · 0.50

Tested by

no test coverage detected