Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/parser/tokenizers/name.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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();

Expand Down
94 changes: 94 additions & 0 deletions tests/unit/spec-name-tokenizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
);
});
Loading