From b109766168e0cdc95f7bc26e8a34c627c1aa54bc Mon Sep 17 00:00:00 2001 From: Antony David Date: Thu, 8 Aug 2024 22:08:10 +0200 Subject: [PATCH 1/2] feat: add invariant codemod --- codemods/invariant/index.js | 47 +++++++++++++++++++ index.js | 4 +- package-lock.json | 4 +- test/codemod.test.js | 2 +- test/fixtures/invariant/cjs/after.js | 7 +++ test/fixtures/invariant/cjs/before.js | 7 +++ test/fixtures/invariant/cjs/result.js | 7 +++ test/fixtures/invariant/esm-default/after.js | 7 +++ test/fixtures/invariant/esm-default/before.js | 7 +++ test/fixtures/invariant/esm-default/result.js | 7 +++ test/fixtures/invariant/esm/after.js | 7 +++ test/fixtures/invariant/esm/before.js | 7 +++ test/fixtures/invariant/esm/result.js | 7 +++ 13 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 codemods/invariant/index.js create mode 100644 test/fixtures/invariant/cjs/after.js create mode 100644 test/fixtures/invariant/cjs/before.js create mode 100644 test/fixtures/invariant/cjs/result.js create mode 100644 test/fixtures/invariant/esm-default/after.js create mode 100644 test/fixtures/invariant/esm-default/before.js create mode 100644 test/fixtures/invariant/esm-default/result.js create mode 100644 test/fixtures/invariant/esm/after.js create mode 100644 test/fixtures/invariant/esm/before.js create mode 100644 test/fixtures/invariant/esm/result.js diff --git a/codemods/invariant/index.js b/codemods/invariant/index.js new file mode 100644 index 0000000..959ae9a --- /dev/null +++ b/codemods/invariant/index.js @@ -0,0 +1,47 @@ +import jscodeshift from 'jscodeshift'; + +/** + * @typedef {import('../../types.js').Codemod} Codemod + * @typedef {import('../../types.js').CodemodOptions} CodemodOptions + */ + +/** + * @param {CodemodOptions} [options] + * @returns {Codemod} + */ +export default function (options) { + return { + name: 'invariant', + transform: ({ file }) => { + const j = jscodeshift; + const root = j(file.source); + + // Transform import declarationss + // biome-ignore lint/complexity/noForEach: Need to iterate through statements. + root.find(j.ImportDeclaration).forEach((path) => { + if ( + j.Literal.check(path.node.source) && + path.node.source.value === 'invariant' + ) { + path.node.source.value = 'tiny-invariant'; + } + }); + + // Transform require statements + // biome-ignore lint/complexity/noForEach: Need to iterate through statements. + root + .find(j.CallExpression, { callee: { name: 'require' } }) + .forEach((path) => { + if ( + path.node.arguments.length === 1 && + j.Literal.check(path.node.arguments[0]) && + path.node.arguments[0].value === 'invariant' + ) { + path.node.arguments[0].value = 'tiny-invariant'; + } + }); + + return root.toSource(); + }, + }; +} diff --git a/index.js b/index.js index d2ad16d..debd483 100644 --- a/index.js +++ b/index.js @@ -68,6 +68,7 @@ import hasSymbols from './codemods/has-symbols/index.js'; import hasTostringtag from './codemods/has-tostringtag/index.js'; import hasown from './codemods/hasown/index.js'; import indexOf from './codemods/index-of/index.js'; +import invariant from './codemods/invariant/index.js'; import isArrayBuffer from './codemods/is-array-buffer/index.js'; import isBooleanObject from './codemods/is-boolean-object/index.js'; import isBuiltinModule from './codemods/is-builtin-module/index.js'; @@ -232,6 +233,7 @@ export const codemods = { "has-tostringtag": hasTostringtag, "hasown": hasown, "index-of": indexOf, + "invariant": invariant, "is-array-buffer": isArrayBuffer, "is-boolean-object": isBooleanObject, "is-builtin-module": isBuiltinModule, @@ -315,4 +317,4 @@ export const codemods = { "typed-array-length": typedArrayLength, "typedarray.prototype.slice": typedarrayPrototypeSlice, "xtend": xtend, -}; \ No newline at end of file +}; diff --git a/package-lock.json b/package-lock.json index fae58c0..1967297 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "module-replacements-codemods", - "version": "1.0.2", + "version": "1.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "module-replacements-codemods", - "version": "1.0.2", + "version": "1.0.4", "license": "MIT", "dependencies": { "@ast-grep/napi": "^0.25.4", diff --git a/test/codemod.test.js b/test/codemod.test.js index 41dbd88..56e3c2e 100644 --- a/test/codemod.test.js +++ b/test/codemod.test.js @@ -1,6 +1,6 @@ import { describe, it } from 'node:test'; import fs from 'node:fs'; -import assert from 'assert'; +import assert from 'node:assert'; import { codemods } from '../index.js'; describe('codemod', async () => { diff --git a/test/fixtures/invariant/cjs/after.js b/test/fixtures/invariant/cjs/after.js new file mode 100644 index 0000000..79eec47 --- /dev/null +++ b/test/fixtures/invariant/cjs/after.js @@ -0,0 +1,7 @@ +const invariant = require("tiny-invariant"); + +const a = 10; + +invariant(a === 10, "a is 10"); + +invariant(a === 20, "a is 20"); diff --git a/test/fixtures/invariant/cjs/before.js b/test/fixtures/invariant/cjs/before.js new file mode 100644 index 0000000..83cc309 --- /dev/null +++ b/test/fixtures/invariant/cjs/before.js @@ -0,0 +1,7 @@ +const invariant = require('invariant'); + +const a = 10; + +invariant(a === 10, "a is 10"); + +invariant(a === 20, "a is 20"); diff --git a/test/fixtures/invariant/cjs/result.js b/test/fixtures/invariant/cjs/result.js new file mode 100644 index 0000000..79eec47 --- /dev/null +++ b/test/fixtures/invariant/cjs/result.js @@ -0,0 +1,7 @@ +const invariant = require("tiny-invariant"); + +const a = 10; + +invariant(a === 10, "a is 10"); + +invariant(a === 20, "a is 20"); diff --git a/test/fixtures/invariant/esm-default/after.js b/test/fixtures/invariant/esm-default/after.js new file mode 100644 index 0000000..281edcb --- /dev/null +++ b/test/fixtures/invariant/esm-default/after.js @@ -0,0 +1,7 @@ +import { invariant } from "tiny-invariant"; + +const a = 10; + +invariant(a === 10, "a is 10"); + +invariant(a === 20, "a is 20"); diff --git a/test/fixtures/invariant/esm-default/before.js b/test/fixtures/invariant/esm-default/before.js new file mode 100644 index 0000000..9f10b30 --- /dev/null +++ b/test/fixtures/invariant/esm-default/before.js @@ -0,0 +1,7 @@ +import { invariant } from "invariant"; + +const a = 10; + +invariant(a === 10, "a is 10"); + +invariant(a === 20, "a is 20"); diff --git a/test/fixtures/invariant/esm-default/result.js b/test/fixtures/invariant/esm-default/result.js new file mode 100644 index 0000000..281edcb --- /dev/null +++ b/test/fixtures/invariant/esm-default/result.js @@ -0,0 +1,7 @@ +import { invariant } from "tiny-invariant"; + +const a = 10; + +invariant(a === 10, "a is 10"); + +invariant(a === 20, "a is 20"); diff --git a/test/fixtures/invariant/esm/after.js b/test/fixtures/invariant/esm/after.js new file mode 100644 index 0000000..f7e14ca --- /dev/null +++ b/test/fixtures/invariant/esm/after.js @@ -0,0 +1,7 @@ +import invariant from "tiny-invariant"; + +const a = 10; + +invariant(a === 10, "a is 10"); + +invariant(a === 20, "a is 20"); diff --git a/test/fixtures/invariant/esm/before.js b/test/fixtures/invariant/esm/before.js new file mode 100644 index 0000000..e76038b --- /dev/null +++ b/test/fixtures/invariant/esm/before.js @@ -0,0 +1,7 @@ +import invariant from "invariant"; + +const a = 10; + +invariant(a === 10, "a is 10"); + +invariant(a === 20, "a is 20"); diff --git a/test/fixtures/invariant/esm/result.js b/test/fixtures/invariant/esm/result.js new file mode 100644 index 0000000..f7e14ca --- /dev/null +++ b/test/fixtures/invariant/esm/result.js @@ -0,0 +1,7 @@ +import invariant from "tiny-invariant"; + +const a = 10; + +invariant(a === 10, "a is 10"); + +invariant(a === 20, "a is 20"); From 6e3460d9d6b2d93fc886230ff715ab2c1e5c4cab Mon Sep 17 00:00:00 2001 From: Antony David Date: Thu, 8 Aug 2024 22:13:01 +0200 Subject: [PATCH 2/2] fix: inverted default test folder --- test/fixtures/invariant/esm-default/after.js | 2 +- test/fixtures/invariant/esm-default/before.js | 2 +- test/fixtures/invariant/esm-default/result.js | 2 +- test/fixtures/invariant/esm/after.js | 2 +- test/fixtures/invariant/esm/before.js | 2 +- test/fixtures/invariant/esm/result.js | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/fixtures/invariant/esm-default/after.js b/test/fixtures/invariant/esm-default/after.js index 281edcb..f7e14ca 100644 --- a/test/fixtures/invariant/esm-default/after.js +++ b/test/fixtures/invariant/esm-default/after.js @@ -1,4 +1,4 @@ -import { invariant } from "tiny-invariant"; +import invariant from "tiny-invariant"; const a = 10; diff --git a/test/fixtures/invariant/esm-default/before.js b/test/fixtures/invariant/esm-default/before.js index 9f10b30..e76038b 100644 --- a/test/fixtures/invariant/esm-default/before.js +++ b/test/fixtures/invariant/esm-default/before.js @@ -1,4 +1,4 @@ -import { invariant } from "invariant"; +import invariant from "invariant"; const a = 10; diff --git a/test/fixtures/invariant/esm-default/result.js b/test/fixtures/invariant/esm-default/result.js index 281edcb..f7e14ca 100644 --- a/test/fixtures/invariant/esm-default/result.js +++ b/test/fixtures/invariant/esm-default/result.js @@ -1,4 +1,4 @@ -import { invariant } from "tiny-invariant"; +import invariant from "tiny-invariant"; const a = 10; diff --git a/test/fixtures/invariant/esm/after.js b/test/fixtures/invariant/esm/after.js index f7e14ca..281edcb 100644 --- a/test/fixtures/invariant/esm/after.js +++ b/test/fixtures/invariant/esm/after.js @@ -1,4 +1,4 @@ -import invariant from "tiny-invariant"; +import { invariant } from "tiny-invariant"; const a = 10; diff --git a/test/fixtures/invariant/esm/before.js b/test/fixtures/invariant/esm/before.js index e76038b..9f10b30 100644 --- a/test/fixtures/invariant/esm/before.js +++ b/test/fixtures/invariant/esm/before.js @@ -1,4 +1,4 @@ -import invariant from "invariant"; +import { invariant } from "invariant"; const a = 10; diff --git a/test/fixtures/invariant/esm/result.js b/test/fixtures/invariant/esm/result.js index f7e14ca..281edcb 100644 --- a/test/fixtures/invariant/esm/result.js +++ b/test/fixtures/invariant/esm/result.js @@ -1,4 +1,4 @@ -import invariant from "tiny-invariant"; +import { invariant } from "tiny-invariant"; const a = 10;