Skip to content

add better handling for typescript compiler #2088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@babel/preset-env": "^7.19.4",
"@babel/preset-react": "^7.18.6",
"@types/jest": "^29.2.0",
"@types/ramda": "^0.28.14",
"@types/react": "^17.0.39",
"babel-loader": "^8.2.5",
"jest": "^29.2.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {WrappedHTMLProps} from '../props';
import { WrappedHTMLProps } from '../props';

/**
* Component docstring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export {
FCComponent,
EmptyComponent,
MixedComponent,
RequiredChildrenComponent,
RequiredChildrenComponent
};
43 changes: 35 additions & 8 deletions dash/extract-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function help() {
console.error('usage: ');
console.error(
'extract-meta ^fileIgnorePattern ^forbidden$|^props$|^patterns$' +
' path/to/component(s) [path/to/more/component(s) ...] > metadata.json'
' path/to/component(s) [path/to/more/component(s) ...] > metadata.json'
);
}

Expand Down Expand Up @@ -98,7 +98,7 @@ function isReservedPropName(propName) {
if (reservedPattern.test(propName)) {
process.stderr.write(
`\nERROR: "${propName}" matches reserved word ` +
`pattern: ${reservedPattern.toString()}\n`
`pattern: ${reservedPattern.toString()}\n`
);
failedBuild = true;
}
Expand All @@ -118,9 +118,15 @@ function checkDocstring(name, value) {
function docstringWarning(doc) {
checkDocstring(doc.displayName, doc.description);

Object.entries(doc.props || {}).forEach(([name, p]) =>
checkDocstring(`${doc.displayName}.${name}`, p.description)
);
const exportedDisplayName = doc.exportedName || doc.displayName
if (doc && doc.props) {
Object.entries(doc.props || {}).forEach(([name, p]) =>
checkDocstring(`${exportedDisplayName}.${name}`, p.description)
);
} else {
// Soft fail
console.warn(`${exportedDisplayName} does not have any props`)
}
}

function zipArrays(...arrays) {
Expand Down Expand Up @@ -160,9 +166,9 @@ function gatherComponents(sources, components = {}) {
const extension = path.extname(filepath);
if (['.jsx', '.js'].includes(extension)) {
components[cleanPath(filepath)] = parseJSX(filepath);
} else if (filepath.endsWith('.tsx')) {
} else if (['.tsx', 'ts'].includes(extension)) {
try {
const name = /(.*)\.tsx/.exec(path.basename(filepath))[1];
const name = /(.*)\.(ts|tsx)/.exec(path.basename(filepath))[1];
filepaths.push(filepath);
names.push(name);
} catch (err) {
Expand Down Expand Up @@ -720,6 +726,24 @@ function gatherComponents(sources, components = {}) {
}
}

const isArrowFunction = ts.isArrowFunction(declaration)
const isClass = ts.isClassDeclaration(declaration);
const isFunction = ts.isFunctionDeclaration(declaration)

if (!isArrowFunction && !isClass && !isFunction) {
// we do not care about these exports
return null
}

if (isArrowFunction || isFunction) {
const signature = checker.getSignaturesOfType(type, ts.SignatureKind.Call)[0];
const returnType = checker.typeToString(signature.getReturnType());
if (!['Element', 'any', 'null'].includes(returnType)) {
// Not JSX so no need to classifiy as compnent
return null;
}
}

let defaultProps = getDefaultProps(typeSymbol, source);
const propsType = getPropsForFunctionalComponent(type);
const isContext = !!type.getProperty('isContext');
Expand Down Expand Up @@ -764,9 +788,12 @@ function gatherComponents(sources, components = {}) {
displayName: name,
description,
props,
isContext
isContext,
exportedName: rootExp.name
};
docstringWarning(doc);

// todo: Add support for mutiple components in single file
components[cleanPath(filepath)] = doc;
});
});
Expand Down