Skip to content

Commit

Permalink
test: add test for strip-ansi codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
Namchee committed Nov 16, 2024
1 parent b5d6527 commit f1e27b3
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 13 deletions.
16 changes: 7 additions & 9 deletions codemods/strip-ansi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,24 @@ export default function (options) {

let importName = 'stripAnsi';

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

const callExpressions = root.find(j.CallExpression, {
root.find(j.CallExpression, {
callee: {
name: importName,
}
});

for (const expression of callExpressions) {
}).forEach((expression) => {
expression.value.callee.name = 'stripVTControlCharacters';

Check failure on line 43 in codemods/strip-ansi/index.js

View workflow job for this annotation

GitHub Actions / typecheck (20.x)

Property 'name' does not exist on type 'ExpressionKind'.
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource() : file.source;
},
Expand Down
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');

0 comments on commit f1e27b3

Please sign in to comment.