Skip to content
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

Fix ts->proptype script #2

Merged
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
28 changes: 21 additions & 7 deletions scripts/babel/proptypes-from-ts-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const path = require('path');
const babelTemplate = require('babel-template');
const babelCore = require('@babel/core');

const importedDefinitionsCache = new Map();

// react-docgen does not understand typescript annotations
function stripTypeScript(filename, ast) {
return babelCore.transform(
Expand Down Expand Up @@ -794,6 +796,16 @@ const typeDefinitionExtractors = {
return [];
}

if (importedDefinitionsCache.has(resolvedPath)) {
return importedDefinitionsCache.get(resolvedPath);
}

// to support circular dependencies, create & pre-cache the array of imported dependencies
// this array is directly mutated after parsing the subsequent files, supporting
// the circular nature as values settle into the correct locations
const importedDefinitions = [];
importedDefinitionsCache.set(resolvedPath, importedDefinitions);

// load & parse the imported file
const ast = parse(fs.readFileSync(resolvedPath).toString());

Expand Down Expand Up @@ -825,18 +837,15 @@ const typeDefinitionExtractors = {
);

// for each importedTypeName, fully resolve the type information
const importedDefinitions = definitions.reduce(
(importedDefinitions, { name, definition }) => {
definitions.forEach(
({ name, definition }) => {
if (importedTypeNames.includes(name)) {
// this type declaration is imported by the parent script
const propTypes = getPropTypesForNode(definition, true, state);
propTypes.isAlreadyResolved = true; // when getPropTypesForNode is called on this node later, tell it to skip processing
importedDefinitions.push({ name, definition: propTypes });
}

return importedDefinitions;
},
[]
}
);

// reset typeDefinitions and continue processing the original file
Expand Down Expand Up @@ -1011,7 +1020,7 @@ function processComponentDeclaration(typeDefinition, path, state) {

// import PropTypes library if it isn't already
const proptypesBinding = getVariableBinding(path, 'PropTypes');
if (proptypesBinding == null) {
if (proptypesBinding == null && state.get('hasInjectedPropTypes') !== true) {
let targetNode;
// find the first statement in the program and import PropTypes there
targetNode = path;
Expand All @@ -1028,6 +1037,7 @@ function processComponentDeclaration(typeDefinition, path, state) {
types.stringLiteral('prop-types')
)
);
state.set('hasInjectedPropTypes', true);
}
}

Expand Down Expand Up @@ -1253,3 +1263,7 @@ module.exports = function propTypesFromTypeScript({ types }) {
},
};
};

module.exports.clearImportCache = function clearImportCache() {
importedDefinitionsCache.clear();
}
3 changes: 3 additions & 0 deletions scripts/babel/proptypes-from-ts-props/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const babelOptions = {
],
filename: 'somefile.tsx',
};
const babelPlugin = require('./index');

beforeEach(() => babelPlugin.clearImportCache());

describe('proptypes-from-ts-props', () => {

Expand Down