-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
581dfc7
commit ac3c27f
Showing
8 changed files
with
79 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# grats-ts-plugin | ||
|
||
Experimental TypeScript plugin for Grats. This plugin is not ready for production use. |
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 @@ | ||
"use strict"; | ||
|
||
// TypeScript is vary particular about how plugins are structured. They must be | ||
// CommonJS and they must have a single value for `module.exports`. Additionally, | ||
// the plugin must be a plain module name (no `/` characters allowed). | ||
// | ||
// However, the logic for the plugin is tightly coupled with Grats itself and | ||
// should really live (and ship) alongside each version of Grats. While | ||
// I continue to struggle with figuring out how to let Grats operate as its own | ||
// TypeScript plugin, this package is just a shim. | ||
module.exports = require("grats").initTsPlugin; |
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 @@ | ||
{ | ||
"name": "grats-ts-plugin", | ||
"version": "0.0.2", | ||
"license": "MIT", | ||
"main": "./", | ||
"module": "./index.js", | ||
"peerDependencies": { | ||
"grats": ">=0.0.19" | ||
}, | ||
"prettier": {} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
packages: | ||
- 'website' | ||
- 'examples/*' | ||
- 'examples/*' | ||
- 'grats-ts-plugin' |
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,43 @@ | ||
import type * as TS from "typescript/lib/tsserverlibrary"; | ||
import { extract } from "../Extractor"; | ||
|
||
// An experimental plugin for TypeScript that adds a new language service | ||
// which reports diagnostics for the current file. Currently it only reports | ||
// syntax errors because semantic errors are too expensive to compute on each | ||
// keystroke. | ||
export function initTsPlugin(modules: { typescript: typeof TS }) { | ||
const ts = modules.typescript; | ||
|
||
function create(info: TS.server.PluginCreateInfo): TS.LanguageService { | ||
const projectRoot = info.project.getCurrentDirectory(); | ||
info.project.projectService.logger.info( | ||
`Grats: Initializing Plugin with project root: ${projectRoot} and TypeScript version: ${ts.version}`, | ||
); | ||
|
||
// Set up decorator object | ||
const proxy: TS.LanguageService = Object.create(null); | ||
for (const k of Object.keys(info.languageService) as Array< | ||
keyof TS.LanguageService | ||
>) { | ||
const x = info.languageService[k]; | ||
// @ts-expect-error | ||
proxy[k] = (...args: Array<any>) => x.apply(info.languageService, args); | ||
} | ||
|
||
proxy.getSyntacticDiagnostics = (filename): TS.DiagnosticWithLocation[] => { | ||
const prior = info.languageService.getSyntacticDiagnostics(filename); | ||
const doc = info.languageService.getProgram()?.getSourceFile(filename); | ||
|
||
if (doc == null) return prior; | ||
const result = extract(doc); | ||
|
||
if (result.kind === "OK") return prior; | ||
|
||
return [...prior, ...result.err]; | ||
}; | ||
|
||
return proxy; | ||
} | ||
|
||
return { create }; | ||
} |
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 |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
"src/tests/fixtures", | ||
"docs", | ||
"examples", | ||
"website" | ||
"website", | ||
"grats-ts-plugin" | ||
] | ||
} |