diff --git a/packages/tl-utils/src/codegen/utils.ts b/packages/tl-utils/src/codegen/utils.ts index 4860c332..9f322be2 100644 --- a/packages/tl-utils/src/codegen/utils.ts +++ b/packages/tl-utils/src/codegen/utils.ts @@ -10,9 +10,12 @@ export const camelToPascal = (s: string): string => export function jsComment(s: string): string { return ( '/**' + + // awesome hack not to break up {@link} links s + .replace(/{@link (.*?)}/g, '{@linkƒ$1}') .replace(/(?![^\n]{1,60}$)([^\n]{1,60})\s/g, '$1\n') - .replace(/\n|^/g, '\n * ') + + .replace(/\n|^/g, '\n * ') + .replace(/{@linkƒ(.*)?}/, '{@link $1}') + '\n */' ) } diff --git a/packages/tl-utils/tests/codegen/types.spec.ts b/packages/tl-utils/tests/codegen/types.spec.ts index 2a074dc0..9dd329c1 100644 --- a/packages/tl-utils/tests/codegen/types.spec.ts +++ b/packages/tl-utils/tests/codegen/types.spec.ts @@ -65,66 +65,92 @@ describe('generateTypescriptDefinitionsForTlEntry', () => { ) }) - it('adds comments', () => { - test( - '// This is a test constructor\ntest = Test;', - '/**', - ' * This is a test constructor', - ' */', - 'interface RawTest {', - " _: 'test';", - '}' - ) - test( - '// @description This is a test constructor\n' + - '// @field Some field\n' + - 'test field:int = Test;', - '/**', - ' * This is a test constructor', - ' */', - 'interface RawTest {', - " _: 'test';", - ' /**', - ' * Some field', - ' */', - ' field: number;', - '}' - ) - test( - '---functions---\n// This is a test method\ntest = Test;', - '/**', - ' * This is a test method', - ' * ', - ' * RPC method returns {@see tl.TypeTest}', - ' */', - 'interface RawTestRequest {', - " _: 'test';", - '}' - ) + describe('comments', () => { + it('adds tl style comments', () => { + test( + '// This is a test constructor\n' + 'test = Test;', + '/**', + ' * This is a test constructor', + ' */', + 'interface RawTest {', + " _: 'test';", + '}' + ) + test( + '---functions---\n' + + '// This is a test method\n' + + 'test = Test;', + '/**', + ' * This is a test method', + ' * ', + ' * RPC method returns {@link tl.TypeTest}', + ' */', + 'interface RawTestRequest {', + " _: 'test';", + '}' + ) + }) - test( - '// This is a test constructor with a very very very very very very very very long comment\ntest = Test;', - '/**', - ' * This is a test constructor with a very very very very very', - ' * very very very long comment', - ' */', - 'interface RawTest {', - " _: 'test';", - '}' - ) + it('adds tdlib style comments', () => { + test( + '// @description This is a test constructor\n' + + '// @field Some field\n' + + 'test field:int = Test;', + '/**', + ' * This is a test constructor', + ' */', + 'interface RawTest {', + " _: 'test';", + ' /**', + ' * Some field', + ' */', + ' field: number;', + '}' + ) + }) - test( - '---functions---\n// This is a test method with a very very very very very very very very long comment\ntest = Test;', - '/**', - ' * This is a test method with a very very very very very very', - ' * very very long comment', - ' * ', - ' * RPC method returns {@see tl.TypeTest}', - ' */', - 'interface RawTestRequest {', - " _: 'test';", - '}' - ) + it('wraps long comments', () => { + test( + '// This is a test constructor with a very very very very very very very very long comment\n' + + 'test = Test;', + '/**', + ' * This is a test constructor with a very very very very very', + ' * very very very long comment', + ' */', + 'interface RawTest {', + " _: 'test';", + '}' + ) + + test( + '---functions---\n' + + '// This is a test method with a very very very very very very very very long comment\n' + + 'test = Test;', + '/**', + ' * This is a test method with a very very very very very very', + ' * very very long comment', + ' * ', + ' * RPC method returns {@link tl.TypeTest}', + ' */', + 'interface RawTestRequest {', + " _: 'test';", + '}' + ) + }) + + it('should not break @link tags', () => { + test( + '// This is a test constructor with a very long comment {@link whatever} more text\n' + + 'test = Test;', + '/**', + ' * This is a test constructor with a very long comment', + ' * {@link whatever} more text', + ' */', + 'interface RawTest {', + " _: 'test';", + '}' + ) + }) }) it('writes generic types', () => { @@ -242,7 +268,7 @@ describe('generateTypescriptDefinitionsForTlSchema', () => { " _: 'test';", '}', '/**', - ' * RPC method returns {@see tl.TypeTest}', + ' * RPC method returns {@link tl.TypeTest}', ' */', 'interface RawGetTestRequest {', " _: 'getTest';", @@ -284,7 +310,7 @@ interface RawTest2 { _: 'test2'; } /** - * RPC method returns {@see tl.TypeTest} + * RPC method returns {@link tl.TypeTest} */ interface RawGetTestRequest { _: 'getTest'; @@ -303,7 +329,7 @@ namespace test { _: 'test.test2'; } /** - * RPC method returns {@see tl.test.TypeTest} + * RPC method returns {@link tl.test.TypeTest} */ interface RawGetTestRequest { _: 'test.getTest';