Skip to content

Commit 58f9829

Browse files
committed
feat: Add libDomPatching that adds more completions for events (for JS projects only). However it adds incorrect completions sometimes, so enable only if you're sure about it!
1 parent 9cb7f19 commit 58f9829

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

src/configurationType.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,12 @@ export type Configuration = {
253253
*/
254254
'jsxImproveElementsSuggestions.filterNamespaces': boolean
255255
/**
256-
* Requires TS server restart
256+
* Requires TS server restart. Recommended to enable only per project.
257+
*
258+
* Enables better lib.dom completions (such as input events). For JS projects only (that don't use `tsc`)!
257259
* @default false
258260
* */
259-
// 'eventTypePatching.enable': boolean
261+
libDomPatching: boolean
260262
// 'globalTypedQuerySelector.enable': boolean,
261263
/**
262264
* For DX in JS projects only!

typescript/src/decorateProxy.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import lodashGet from 'lodash.get'
1414
import decorateWorkspaceSymbolSearch from './workspaceSymbolSearch'
1515
import decorateFormatFeatures from './decorateFormatFeatures'
1616
import namespaceAutoImports from './namespaceAutoImports'
17+
import libDomPatching from './libDomPatching'
1718

1819
/** @internal */
1920
export const thisPluginMarker = '__essentialPluginsMarker__'
@@ -164,6 +165,8 @@ export const decorateLanguageService = (
164165
return languageService.findRenameLocations(fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename)
165166
}
166167

168+
libDomPatching(languageServiceHost, c)
169+
167170
if (pluginSpecificSyntaxServerConfigCheck) {
168171
if (!__WEB__) {
169172
// dedicated syntax server (which is enabled by default), which fires navtree doesn't seem to receive onConfigurationChanged

typescript/src/libDomPatching.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import type tslib from 'typescript/lib/tsserverlibrary'
1+
import { GetConfig } from './types'
2+
import { patchMethod } from './utils'
23

3-
// not used for now
4-
export default (info: ts.server.PluginCreateInfo) => {
5-
// info.serverHost.readFile = fileName => {
6-
// let contents = realReadFile(fileName)
7-
// if (fileName.endsWith('/node_modules/typescript/lib/lib.dom.d.ts') && c('eventTypePatching.enable')) {
8-
// contents = contents
9-
// ?.replace('interface EventTarget {', 'interface EventTarget extends HTMLElement {')
10-
// .replace('"change": Event;', '"change": Event & {currentTarget: HTMLInputElement, target: HTMLInputElement};')
11-
// .replace('"change": Event;', '"change": Event & {currentTarget: HTMLInputElement, target: HTMLInputElement};')
12-
// .replace('"input": Event;', '"input": Event & {currentTarget: HTMLInputElement, target: HTMLInputElement};')
13-
// }
14-
// return contents
15-
// }
4+
export default (languageServiceHost: ts.LanguageServiceHost, c: GetConfig) => {
5+
if (!c('libDomPatching')) return
6+
patchMethod(languageServiceHost, 'getScriptSnapshot', oldMethod => fileName => {
7+
const scriptSnapshot = oldMethod(fileName)
8+
if (!fileName.endsWith('/node_modules/typescript/lib/lib.dom.d.ts') /* && c('eventTypePatching.enable') */) {
9+
return scriptSnapshot
10+
}
11+
if (!scriptSnapshot) return
12+
let contents = scriptSnapshot.getText(0, scriptSnapshot.getLength())
13+
contents = contents
14+
// .replace(/(interface EventListener \{\n\s*\(evt: )Event(\): void;\n\})/, '$1CustomEvent$2')
15+
.replace('interface EventTarget {', 'interface EventTarget extends HTMLElement {')
16+
.replace('"change": Event;', '"change": Event & {currentTarget: HTMLInputElement, target: HTMLInputElement};')
17+
.replace('"change": Event;', '"change": Event & {currentTarget: HTMLInputElement, target: HTMLInputElement};')
18+
.replace('"input": Event;', '"input": Event & {currentTarget: HTMLInputElement, target: HTMLInputElement};')
19+
return ts.ScriptSnapshot.fromString(contents)
20+
})
1621
}

0 commit comments

Comments
 (0)