Skip to content

Commit

Permalink
fix: correct glob pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobJingleheimer committed Aug 25, 2024
1 parent 23b4069 commit 26ae3be
Showing 1 changed file with 51 additions and 41 deletions.
92 changes: 51 additions & 41 deletions src/workflow.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,64 @@
import type { Api } from "@codemod.com/workflow";
import {
type Api,
api,
} from '@codemod.com/workflow';
import type { Helpers } from '@codemod.com/workflow/dist/jsFam.d.ts';

import { mapImports } from './map-imports.ts';


export async function workflow({ contexts, files }: Api) {
await files("**/*.{cjs,mjs,js,jsx,(d.)?cts,(d.)?mts,(d.)?ts,tsx}").jsFam(async ({ astGrep }) => {
await files(globPattern).jsFam(processModule);

async function processModule({ astGrep }: Helpers) {
const filepath = contexts.getFileContext().file; // absolute fs path
await astGrep`rule:
await astGrep`rule:
any:
- kind: import_statement
- kind: export_statement
has:
kind: string`
.replace(async ({ getNode }) => {
const statement = getNode();
const importSpecifier = statement.find({
rule: {
kind: "string_fragment",
inside: {
kind: "string",
},
},
});

if (!importSpecifier) return;

const { isType, replacement } = await mapImports(filepath, importSpecifier.text());

if (!replacement) return;

const edits = [importSpecifier.replace(replacement)];

if (
isType
&&
!statement.children().some((node) => node.kind() === 'type')
) {
const clause = statement.find({
rule: {
any: [
{ kind: 'import_clause' },
{ kind: 'export_clause' },
],
},
});

if (clause) edits[1] = clause.replace(`type ${clause.text()}`);
}

return statement.commitEdits(edits);
});
});
const statement = getNode();
const importSpecifier = statement.find({
rule: {
kind: "string_fragment",
inside: {
kind: "string",
},
},
});

if (!importSpecifier) return;

const { isType, replacement } = await mapImports(filepath, importSpecifier.text());

if (!replacement) return;

const edits = [importSpecifier.replace(replacement)];

if (
isType
&&
!statement.children().some((node) => node.kind() === 'type')
) {
const clause = statement.find({
rule: {
any: [
{ kind: 'import_clause' },
{ kind: 'export_clause' },
],
},
});

if (clause) edits[1] = clause.replace(`type ${clause.text()}`);
}

return statement.commitEdits(edits);
});
}
}

const globPattern = '**/*.{cjs,mjs,js,jsx,?(d.)cts,?(d.)mts,?(d.)ts,tsx}';

workflow(api).then(console.log);

0 comments on commit 26ae3be

Please sign in to comment.