Skip to content

Commit 1db2c14

Browse files
committed
signature code block
1 parent d284edb commit 1db2c14

File tree

7 files changed

+48
-29
lines changed

7 files changed

+48
-29
lines changed

src/generators/jsx-ast/constants.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,9 @@ export const OVERRIDDEN_POSITIONS = [
9595
];
9696

9797
// These types are methods, and have signatures we should enumerate
98-
export const TYPES_WITH_METHOD_SIGNATURES = ['ctor', 'method', 'classMethod'];
98+
export const TYPES_WITH_METHOD_SIGNATURES = [
99+
'class',
100+
'ctor',
101+
'method',
102+
'classMethod',
103+
];

src/generators/jsx-ast/utils/createSignatureElements.mjs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ export const parseListIntoProperties = node => {
3434

3535
if (children[0].type === 'inlineCode') {
3636
name = children.shift().value.trimEnd();
37-
} else if (children[0].value && children[0].value.startsWith('Returns')) {
38-
name = 'Returns';
39-
children[0].value = children[0].value.replace(/^Returns:?\s*/i, '');
40-
if (!children[0].value.trim()) {
41-
children.shift();
37+
} else if (children[0].value) {
38+
const starter = children[0].value.match(
39+
createQueries.QUERIES.typedListStarters
40+
);
41+
42+
if (starter) {
43+
name = starter[1] !== 'Type' && starter[1];
44+
children[0].value = children[0].value.slice(starter[0].length);
45+
if (!children[0].value.trim()) {
46+
children.shift();
47+
}
4248
}
4349
}
4450

@@ -89,13 +95,9 @@ export const createPropertyTable = (node, withHeading = true) => {
8995
const rows = properties.flatMap(prop => {
9096
const cells = [];
9197

92-
if (prop.name?.startsWith('Returns')) {
93-
cells.push(createElement('td', 'Returns'));
94-
} else {
95-
cells.push(
96-
createElement('td', prop.name ? createElement('code', prop.name) : '-')
97-
);
98-
}
98+
cells.push(
99+
createElement('td', prop.name ? createElement('code', prop.name) : '-')
100+
);
99101

100102
cells.push(createElement('td', prop.types.length > 0 ? prop.types : '-'));
101103
cells.push(createElement('td', prop.desc.length > 0 ? prop.desc : '-'));
@@ -139,9 +141,13 @@ export const createPropertyTable = (node, withHeading = true) => {
139141
*/
140142
export const generateSignature = (
141143
functionName,
142-
{ params, return: returnType },
144+
{ params, return: returnType, extends: extendsType },
143145
prefix = ''
144146
) => {
147+
if (extendsType) {
148+
return `class ${prefix}${functionName} extends ${extendsType.type}`;
149+
}
150+
145151
const returnStr = returnType ? `: ${returnType.type}` : '';
146152
const paramsStr = params
147153
.map(param => {
@@ -195,13 +201,19 @@ export const getFullName = ({ name, text }, fallback = name) => {
195201
*/
196202
export default ({ children }, { data }, idx) => {
197203
// Find the list in the parent
198-
const list = children.find(createQueries.UNIST.isTypedList);
204+
const listIdx = children.findIndex(createQueries.UNIST.isTypedList);
199205

200-
const params = list ? list.children.map(parseListItem) : [];
206+
const params =
207+
listIdx >= 0 ? children[listIdx].children.map(parseListItem) : [];
201208

202209
const signature = parseSignature(data.text, params);
203210
const displayName = getFullName(data);
204211

212+
if (data.type === 'class') {
213+
// Remove the 'Extends' list
214+
children.splice(listIdx, 1);
215+
}
216+
205217
children.splice(
206218
idx,
207219
0,

src/generators/legacy-json/constants.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Grabs a method's return value
2-
export const RETURN_EXPRESSION = /^returns?\s*:?\s*/i;
3-
41
// Grabs a method's name
52
export const NAME_EXPRESSION = /^['`"]?([^'`": {]+)['`"]?\s*:?\s*/;
63

src/generators/legacy-json/utils/parseList.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
DEFAULT_EXPRESSION,
33
LEADING_HYPHEN,
44
NAME_EXPRESSION,
5-
RETURN_EXPRESSION,
65
TYPE_EXPRESSION,
76
} from '../constants.mjs';
87
import parseSignature from './parseSignature.mjs';
@@ -59,9 +58,11 @@ export function parseListItem(child) {
5958
let text = current.textRaw;
6059

6160
// Identify return items or extract key properties (name, type, default) from the text
62-
if (RETURN_EXPRESSION.test(text)) {
63-
current.name = 'return';
64-
text = text.replace(RETURN_EXPRESSION, '');
61+
const starter = text.match(createQueries.QUERIES.typedListStarters);
62+
if (starter) {
63+
current.name =
64+
starter[1] === 'Returns' ? 'return' : starter[1].toLowerCase();
65+
text = text.slice(starter[0].length);
6566
} else {
6667
text = extractPattern(text, NAME_EXPRESSION, 'name', current);
6768
}

src/generators/legacy-json/utils/parseSignature.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ export default (textRaw, markdownParameters) => {
175175
*/
176176
const signature = { params: [] };
177177

178-
// Find the return value & filter it out
178+
// Find the return/extends value & filter it out
179179
markdownParameters = markdownParameters.filter(value => {
180-
if (value.name === 'return') {
181-
signature.return = value;
180+
if (value.name === 'return' || value.name === 'extends') {
181+
signature[value.name] = value;
182182
return false;
183183
}
184184

src/generators/web/ui/components/CodeBox.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default ({ className, ...props }) => {
4545
onCopy={onCopy}
4646
language={getLanguageDisplayName(language)}
4747
{...props}
48-
buttonText='Copy to clipboard'
48+
buttonText="Copy to clipboard"
4949
/>
5050
);
5151
};

src/utils/queries/index.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ createQueries.QUERIES = {
210210
yamlInnerContent: /^<!--[ ]?(?:YAML([\s\S]*?)|([ \S]*?))?[ ]?-->/,
211211
// ReGeX for finding references to Unix manuals
212212
unixManualPage: /\b([a-z.]+)\((\d)([a-z]?)\)/g,
213+
// ReGeX for determing a typed list's non-property names
214+
typedListStarters: /^(Returns|Extends|Type):?\s*/,
213215
};
214216

215217
createQueries.UNIST = {
@@ -282,8 +284,10 @@ createQueries.UNIST = {
282284
return false;
283285
}
284286

285-
// Check for "Returns"
286-
if (node.value?.trimStart().startsWith('Returns')) {
287+
// Check for other starters
288+
if (
289+
node.value?.trimStart().match(createQueries.QUERIES.typedListStarters)
290+
) {
287291
return true;
288292
}
289293

0 commit comments

Comments
 (0)