') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Add custom importers for importing types/values by phated · Pull Request #464 · reactjs/react-docgen · GitHub
Skip to content

Add custom importers for importing types/values - #464

Merged
danez merged 70 commits into
reactjs:masterfrom
phated:master
Oct 21, 2020
Merged

Add custom importers for importing types/values#464
danez merged 70 commits into
reactjs:masterfrom
phated:master

Conversation

@phated

@phatedphated commented Sep 1, 2020

Copy link
Copy Markdown
Contributor

Alright, I think this is ready to get some serious feedback. Promoting from draft!

Building on the excellent work by @devongovett and @motiz88, I've been trying to implement the requested changes in #352 (specifically #352 (comment))

What I've done

  • Made the "importer" configurable. It is passed as an argument to the parse method, but if using the defaultParse from main.js, it is an additional option that can be specified. This is to make everything backwards compatible.
  • Made the default importer a no-op, which might result in some extra function calls during resolution, but keeps all behavior completely the same unless someone explicitly overrides it. This is to make everything backwards compatible.
  • Threaded the importer throughout the codebase.
  • Added tests for a "mockImporter" throughout the entire test suite.
  • Renamed the importer added in Support for importing types #352 to fsImporter and wrapped it in a factory function so the module lookup and cache could be configured. This also allows caching to be done for entire run of react-docgen (if you have a way to reuse your cache, it can even be done between runs).

Stuff I still need to do:

  • Add tests for makeFsImporter
  • Add docs for importers

Comment threadpackage.json Outdated
@phated

Copy link
Copy Markdown
ContributorAuthor

I might have run into a problem when testing this against some local code today. Need to dig in further.

@shilmanshilman mentioned this pull request Sep 14, 2020
2 tasks
motiz88and others added 27 commits September 20, 2020 12:50
Squashed commit of the following:
commit 3b3bbf6
Author: Daniel Tschinder <daniel@tschinder.de>
Date: Thu Nov 21 21:52:40 2019 +0100
Add additional test
commit 048646d
Author: Daniel Tschinder <daniel@tschinder.de>
Date: Thu Nov 21 21:41:41 2019 +0100
Fix order of deps
commit e69ef4f
Author: Daniel Tschinder <daniel@tschinder.de>
Date: Thu Nov 21 21:39:59 2019 +0100
Add mjs
commit 26874ee
Merge: 02649b7b3601b6
Author: Daniel Tschinder <daniel@tschinder.de>
Date: Fri Oct 25 22:25:43 2019 +0200
Merge branch 'master' into importing
# Conflicts:
#	src/utils/isReactComponentClass.js
commit 02649b7
Merge: 8c0a15faa54200
Author: Daniel Tschinder <daniel@tschinder.de>
Date: Fri Oct 25 22:21:51 2019 +0200
Merge branch 'master' into importing
# Conflicts:
#	src/__tests__/__snapshots__/main-test.js.snap
#	src/__tests__/fixtures/component_28.tsx
commit 8c0a15f
Author: Devon Govett <devongovett@gmail.com>
Date: Sat May 4 17:56:04 2019 -0700
Missing typedef
commit 08b5e7f
Author: Devon Govett <devongovett@gmail.com>
Date: Sat May 4 17:45:34 2019 -0700
Fix lint
commit c180af8
Author: Devon Govett <devongovett@gmail.com>
Date: Sat May 4 15:08:19 2019 -0700
Support named re-exports
commit 80200c8
Author: Devon Govett <devongovett@gmail.com>
Date: Sat May 4 13:18:59 2019 -0700
Handle recursive imports
commit 79a814f
Author: Devon Govett <devongovett@gmail.com>
Date: Sat May 4 13:09:16 2019 -0700
Support export all declaration
commit 840ca88
Author: Devon Govett <devongovett@gmail.com>
Date: Fri May 3 23:25:57 2019 -0700
Handle namespace imports
commit 268b828
Author: Devon Govett <devongovett@gmail.com>
Date: Fri May 3 22:51:00 2019 -0700
Add noop browser version of resolveImportedValue
commit 00b3d35
Author: Devon Govett <devongovett@gmail.com>
Date: Sun Apr 28 21:59:00 2019 -0700
Initial support for importing prop types
This is not airtight.
1. We may need to make this configurable with a pragma comment.
2. We should probably shallowly resolve the import and record the file and exported name of the alias, instead of just the local name.
@phated
phated marked this pull request as ready for review September 21, 2020 23:00
@phated

Copy link
Copy Markdown
ContributorAuthor

Alright, I think this is ready for an initial review while I continue the follow up work of adding test for makeFsImporter and document importers.

What do you think @danez?

@phatedphated changed the title WIP: Importing typesAdd custom importers for importing types/valuesSep 21, 2020
@shilman

Copy link
Copy Markdown

Heroic work @phated 🎖️ 🏅 🥇

@phated

Copy link
Copy Markdown
ContributorAuthor

It's probably worth mentioning; I bootstrapped this into a large project that I'm working on and it works splendidly!

@kivervinicius

Copy link
Copy Markdown

@phated wow!

@kivervinicius

Copy link
Copy Markdown

@phated can i use this with you repository? In npm

@danez

danez commented Oct 21, 2020

Copy link
Copy Markdown
Collaborator

I will merge now and release an alpha version with it. Feel free to create follow up PRs for the tests, bugfixes etc.

yarn add react-docgen@next

@stu039

Copy link
Copy Markdown

@danez - Any documentation on usage of this please? Using imported proptypes has been an issue for us for a while. If there is no documentation showing how to enable this in the alpha version, we may have to move away from docgen.

@danez

danez commented Jun 2, 2021

Copy link
Copy Markdown
Collaborator

@stu039 see #507 the cli does not support it at the moment. There might still be bugs, so if you find anything not working please report.

@dozortsev

Copy link
Copy Markdown

@danez@phated I guess note that imported types aren't supported is redundant, right?

https://github.com/reactjs/react-docgen/blob/master/README.md#flow-and-typescript-support

Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@phated@shilman@kivervinicius@danez@stu039@dozortsev@motiz88