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 strip-ansi codemod #99

Open
wants to merge 4 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
55 changes: 55 additions & 0 deletions codemods/strip-ansi/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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: 'strip-ansi',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

const importDeclarations = root.find(j.ImportDeclaration, {
source: {
value: 'strip-ansi',
},
});

let importName = 'stripAnsi';

for (const declaration of importDeclarations.nodes()) {
const name = declaration.specifiers?.[0].local?.name;
if (name) {
importName = name;
}
declaration.source.value = 'node:util';
declaration.specifiers = [j.importSpecifier(j.identifier('stripVTControlCharacters'))];
dirtyFlag = true;
}

const callExpression = root.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: importName,
},
});

for (const expression of callExpression.nodes()) {
if (expression.callee.type === 'Identifier') {
expression.callee.name = 'stripVTControlCharacters';
dirtyFlag = true;
}
}

return dirtyFlag ? root.toSource() : file.source;
},
};
}
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ import functionsHaveNames from './codemods/functions-have-names/index.js';
import getSymbolDescription from './codemods/get-symbol-description/index.js';
import global from './codemods/global/index.js';
import gopd from './codemods/gopd/index.js';
import has from './codemods/has/index.js';
import hasOwnProp from './codemods/has-own-prop/index.js';
import hasProto from './codemods/has-proto/index.js';
import hasSymbols from './codemods/has-symbols/index.js';
import hasTostringtag from './codemods/has-tostringtag/index.js';
import has from './codemods/has/index.js';
import hasown from './codemods/hasown/index.js';
import indexOf from './codemods/index-of/index.js';
import isArrayBuffer from './codemods/is-array-buffer/index.js';
Expand All @@ -76,8 +76,8 @@ import isEven from './codemods/is-even/index.js';
import isNan from './codemods/is-nan/index.js';
import isNegativeZero from './codemods/is-negative-zero/index.js';
import isNpm from './codemods/is-npm/index.js';
import isNumber from './codemods/is-number/index.js';
import isNumberObject from './codemods/is-number-object/index.js';
import isNumber from './codemods/is-number/index.js';
import isOdd from './codemods/is-odd/index.js';
import isPlainObject from './codemods/is-plain-object/index.js';
import isPrimitive from './codemods/is-primitive/index.js';
Expand All @@ -96,8 +96,8 @@ import mathClz32 from './codemods/math.clz32/index.js';
import mathF16round from './codemods/math.f16round/index.js';
import mathFround from './codemods/math.fround/index.js';
import mathImul from './codemods/math.imul/index.js';
import mathLog10 from './codemods/math.log10/index.js';
import mathLog1p from './codemods/math.log1p/index.js';
import mathLog10 from './codemods/math.log10/index.js';
import mathSign from './codemods/math.sign/index.js';
import md5 from './codemods/md5/index.js';
import numberIsfinite from './codemods/number.isfinite/index.js';
Expand Down Expand Up @@ -146,6 +146,7 @@ import stringPrototypeTrimleft from './codemods/string.prototype.trimleft/index.
import stringPrototypeTrimright from './codemods/string.prototype.trimright/index.js';
import stringPrototypeTrimstart from './codemods/string.prototype.trimstart/index.js';
import stringRaw from './codemods/string.raw/index.js';
import stripAnsi from './codemods/strip-ansi/index.js';
import symbolPrototypeDescription from './codemods/symbol.prototype.description/index.js';
import traverse from './codemods/traverse/index.js';
import typedArrayBuffer from './codemods/typed-array-buffer/index.js';
Expand Down Expand Up @@ -313,6 +314,7 @@ export const codemods = {
"string.prototype.trimright": stringPrototypeTrimright,
"string.prototype.trimstart": stringPrototypeTrimstart,
"string.raw": stringRaw,
"strip-ansi": stripAnsi,
"symbol.prototype.description": symbolPrototypeDescription,
"traverse": traverse,
"typed-array-buffer": typedArrayBuffer,
Expand All @@ -321,4 +323,4 @@ export const codemods = {
"typed-array-length": typedArrayLength,
"typedarray.prototype.slice": typedarrayPrototypeSlice,
"xtend": xtend,
};
};
3 changes: 3 additions & 0 deletions test/fixtures/strip-ansi/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { stripVTControlCharacters } from "node:util";

stripVTControlCharacters('\u001B[4mUnicorn\u001B[0m');
3 changes: 3 additions & 0 deletions test/fixtures/strip-ansi/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import stripAnsi from 'strip-ansi';

stripAnsi('\u001B[4mUnicorn\u001B[0m');
3 changes: 3 additions & 0 deletions test/fixtures/strip-ansi/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { stripVTControlCharacters } from "node:util";

stripVTControlCharacters('\u001B[4mUnicorn\u001B[0m');
3 changes: 3 additions & 0 deletions test/fixtures/strip-ansi/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { stripVTControlCharacters } from "node:util";

stripVTControlCharacters('\u001B[4mUnicorn\u001B[0m');
3 changes: 3 additions & 0 deletions test/fixtures/strip-ansi/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import strip from 'strip-ansi';

strip('\u001B[4mUnicorn\u001B[0m');
3 changes: 3 additions & 0 deletions test/fixtures/strip-ansi/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { stripVTControlCharacters } from "node:util";

stripVTControlCharacters('\u001B[4mUnicorn\u001B[0m');
Loading