-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Try aliasing preact typescript declaration to fix ts errors #11088
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
Merged
AbhiPrasad
merged 4 commits into
cl/screenshot-integration
from
abhi-screenshot-feedback-preact
Mar 14, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// preact does not support more modern TypeScript versions, which breaks our users that depend on older | ||
// TypeScript versions. To fix this, we shim the types from preact to be any and remove the dependency on preact | ||
// for types directly. This script is meant to be run after the build/npm/types-ts3.8 directory is created. | ||
|
||
// Path: build/npm/types-ts3.8/global.d.ts | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
/** | ||
* This regex looks for preact imports we can replace and shim out. | ||
* | ||
* Example: | ||
* import { ComponentChildren, VNode } from 'preact'; | ||
*/ | ||
const preactImportRegex = /import\s*{\s*([\w\s,]+)\s*}\s*from\s*'preact'\s*;?/; | ||
|
||
function walk(dir) { | ||
const files = fs.readdirSync(dir); | ||
files.forEach(file => { | ||
const filePath = path.join(dir, file); | ||
const stat = fs.lstatSync(filePath); | ||
if (stat.isDirectory()) { | ||
walk(filePath); | ||
} else { | ||
if (filePath.endsWith('.d.ts')) { | ||
const content = fs.readFileSync(filePath, 'utf8'); | ||
const capture = preactImportRegex.exec(content); | ||
if (capture) { | ||
const groups = capture[1].split(',').map(s => s.trim()); | ||
|
||
// This generates a shim snippet to replace the type imports from preact | ||
// It generates a snippet based on the capture groups of preactImportRegex. | ||
// | ||
// Example: | ||
// | ||
// import type { ComponentChildren, VNode } from 'preact'; | ||
// becomes | ||
// type ComponentChildren: any; | ||
// type VNode: any; | ||
const snippet = groups.reduce((acc, curr) => { | ||
const searchableValue = curr.includes(' as ') ? curr.split(' as ')[1] : curr; | ||
|
||
// look to see if imported as value, then we have to use declare const | ||
if (content.includes(`typeof ${searchableValue}`)) { | ||
return `${acc}declare const ${searchableValue}: any;\n`; | ||
} | ||
|
||
// look to see if generic type like Foo<T> | ||
if (content.includes(`${searchableValue}<`)) { | ||
return `${acc}type ${searchableValue}<T> = any;\n`; | ||
} | ||
|
||
// otherwise we can just leave as type | ||
return `${acc}type ${searchableValue} = any;\n`; | ||
}, ''); | ||
|
||
// we then can remove the import from preact | ||
const newContent = content.replace(preactImportRegex, '// replaced import from preact'); | ||
|
||
// and write the new content to the file | ||
fs.writeFileSync(filePath, snippet + newContent, 'utf8'); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function run() { | ||
// recurse through build/npm/types-ts3.8 directory | ||
const dir = path.join('build', 'npm', 'types-ts3.8'); | ||
walk(dir); | ||
} | ||
|
||
run(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shrewd readers would notice that this breaks completely with multiline imports
I know, but we can cross that bridge when we come to it - prefer to get this out to unblock asap.