Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Draft: TS-Migrate-Modules Codemod #562

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions codemods/typescript/migrate-modules/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"loader": ["ts-node/esm"],
"full-trace": true,
"failZero": false,
"bail": true,
"spec": "./**/test.ts",
"timeout": 5000
}
Empty file.
5 changes: 5 additions & 0 deletions codemods/typescript/migrate-modules/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"schemaVersion": "1.0.0",
"name": "typescript/migrate-modules",
"engine": "jscodeshift"
}
2 changes: 2 additions & 0 deletions codemods/typescript/migrate-modules/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import type { API, FileInfo } from 'jscodeshift';
export default function transform(file: FileInfo, api: API): string;
31 changes: 31 additions & 0 deletions codemods/typescript/migrate-modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@codemod-registry/typescript-migrate-modules",
"dependencies": {},
"devDependencies": {
"@codemod-registry/tsconfig": "workspace:*",
"@codemod-registry/utilities": "workspace:*",
"@codemod-registry/cjs-builder": "workspace:*",
"typescript": "^5.2.2",
"esbuild": "0.19.5",
"ts-node": "^10.9.1",
"jscodeshift": "^0.15.1",
"@types/jscodeshift": "^0.11.10",
"vitest": "^1.0.1",
"@vitest/coverage-v8": "^1.0.1"
},
"main": "./dist/index.cjs",
"types": "/dist/index.d.ts",
"scripts": {
"build:cjs": "cjs-builder ./src/index.ts",
"test": "vitest run",
"test:watch": "vitest watch",
"coverage": "vitest run --coverage"
},
"files": [
"README.md",
"config.json",
"./dist/index.cjs",
"./index.d.ts"
],
"type": "module"
}
51 changes: 51 additions & 0 deletions codemods/typescript/migrate-modules/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { API, FileInfo } from 'jscodeshift';

export default function transformer(file: FileInfo, api: API) {
const j = api.jscodeshift;
const root = j(file.source);

root.find(j.TSModuleDeclaration).replaceWith((path) => {
const { node } = path;

if (node.body && node.body.type === 'TSModuleBlock') {
const body = node.body.body.map((declaration) => {
if (declaration.type === 'TSModuleDeclaration') {
return j.exportDeclaration(
[
j.exportSpecifier(
j.identifier(declaration.id.name),
j.identifier(declaration.id.name),
),
],
j.literal(null),
j.objectExpression([]),
);
} else {
return declaration;
}
});

return body;
}

return node;
});

root.find(j.TSImportEqualsDeclaration).replaceWith((path) => {
const { node } = path;
if (node.id.type === 'Identifier') {
return j.importDeclaration(
[j.importNamespaceSpecifier(node.id)],
j.literal(
node.moduleReference.type === 'TSExternalModuleReference'
? node.moduleReference.expression.value
: null,
),
);
}
});

root.find(j.TSExportAssignment).remove();

return root.toSource();
}
207 changes: 207 additions & 0 deletions codemods/typescript/migrate-modules/test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import assert from 'node:assert';
import { FileInfo } from 'jscodeshift';
import { describe, it } from 'vitest';
import transform from '../src/index';
import { buildApi } from '@codemod-registry/utilities';

describe('typescript migrate-modules', function () {
it('1', function () {
const INPUT = `
module Module1 {
export interface Person {
name: string;
age: number;
}

export function greet(person: Person): string {
return 'Hello';
}
}

export = Module1;
`;

const OUTPUT = `
export interface Person {
name: string;
age: number;
}

export function greet(person: Person): string {
return 'Hello';
}
`;

const fileInfo: FileInfo = {
path: 'index.js',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('tsx'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, ''),
);
});

it('2', function () {
const INPUT = `
module Module1 {
import Module2 = require('./Module2');

export interface Person {
name: string;
age: number;
}

export function greet(person: Person): string {
return 'Hello';
}

export function waveHands(): void {
Module2.wave();
}
}

export = Module1;
`;

const OUTPUT = `
import * as Module2 from './Module2';

export interface Person {
name: string;
age: number;
}

export function greet(person: Person): string {
return 'Hello';
}

export function waveHands(): void {
Module2.wave();
}
`;

const fileInfo: FileInfo = {
path: 'index.js',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('tsx'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, ''),
);
});

it('aliases', function () {
const INPUT = `
module Module1 {
import Module2 = require('./Module2');

export interface Person {
name: string;
age: number;
}

export function greet(person: Person): string {
return 'Hello';
}

export { Module2 as AliasModule };
}

export = Module1;
`;

const OUTPUT = `
import * as Module2 from './Module2';

export interface Person {
name: string;
age: number;
}

export function greet(person: Person): string {
return 'Hello';
}

export { Module2 as AliasModule };
`;

const fileInfo: FileInfo = {
path: 'index.js',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('tsx'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, ''),
);
});

it('nested', function () {
const INPUT = `
module Module1 {
module NestedModule {
export const constantValue = 42;
}

export { NestedModule };
}

export = Module1;
`;

const OUTPUT = `
export module NestedModule {
export const constantValue = 42;
}
`;

const fileInfo: FileInfo = {
path: 'index.js',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('tsx'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, ''),
);
});

it('Exporting and Importing Types', function () {
const INPUT = `
module MyModule {
import SomeType = require('./SomeType');
export { SomeType };
}

export = MyModule;
`;

const OUTPUT = `
import * as SomeType from './SomeType';
export { SomeType };
`;

const fileInfo: FileInfo = {
path: 'index.js',
source: INPUT,
};

const actualOutput = transform(fileInfo, buildApi('tsx'));

assert.deepEqual(
actualOutput?.replace(/\W/gm, ''),
OUTPUT.replace(/\W/gm, ''),
);
});
});
9 changes: 9 additions & 0 deletions codemods/typescript/migrate-modules/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@codemod-registry/tsconfig",
"include": [
"./src/**/*.ts",
"./src/**/*.js",
"./test/**/*.ts",
"./test/**/*.js"
]
}
Empty file.
5 changes: 5 additions & 0 deletions codemods/typescript/recipe/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"schemaVersion": "1.0.0",
"engine": "recipe",
"names": ["typescript/migrate-modules"]
}
8 changes: 8 additions & 0 deletions codemods/typescript/recipe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@codemod-registry/typescript-recipe",
"files": [
"README.md",
"config.json"
],
"type": "module"
}
Loading
Loading