-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90a610b
commit 0bc879b
Showing
9 changed files
with
191 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@effect/codemod": patch | ||
--- | ||
|
||
Add codemods for swap-type-params in Effect,Exit,STM,Stream,Layer,Schema and add-tag-identifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"access": "public", | ||
"directory": "dist" | ||
}, | ||
"packageManager": "[email protected]", | ||
"description": "Code mod's for the Effect ecosystem", | ||
"engines": { | ||
"node": ">=16.17.1" | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type cs from "jscodeshift" | ||
|
||
export default function transformer(file: cs.FileInfo, api: cs.API) { | ||
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
root.find(j.VariableDeclaration).forEach(ast => { | ||
writeTagIdentifier(ast, j) | ||
}) | ||
|
||
return root.toSource() | ||
} | ||
|
||
const writeTagIdentifier = ( | ||
ast: cs.ASTPath<cs.VariableDeclaration>, | ||
j: cs.API["jscodeshift"], | ||
) => { | ||
if (ast.value.declarations.length === 1) { | ||
const declaration = ast.value.declarations[0] | ||
if ( | ||
declaration.type === "VariableDeclarator" | ||
&& declaration.init | ||
&& declaration.init.type === "CallExpression" | ||
) { | ||
const init = declaration.init | ||
const callee = init.callee | ||
const isTag = (node: typeof callee): boolean => { | ||
switch (node.type) { | ||
case "Identifier": { | ||
return node.name === "Tag" | ||
} | ||
case "MemberExpression": { | ||
return isTag(node.property) | ||
} | ||
default: { | ||
return false | ||
} | ||
} | ||
} | ||
if ( | ||
isTag(callee) | ||
&& init.arguments.length === 0 | ||
&& declaration.id.type === "Identifier" | ||
) { | ||
init.arguments.push(j.stringLiteral(`@services/${declaration.id.name}`)) | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import type k from "ast-types/gen/kinds.js" | ||
import type cs from "jscodeshift" | ||
import type { Collection } from "jscodeshift/src/Collection" | ||
|
||
export default function transformer(file: cs.FileInfo, api: cs.API) { | ||
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
forEveryTypeReference(root, j, ast => { | ||
swapParams(ast, "Effect", 3) | ||
swapParams(ast, "Stream", 3) | ||
swapParams(ast, "STM", 3) | ||
swapParams(ast, "Layer", 3) | ||
swapParams(ast, "Exit", 2) | ||
swapSchema(ast, j) | ||
}) | ||
|
||
return root.toSource() | ||
} | ||
|
||
// | ||
// utilities | ||
// | ||
|
||
const swapParams = ( | ||
ast: cs.ASTPath<cs.TSTypeReference>, | ||
name: string, | ||
size: number, | ||
) => { | ||
if (hasName(ast, name) && ast.value.typeParameters?.params.length === size) { | ||
const params = ast.value.typeParameters.params | ||
params.reverse() | ||
for (let i = 0; i < size - 1; i++) { | ||
popNever(params) | ||
} | ||
} | ||
} | ||
|
||
const swapSchema = ( | ||
ast: cs.ASTPath<cs.TSTypeReference>, | ||
j: cs.API["jscodeshift"], | ||
) => { | ||
if (hasName(ast, "Schema") && ast.value.typeParameters?.params.length === 3) { | ||
const params = ast.value.typeParameters.params | ||
params.reverse() | ||
popNever(params) | ||
if ( | ||
params.length === 2 | ||
&& j(params[0]).toSource() === j(params[1]).toSource() | ||
) { | ||
params.pop() | ||
} | ||
} | ||
} | ||
|
||
const popNever = (params: Array<k.TSTypeKind>) => { | ||
if ( | ||
params.length > 0 | ||
&& params[params.length - 1].type === "TSNeverKeyword" | ||
) { | ||
params.pop() | ||
} | ||
} | ||
|
||
const hasName = (reference: cs.ASTPath<cs.TSTypeReference>, name: string) => { | ||
const initial = reference.value.typeName | ||
const loop = (node: typeof initial): boolean => { | ||
switch (node.type) { | ||
case "Identifier": { | ||
return node.name === name | ||
} | ||
case "JSXIdentifier": { | ||
return false | ||
} | ||
case "TSQualifiedName": { | ||
return loop(node.right) | ||
} | ||
case "TSTypeParameter": { | ||
return false | ||
} | ||
} | ||
} | ||
return loop(initial) | ||
} | ||
|
||
// | ||
// this is needed to resolve a bug in jscodeshift that | ||
// forgets to traverse type parameters in call expressions | ||
// | ||
|
||
declare module "ast-types/gen/namedTypes" { | ||
namespace namedTypes { | ||
interface CallExpression extends TSHasOptionalTypeParameterInstantiation {} | ||
} | ||
} | ||
|
||
const forEveryTypeReference = ( | ||
node: Collection<any>, | ||
j: cs.API["jscodeshift"], | ||
f: (ast: cs.ASTPath<cs.TSTypeReference>) => void, | ||
) => { | ||
const visited = new Set() | ||
node.find(j.TSTypeReference).forEach(ast => { | ||
if (!visited.has(ast)) { | ||
visited.add(ast) | ||
f(ast) | ||
} | ||
}) | ||
node.find(j.CallExpression).forEach(path => { | ||
const typeParams = path.value.typeParameters | ||
if (typeParams) { | ||
j(typeParams).find(j.TSTypeReference).forEach(ast => { | ||
if (!visited.has(ast)) { | ||
visited.add(ast) | ||
f(ast) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters