@makeshift27015/typescript-yaml-plugin 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +52 -0
- package/dist/index.js +102 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 João Cabral Pinto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# typescript-yaml-plugin
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.com/package/typescript-yaml-plugin)
|
|
4
|
+
[](https://github.com/cabralpinto/typescript-yaml-plugin/blob/main/LICENSE)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
|
|
7
|
+
Import `.yaml` files in TypeScript 5+ with autocomplete and type checking.
|
|
8
|
+
|
|
9
|
+
<sub></sub>
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Install the plugin:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install --save-dev typescript-yaml-plugin
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Update your tsconfig.json:
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"compilerOptions": {
|
|
24
|
+
"plugins": [{ "name": "typescript-yaml-plugin" }]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Start importing YAML files! 🎉
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import schema from './schema.yaml';
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### VSCode Users
|
|
36
|
+
|
|
37
|
+
Make sure your editor is using the workspace version of TypeScript (the one where the plugin is installed). To do this:
|
|
38
|
+
|
|
39
|
+
1. Open the Command Palette (`Ctrl+Shift+P` or `Cmd+Shift+P`)
|
|
40
|
+
2. Run `TypeScript: Select TypeScript Version`
|
|
41
|
+
3. Choose `Use Workspace Version`
|
|
42
|
+
|
|
43
|
+
## Notes
|
|
44
|
+
|
|
45
|
+
- This plugin uses the [`yaml`](https://www.npmjs.com/package/yaml) package under the hood. Supported features and limitations are fully inherited from that library.
|
|
46
|
+
- This plugin is **only for editor support** (autocomplete and type-checking). It does **not** make `.yaml` files work at runtime. To actually be able to import YAML files in your running code, you’ll need to pair this with a runtime plugin that handles `.yaml` files like [`bun-plugin-yaml`](https://www.npmjs.com/package/bun-plugin-yaml)
|
|
47
|
+
or [`@modyfi/vite-plugin-yaml`](https://www.npmjs.com/package/@modyfi/vite-plugin-yaml), depending on your runtime.
|
|
48
|
+
- This plugin was inspired by [`typescript-plugin-yaml`](https://github.com/await-ovo/typescript-plugin-yaml), which is no longer maintained and does not support TypeScript 5+.
|
|
49
|
+
|
|
50
|
+
## Contributing
|
|
51
|
+
|
|
52
|
+
Issues and PRs are welcome!
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const fs_1 = __importDefault(require("fs"));
|
|
6
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
8
|
+
const hasConstAttribute = (ts_, node) => {
|
|
9
|
+
var _a, _b;
|
|
10
|
+
const importDecl = ts_.isImportDeclaration(node.parent) ? node.parent : (_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent;
|
|
11
|
+
if (!importDecl || !ts_.isImportDeclaration(importDecl))
|
|
12
|
+
return false;
|
|
13
|
+
const attributes = (_b = importDecl.attributes) === null || _b === void 0 ? void 0 : _b.elements;
|
|
14
|
+
if (!attributes)
|
|
15
|
+
return false;
|
|
16
|
+
return attributes.some(attr => attr.name.text === 'const' &&
|
|
17
|
+
ts_.isStringLiteral(attr.value) &&
|
|
18
|
+
attr.value.text === 'true');
|
|
19
|
+
};
|
|
20
|
+
module.exports = ({ typescript: ts_ }) => ({
|
|
21
|
+
create: (info) => {
|
|
22
|
+
var _a;
|
|
23
|
+
const logger = info.project.projectService.logger;
|
|
24
|
+
const { languageServiceHost, languageService } = info;
|
|
25
|
+
const constImports = new Set();
|
|
26
|
+
const getScriptKind = (_a = languageServiceHost.getScriptKind) === null || _a === void 0 ? void 0 : _a.bind(languageServiceHost);
|
|
27
|
+
languageServiceHost.getScriptKind = fileName => {
|
|
28
|
+
if (!getScriptKind)
|
|
29
|
+
return ts_.ScriptKind.Unknown;
|
|
30
|
+
if (/\.ya?ml$/.test(fileName))
|
|
31
|
+
return ts_.ScriptKind.TS;
|
|
32
|
+
return getScriptKind(fileName);
|
|
33
|
+
};
|
|
34
|
+
const fileExists = languageServiceHost.fileExists.bind(languageServiceHost);
|
|
35
|
+
const getScriptSnapshot = languageServiceHost.getScriptSnapshot.bind(languageServiceHost);
|
|
36
|
+
languageServiceHost.getScriptSnapshot = fileName => {
|
|
37
|
+
if (!/\.ya?ml$/.test(fileName))
|
|
38
|
+
return getScriptSnapshot(fileName);
|
|
39
|
+
if (!fileExists(fileName))
|
|
40
|
+
return;
|
|
41
|
+
const content = fs_1.default.readFileSync(fileName, 'utf8');
|
|
42
|
+
let object;
|
|
43
|
+
try {
|
|
44
|
+
object = yaml_1.default.parse(content);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
logger.info(`[typescript-plugin-yaml] YAML.parse error:\n${error}`);
|
|
48
|
+
}
|
|
49
|
+
const asConst = constImports.has(fileName) ? ' as const' : '';
|
|
50
|
+
const text = `export default ${JSON.stringify(object)}${asConst};`;
|
|
51
|
+
return ts_.ScriptSnapshot.fromString(text);
|
|
52
|
+
};
|
|
53
|
+
const resolveModuleNameLiterals = languageServiceHost.resolveModuleNameLiterals.bind(languageServiceHost);
|
|
54
|
+
languageServiceHost.resolveModuleNameLiterals = (moduleLiterals, containingFile, ...rest) => resolveModuleNameLiterals(moduleLiterals, containingFile, ...rest).map((resolvedModule, index) => {
|
|
55
|
+
const moduleLiteral = moduleLiterals[index];
|
|
56
|
+
const moduleName = moduleLiteral.text;
|
|
57
|
+
if (!/\.ya?ml$/.test(moduleName))
|
|
58
|
+
return resolvedModule;
|
|
59
|
+
const resolvedFileName = resolvedModule.failedLookupLocations[1].slice(0, -3);
|
|
60
|
+
if (hasConstAttribute(ts_, moduleLiteral)) {
|
|
61
|
+
constImports.add(resolvedFileName);
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
...resolvedModule,
|
|
65
|
+
resolvedModule: {
|
|
66
|
+
extension: ts_.Extension.Ts,
|
|
67
|
+
isExternalLibraryImport: false,
|
|
68
|
+
resolvedFileName
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
const languageServiceOverride = {
|
|
73
|
+
getCompletionsAtPosition(fileName, position, options, formattingSettings) {
|
|
74
|
+
var _a;
|
|
75
|
+
const completions = languageService.getCompletionsAtPosition(fileName, position, options, formattingSettings);
|
|
76
|
+
if (!completions)
|
|
77
|
+
return completions;
|
|
78
|
+
const sourceFile = (_a = info.languageService.getProgram()) === null || _a === void 0 ? void 0 : _a.getSourceFile(fileName);
|
|
79
|
+
if (!sourceFile)
|
|
80
|
+
return completions;
|
|
81
|
+
const token = ts_.getTokenAtPosition(sourceFile, position);
|
|
82
|
+
if (!ts_.isModuleSpecifierLike(token))
|
|
83
|
+
return completions;
|
|
84
|
+
const [{ failedLookupLocations }] = resolveModuleNameLiterals([token], fileName, undefined, info.project.getCompilerOptions(), sourceFile, undefined);
|
|
85
|
+
fs_1.default.globSync(`${path_1.default.dirname(failedLookupLocations[0])}/*.{yaml,yml}`)
|
|
86
|
+
.map(fileName => path_1.default.basename(fileName))
|
|
87
|
+
.forEach(baseFileName => completions.entries.push({
|
|
88
|
+
name: baseFileName,
|
|
89
|
+
kind: ts_.ScriptElementKind.scriptElement,
|
|
90
|
+
kindModifiers: '.yaml',
|
|
91
|
+
sortText: '11'
|
|
92
|
+
}));
|
|
93
|
+
return completions;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const languageServiceProxy = new Proxy(languageService, {
|
|
97
|
+
get: (target, key) => { var _a; return (_a = languageServiceOverride[key]) !== null && _a !== void 0 ? _a : target[key]; }
|
|
98
|
+
});
|
|
99
|
+
return languageServiceProxy;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../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typescript-yaml-plugin",
|
|
3
|
+
"version": "1.0.11",
|
|
4
|
+
"description": "TypeScript Language Service plugin for YAML",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"ts",
|
|
8
|
+
"typescript-5",
|
|
9
|
+
"ts-5",
|
|
10
|
+
"yaml",
|
|
11
|
+
"yml",
|
|
12
|
+
"plugin",
|
|
13
|
+
"language-service",
|
|
14
|
+
"intellisense",
|
|
15
|
+
"autocomplete"
|
|
16
|
+
],
|
|
17
|
+
"author": "João Cabral Pinto",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/cabralpinto/typescript-yaml-plugin.git"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/cabralpinto/typescript-yaml-plugin#readme",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/cabralpinto/typescript-yaml-plugin/issues"
|
|
25
|
+
},
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"files": [
|
|
29
|
+
"./dist",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc",
|
|
34
|
+
"format": "prettier --write ."
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"typescript": ">=5.0"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"typescript": "^5.9.3",
|
|
41
|
+
"yaml": "^2.8.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^24.2.0",
|
|
45
|
+
"prettier": "^3.6.2"
|
|
46
|
+
}
|
|
47
|
+
}
|