Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add invariant codemod #91

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions codemods/invariant/index.js
Original file line number Diff line number Diff line change
@@ -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();
},
};
}
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -235,6 +236,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,
Expand Down Expand Up @@ -321,4 +323,4 @@ export const codemods = {
"typed-array-length": typedArrayLength,
"typedarray.prototype.slice": typedarrayPrototypeSlice,
"xtend": xtend,
};
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/codemod.test.js
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/invariant/cjs/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const invariant = require("tiny-invariant");

const a = 10;

invariant(a === 10, "a is 10");

invariant(a === 20, "a is 20");
7 changes: 7 additions & 0 deletions test/fixtures/invariant/cjs/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const invariant = require('invariant');

const a = 10;

invariant(a === 10, "a is 10");

invariant(a === 20, "a is 20");
7 changes: 7 additions & 0 deletions test/fixtures/invariant/cjs/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const invariant = require("tiny-invariant");

const a = 10;

invariant(a === 10, "a is 10");

invariant(a === 20, "a is 20");
7 changes: 7 additions & 0 deletions test/fixtures/invariant/esm-default/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import invariant from "tiny-invariant";

const a = 10;

invariant(a === 10, "a is 10");

invariant(a === 20, "a is 20");
7 changes: 7 additions & 0 deletions test/fixtures/invariant/esm-default/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import invariant from "invariant";

const a = 10;

invariant(a === 10, "a is 10");

invariant(a === 20, "a is 20");
7 changes: 7 additions & 0 deletions test/fixtures/invariant/esm-default/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import invariant from "tiny-invariant";

const a = 10;

invariant(a === 10, "a is 10");

invariant(a === 20, "a is 20");
7 changes: 7 additions & 0 deletions test/fixtures/invariant/esm/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { invariant } from "tiny-invariant";

const a = 10;

invariant(a === 10, "a is 10");

invariant(a === 20, "a is 20");
7 changes: 7 additions & 0 deletions test/fixtures/invariant/esm/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { invariant } from "invariant";

const a = 10;

invariant(a === 10, "a is 10");

invariant(a === 20, "a is 20");
7 changes: 7 additions & 0 deletions test/fixtures/invariant/esm/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { invariant } from "tiny-invariant";

const a = 10;

invariant(a === 10, "a is 10");

invariant(a === 20, "a is 20");