diff --git a/packages/ts-codegen/__tests__/cleanse.test.ts b/packages/ts-codegen/__tests__/cleanse.test.ts index f3980c2f..a4ee10c2 100644 --- a/packages/ts-codegen/__tests__/cleanse.test.ts +++ b/packages/ts-codegen/__tests__/cleanse.test.ts @@ -2,10 +2,22 @@ import { writeFileSync } from 'fs'; import { sync as mkdirp } from 'mkdirp'; import { readSchemas } from '../src/utils'; +import { cleanFor } from '../src/utils/cleanse'; const FIXTURE_DIR = __dirname + '/../../../__fixtures__'; const OUTPUT_DIR = __dirname + '/../../../__output__'; +describe('cleanFor', () => { + it('works', () => { + expect(cleanFor('NftInfoResponse_for_Empty')).toEqual( + 'NftInfoResponseForEmpty' + ); + expect(cleanFor('UpdateCollectionInfoMsg_for_RoyaltyInfoResponse')).toEqual( + 'UpdateCollectionInfoMsgForRoyaltyInfoResponse' + ); + }); +}); + it('sg721', async () => { const out = OUTPUT_DIR + '/sg721'; const schemaDir = FIXTURE_DIR + '/sg721/'; diff --git a/packages/ts-codegen/src/utils/cleanse.ts b/packages/ts-codegen/src/utils/cleanse.ts index 9ae2bd75..a724d330 100644 --- a/packages/ts-codegen/src/utils/cleanse.ts +++ b/packages/ts-codegen/src/utils/cleanse.ts @@ -1,13 +1,20 @@ import { pascal } from 'case'; -const cleanFor = (str: string) => { +export const cleanFor = (str: string) => { /* 1. look at first char after _for_ 2. ONLY if you find capitals after, modify it */ - while (/_[a-z]+_[A-Z]/.test(str)) { - const m = str.match(/(_[a-z]+_)[A-Z]/); - str = str.replace(m[1], pascal(m[1])); + + // When we upgrade to eslint v9, we can remove this exception and + // rely on allExceptWhileTrue (https://eslint.org/docs/latest/rules/no-constant-condition) + // eslint-disable-next-line no-constant-condition + while (true) { + const match = str.match(/(_[a-z]+_)[A-Z]/); + if (!match) break; + // this replace is unsafe as it replaces the same text but maybe + // in a different location than the match + str = str.replace(match[1], pascal(match[1])); } return str;