diff --git a/src/parser/tokenizers/name.ts b/src/parser/tokenizers/name.ts index 1571099e..fabf02de 100644 --- a/src/parser/tokenizers/name.ts +++ b/src/parser/tokenizers/name.ts @@ -1,4 +1,4 @@ -import { Spec, Line } from '../../primitives.js'; +import { Spec, Line, Tokens } from '../../primitives.js'; import { splitSpace, isSpace } from '../../util.js'; import { Tokenizer } from './index.js'; @@ -16,14 +16,18 @@ export default function nameTokenizer(): Tokenizer { // look for the name starting in the line where {type} ends let finalTypeLine = spec.source.reduce(typeEnd, 0); - let tokens; - do { + let tokens: Tokens; + if (spec.type) { + do { + ({ tokens } = spec.source[finalTypeLine]); + if (tokens.description.trim()) { + break; + } + finalTypeLine++; + } while (spec.source[finalTypeLine]); + } else { ({ tokens } = spec.source[finalTypeLine]); - if (tokens.description.trim()) { - break; - } - finalTypeLine++; - } while (spec.source[finalTypeLine]); + } const source = tokens.description.trimStart(); diff --git a/tests/unit/spec-name-tokenizer.spec.ts b/tests/unit/spec-name-tokenizer.spec.ts index 0bf4f458..cffcb236 100644 --- a/tests/unit/spec-name-tokenizer.spec.ts +++ b/tests/unit/spec-name-tokenizer.spec.ts @@ -734,3 +734,97 @@ test('after multiline {type}', () => { }) ); }); + +test('name on second line after type', () => { + const sourceIn = [ + { + number: 0, + source: '...', + tokens: seedTokens({ + tag: '@param', + postTag: ' ', + type: '{string}', + postType: '', + description: '', + }), + }, + { + number: 1, + source: '...', + tokens: seedTokens({ + description: 'foo The foo description.', + }), + }, + ]; + + const sourceOut = JSON.parse(JSON.stringify(sourceIn)); + Object.assign(sourceOut[1].tokens, { + name: 'foo', + postName: ' ', + description: 'The foo description.', + }); + + expect(tokenize(seedSpec({ type: 'string', source: sourceIn }))).toEqual( + seedSpec({ + type: 'string', + name: 'foo', + source: sourceOut, + }) + ); +}); + +test('no type - should not look for name on subsequent lines', () => { + const sourceIn = [ + { + number: 0, + source: '...', + tokens: seedTokens({ + tag: '@example', + postTag: '', + description: '', + }), + }, + { + number: 1, + source: '...', + tokens: seedTokens({ + description: 'const foo = bar()', + }), + }, + ]; + + expect(tokenize(seedSpec({ source: sourceIn }))).toEqual( + seedSpec({ + name: '', + source: sourceIn, + }) + ); +}); + +test('no type with comment syntax - should not parse as name', () => { + const sourceIn = [ + { + number: 0, + source: '...', + tokens: seedTokens({ + tag: '@example', + postTag: '', + description: '', + }), + }, + { + number: 1, + source: '...', + tokens: seedTokens({ + description: '// Create something', + }), + }, + ]; + + expect(tokenize(seedSpec({ source: sourceIn }))).toEqual( + seedSpec({ + name: '', + source: sourceIn, + }) + ); +});