-
-
Notifications
You must be signed in to change notification settings - Fork 613
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
build: migrate codegen to TS transformers #6141
Draft
AlCalzone
wants to merge
1
commit into
master
Choose a base branch
from
live-codegen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 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 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,66 @@ | ||
{ | ||
"name": "@zwave-js/ts-plugins", | ||
"version": "11.3.0", | ||
"description": "zwave-js: TypeScript plugins", | ||
"private": true, | ||
"keywords": [], | ||
"main": "build/index.js", | ||
"exports": { | ||
".": "./build/index.js", | ||
"./package.json": "./package.json", | ||
"./tsc": "./build/index_tsc.js", | ||
"./lsp": "./build/index_lsp.js" | ||
}, | ||
"types": "build/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"tsc": [ | ||
"build/index_tsc.d.ts" | ||
], | ||
"lsp": [ | ||
"build/index_lsp.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"build/**/*.{js,d.ts,map}" | ||
], | ||
"author": { | ||
"name": "AlCalzone", | ||
"email": "[email protected]" | ||
}, | ||
"license": "MIT", | ||
"homepage": "https://github.com/AlCalzone/node-zwave-js#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/AlCalzone/node-zwave-js.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/AlCalzone/node-zwave-js/issues" | ||
}, | ||
"funding": { | ||
"url": "https://github.com/sponsors/AlCalzone/" | ||
}, | ||
"engines": { | ||
"node": ">=14.13.0 <15 || >= 16 <16.9.0 || >16.9.0" | ||
}, | ||
"scripts": { | ||
"build": "tsc -b tsconfig.build.json --pretty", | ||
"clean": "del-cli build/ \"*.tsbuildinfo\"", | ||
"lint:ts": "eslint --ext .ts --rule \"prettier/prettier: off\" \"src/**/*.ts\"", | ||
"lint:ts:fix": "yarn run lint:ts --fix", | ||
"lint:prettier": "prettier -c \"src/**/*.ts\"", | ||
"lint:prettier:fix": "yarn run lint:prettier -w", | ||
"//test:ts": "ava", | ||
"//test:dirty": "node -r ../../maintenance/esbuild-register.js ../maintenance/src/resolveDirtyTests.ts --run" | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "^9.0.13", | ||
"@types/node": "^14.18.52", | ||
"del-cli": "^5.0.0", | ||
"esbuild": "0.15.18", | ||
"esbuild-register": "^3.4.2", | ||
"prettier": "^2.8.8", | ||
"typescript": "5.1.6" | ||
} | ||
} |
This file contains 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,130 @@ | ||
import type tslib from "typescript/lib/tsserverlibrary"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function init(modules: { typescript: typeof tslib }) { | ||
const ts = modules.typescript; | ||
|
||
function findChildContainingPosition( | ||
sourceFile: tslib.SourceFile, | ||
position: number, | ||
): tslib.Node | undefined { | ||
function find(node: tslib.Node): tslib.Node | undefined { | ||
const text = sourceFile.text.slice(node.getStart(), node.getEnd()); | ||
if (position >= node.getStart() && position <= node.getEnd()) { | ||
return ts.forEachChild(node, find) || node; | ||
} | ||
} | ||
return find(sourceFile); | ||
} | ||
|
||
function create(info: tslib.server.PluginCreateInfo) { | ||
// Get a list of things to remove from the completion list from the config object. | ||
// If nothing was specified, we'll just remove 'caller' | ||
const whatToRemove: string[] = info.config.remove || ["caller"]; | ||
Check notice Code scanning / CodeQL Unused variable, import, function or class Note
Unused variable whatToRemove.
|
||
|
||
// Diagnostic logging | ||
info.project.projectService.logger.info( | ||
"I'm getting set up now! Check the log for this message.", | ||
); | ||
|
||
// Set up decorator object | ||
const proxy: tslib.LanguageService = Object.create(null); | ||
for (const k of Object.keys(info.languageService) as Array< | ||
keyof tslib.LanguageService | ||
>) { | ||
const x = info.languageService[k]!; | ||
// @ts-expect-error - JS runtime trickery which is tricky to type tersely | ||
// prettier-ignore | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
proxy[k] = (...args: Array<{}>) => x.apply(info.languageService, args); | ||
} | ||
|
||
// Remove specified entries from completion list | ||
proxy.getCompletionsAtPosition = (fileName, position, options) => { | ||
const prior = info.languageService.getCompletionsAtPosition( | ||
fileName, | ||
position, | ||
options, | ||
); | ||
if (!prior) return; | ||
|
||
const program = info.languageService.getProgram(); | ||
const sourceFile = program?.getSourceFile(fileName); | ||
const checker = program?.getTypeChecker(); | ||
|
||
if (!sourceFile || !checker) return; | ||
|
||
const node = findChildContainingPosition(sourceFile, position); | ||
if (!node) return; | ||
|
||
const siblings = node.parent.getChildren(); | ||
const index = siblings.indexOf(node); | ||
let dot: tslib.Node | undefined; | ||
let thing: tslib.Node | undefined; | ||
if ( | ||
index >= 2 && | ||
siblings[index - 1]?.kind === ts.SyntaxKind.DotToken | ||
) { | ||
dot = siblings[index - 1]; | ||
thing = siblings[index - 2]; | ||
} else { | ||
return prior; | ||
} | ||
|
||
// What is being auto-completed on? | ||
const typeOfThing = checker.getTypeAtLocation(thing); | ||
const symbolOfThing = typeOfThing.getSymbol(); | ||
|
||
const isCCAPIs = | ||
symbolOfThing?.name === "CCAPIs" && | ||
symbolOfThing.flags === ts.SymbolFlags.Interface && | ||
symbolOfThing.declarations?.[0].getSourceFile().fileName === | ||
"/home/dominic/Repositories/node-zwave-js/packages/cc/build/lib/API.d.ts"; | ||
|
||
// const isZWaveController = | ||
// symbolOfThing?.name === "ZWaveController" && | ||
// symbolOfThing.valueDeclaration?.kind === | ||
// ts.SyntaxKind.ClassDeclaration && | ||
// symbolOfThing.valueDeclaration.getSourceFile().fileName === | ||
// "/home/dominic/Repositories/node-zwave-js/packages/zwave-js/src/lib/controller/Controller.ts"; | ||
|
||
if (isCCAPIs) { | ||
return { | ||
...prior, | ||
entries: [ | ||
...prior.entries, | ||
{ | ||
name: "AAABB", | ||
kind: ts.ScriptElementKind.memberVariableElement, | ||
kindModifiers: | ||
ts.ScriptElementKindModifier.ambientModifier, | ||
insertText: `["AAA BB"]`, | ||
sortText: "AAA BB", | ||
replacementSpan: { | ||
start: dot.getStart(), | ||
length: position - dot.getStart(), | ||
}, | ||
}, | ||
{ | ||
name: "AAACC", | ||
kind: ts.ScriptElementKind.memberVariableElement, | ||
kindModifiers: | ||
ts.ScriptElementKindModifier.ambientModifier, | ||
insertText: "AAACC", | ||
sortText: "AAACC", | ||
}, | ||
], | ||
}; | ||
} else { | ||
return prior; | ||
} | ||
}; | ||
|
||
return proxy; | ||
} | ||
|
||
return { create }; | ||
} | ||
|
||
/** from given position we find the child node that contains it */ | ||
export = init; |
This file contains 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,11 @@ | ||
// tsconfig for building - only applies to the src directory | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "build" | ||
}, | ||
"references": [], | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["src/**/*.test.ts"] | ||
} |
This file contains 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,8 @@ | ||
// tsconfig for IntelliSense - active in all files in the current package | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": {}, | ||
"references": [], | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["build/**", "node_modules/**"] | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.
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.
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note