chore(test): add script for transforming lodash test codes (#516)

* Add transform lodash test code

* Simplify using destructure
This commit is contained in:
Dayong Lee 2024-09-12 21:31:29 +09:00 committed by GitHub
parent 33bdce1254
commit 9aa89a7cfb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 924 additions and 9 deletions

View File

@ -0,0 +1,21 @@
export function formatBrokenSyntax(source: string): string {
// Fix broken syntax
const brokenMatched = source.match(/(?<=,)[\s\d\w?]+\[.+\).toEqual\((?!\[).+\]\);/g);
if (brokenMatched != null) {
for (const match of brokenMatched) {
const splited = match.split(').toEqual(');
const formatted = `).toEqual(${splited.join(', ')}`;
source = source.replace(match, formatted);
}
}
// Remove deleting local variable
const deleteMatched = source.match(/delete [\w\d]+;/g);
if (deleteMatched != null) {
for (const match of deleteMatched) {
source = source.replace(match, '');
}
}
return source;
}

View File

@ -0,0 +1,56 @@
import type { Collection, JSCodeshift } from 'jscodeshift';
export function transformImport(root: Collection, jscodeshift: JSCodeshift): void {
const astPath = root.getAST()[0];
// Change `import {a, b} from './utils'` to `import {a} from '../_internal/a';\n import {b} from '../_internal/b';`
root
.find(jscodeshift.ImportDeclaration, {
source: {
value: './utils',
},
})
.forEach(({ node }) => {
if (node.specifiers) {
const excludes = ['_']; // Exclude lodash
const dirnames = '../_internal/';
node.specifiers
.filter(specifier => specifier.type === 'ImportSpecifier')
.forEach(specifier => {
if (specifier.type !== 'ImportSpecifier' || excludes.includes(specifier.imported.name)) {
return;
}
// Add new import to the top of the file
astPath.value.program.body.unshift(
jscodeshift.importDeclaration(
[jscodeshift.importSpecifier(jscodeshift.identifier(specifier.imported.name))],
jscodeshift.literal(`${dirnames}${specifier.imported.name}`)
)
);
});
}
})
.remove();
// Add import { describe, it, expect } from 'vitest';
const vitestImport = jscodeshift.importDeclaration(
[
jscodeshift.importSpecifier(jscodeshift.identifier('describe')),
jscodeshift.importSpecifier(jscodeshift.identifier('it')),
jscodeshift.importSpecifier(jscodeshift.identifier('expect')),
],
jscodeshift.literal('vitest')
);
astPath.value.program.body.unshift(vitestImport);
// Remove import from 'lodash'
root
.find(jscodeshift.ImportDeclaration, {
source: {
value: 'lodash',
},
})
.remove();
}

View File

@ -0,0 +1,37 @@
import type { Collection, JSCodeshift } from 'jscodeshift';
export function transformLodashStable(root: Collection, jscodeshift: JSCodeshift): void {
// Replace lodashStable.each and lodashStable.map with Array.prototype.forEach and Array.prototype.map
root
.find(jscodeshift.CallExpression, {
callee: {
type: 'MemberExpression',
object: {
name: 'lodashStable',
},
property: {
type: 'Identifier',
},
},
})
.replaceWith(({ node }) => {
// this condition is for type narrowing
if (node.callee.type === 'MemberExpression' && node.callee.property.type === 'Identifier') {
switch (node.callee.property.name) {
case 'each':
node.callee.property.name = 'forEach';
node.callee.object = node.arguments[0] as any;
return jscodeshift.callExpression(node.callee, node.arguments.slice(1));
case 'map':
node.callee.property.name = 'map';
node.callee.object = node.arguments[0] as any;
return jscodeshift.callExpression(node.callee, node.arguments.slice(1));
}
// Remove lodashStable from the callee
return jscodeshift.callExpression(node.callee.property, node.arguments);
}
return node;
});
}

View File

@ -0,0 +1,21 @@
import type { API, FileInfo } from 'jscodeshift';
import { formatBrokenSyntax } from './_internal/formatter/brokenSyntax';
import { transformImport } from './_internal/transform/import';
import { transformLodashStable } from './_internal/transform/lodashStable';
export default function transform(file: FileInfo, { jscodeshift }: API) {
try {
const root = jscodeshift(formatBrokenSyntax(file.source));
transformImport(root, jscodeshift);
transformLodashStable(root, jscodeshift);
return root.toSource();
} catch (error) {
if (error instanceof Error) {
console.error(`Error Messaging: ${error.message}`);
console.error('Please resolve the error before continuing.');
console.error('If you need help, please open an issue on GitHub.');
}
}
}

View File

@ -139,6 +139,7 @@
"@rollup/plugin-typescript": "^11.1.6",
"@types/broken-link-checker": "^0",
"@types/eslint": "^9",
"@types/jscodeshift": "^0.11.11",
"@types/node": "^20.12.11",
"@types/tar": "^6.1.13",
"@vitest/coverage-istanbul": "^1.5.2",
@ -149,6 +150,7 @@
"eslint-plugin-vue": "^9.28.0",
"execa": "^9.3.0",
"globals": "^15.9.0",
"jscodeshift": "^17.0.0",
"prettier": "^3.2.5",
"rollup": "^4.19.0",
"rollup-plugin-dts": "^6.1.1",
@ -166,6 +168,7 @@
"test": "vitest run --coverage --typecheck",
"bench": "vitest bench",
"lint": "eslint --config eslint.config.mjs",
"format": "prettier --write ."
"format": "prettier --write .",
"transform": "jscodeshift -t ./.scripts/tests/transform-lodash-test.ts $0 && prettier --write $0"
}
}

793
yarn.lock

File diff suppressed because it is too large Load Diff