Skip to content

Commit d5f2aa5

Browse files
committed
add playground for contrib faster local prototyping
eslint things
1 parent a8d830d commit d5f2aa5

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# ignore files that have typescript import
22
typescript/**
33
src/configurationType.ts
4+
playground.ts

buildTsPlugin.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ export const buildTsPlugin = async (/** @type {string} */ outDir, /** @type {str
3030
}
3131

3232
const name = 'typescript-essential-plugins'
33-
buildTsPlugin(`out/node_modules/${name}`, name, 'typescript/src/index.ts')
33+
await buildTsPlugin(`out/node_modules/${name}`, name, 'typescript/src/index.ts')

playground.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ts-check
2+
import ts from 'typescript/lib/tsserverlibrary'
3+
import { createLanguageService } from './typescript/src/dummyLanguageService'
4+
5+
let testString = 'const a: {/** @default test */a: 5} | {b: 6, /** yes */a: 9} = null as any;\nif ("||" in a) {}'
6+
const replacement = '||'
7+
const pos = testString.indexOf(replacement)
8+
testString = testString.slice(0, pos) + testString.slice(pos + replacement.length)
9+
const filePath = '/test.ts'
10+
const languageService = createLanguageService({
11+
[filePath]: testString,
12+
})
13+
14+
const program = languageService.getProgram()
15+
const sourceFile = program?.getSourceFile(filePath)
16+
if (!program || !sourceFile) throw new Error('No source file')
17+
18+
const typeChecker = program.getTypeChecker()
19+
const node = findChildContainingPosition(ts, sourceFile, pos)
20+
if (!node) throw new Error('No node')
21+
const type = typeChecker.getTypeAtLocation(node)
22+
23+
// explore:
24+
25+
console.log(typeChecker.typeToString(type))
26+
27+
function findChildContainingPosition(
28+
typescript: typeof import('typescript/lib/tsserverlibrary'),
29+
sourceFile: ts.SourceFile,
30+
position: number,
31+
): ts.Node | undefined {
32+
function find(node: ts.Node): ts.Node | undefined {
33+
if (position >= node.getStart() && position < node.getEnd()) return typescript.forEachChild(node, find) || node
34+
35+
return
36+
}
37+
return find(sourceFile)
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// only for basic testing, as vscode is actually using server
2+
import ts from 'typescript/lib/tsserverlibrary'
3+
4+
export const createLanguageService = (files: Record<string, string>) => {
5+
const dummyVersion = '1'
6+
const languageService = ts.createLanguageService({
7+
getProjectVersion: () => dummyVersion,
8+
getScriptVersion: () => dummyVersion,
9+
getCompilationSettings: () => ({ allowJs: true, jsx: ts.JsxEmit.Preserve }),
10+
getScriptFileNames: () => Object.keys(files),
11+
getScriptSnapshot: fileName => {
12+
const contents = files[fileName]
13+
if (contents === undefined) return
14+
return ts.ScriptSnapshot.fromString(contents)
15+
},
16+
getCurrentDirectory: () => '',
17+
getDefaultLibFileName: () => 'defaultLib:lib.d.ts',
18+
})
19+
return languageService
20+
}

0 commit comments

Comments
 (0)