@glint/tsserver-plugin 0.2.1 → 1.0.1-unstable.845d5fa
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/README.md +4 -4
- package/lib/typescript-server-plugin.d.ts +34 -0
- package/lib/typescript-server-plugin.d.ts.map +1 -0
- package/lib/typescript-server-plugin.js +251 -0
- package/lib/typescript-server-plugin.js.map +1 -0
- package/package.json +27 -14
- package/lib/index.d.ts +0 -3
- package/lib/index.js +0 -57
- package/lib/language-service.d.ts +0 -32
- package/lib/language-service.js +0 -363
- package/lib/logging.d.ts +0 -11
- package/lib/logging.js +0 -53
- package/lib/patch/language-service-host.d.ts +0 -7
- package/lib/patch/language-service-host.js +0 -60
- package/lib/patch/lib.d.ts +0 -10
- package/lib/patch/lib.js +0 -101
- package/lib/path-transformation.d.ts +0 -15
- package/lib/path-transformation.js +0 -30
- package/lib/util/auto-import.d.ts +0 -3
- package/lib/util/auto-import.js +0 -105
- package/lib/util/logging.d.ts +0 -11
- package/lib/util/logging.js +0 -53
- package/lib/util/path-transformation.d.ts +0 -15
- package/lib/util/path-transformation.js +0 -30
- package/lib/virtual-module-manager.d.ts +0 -15
- package/lib/virtual-module-manager.js +0 -68
package/lib/util/auto-import.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rewriteAutoImportChange = exports.isAutoImportChange = void 0;
|
|
4
|
-
function isAutoImportChange(change) {
|
|
5
|
-
return change.newText.startsWith('import');
|
|
6
|
-
}
|
|
7
|
-
exports.isAutoImportChange = isAutoImportChange;
|
|
8
|
-
function rewriteAutoImportChange(ts, change, sourceFile) {
|
|
9
|
-
let importPath = getImportPath(ts, change.newText);
|
|
10
|
-
if (!importPath)
|
|
11
|
-
return change;
|
|
12
|
-
let existingImport = findExistingImport(ts, importPath, sourceFile);
|
|
13
|
-
if (!existingImport)
|
|
14
|
-
return change;
|
|
15
|
-
let combinedImport = synthesizeCombinedImport(ts, existingImport.text, change.newText);
|
|
16
|
-
if (!combinedImport)
|
|
17
|
-
return change;
|
|
18
|
-
return {
|
|
19
|
-
newText: combinedImport,
|
|
20
|
-
span: existingImport.span,
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
exports.rewriteAutoImportChange = rewriteAutoImportChange;
|
|
24
|
-
function findExistingImport(ts, path, sourceFile) {
|
|
25
|
-
let importDeclaration = sourceFile.statements.find((statement) => {
|
|
26
|
-
return (ts.isImportDeclaration(statement) &&
|
|
27
|
-
ts.isStringLiteral(statement.moduleSpecifier) &&
|
|
28
|
-
statement.moduleSpecifier.text === path);
|
|
29
|
-
});
|
|
30
|
-
if (importDeclaration) {
|
|
31
|
-
let text = importDeclaration.getText(sourceFile);
|
|
32
|
-
let start = importDeclaration.getStart(sourceFile);
|
|
33
|
-
let length = importDeclaration.getEnd() - start;
|
|
34
|
-
return { text, span: { start, length } };
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
function getImportPath(ts, text) {
|
|
38
|
-
try {
|
|
39
|
-
let file = ts.createSourceFile('import.ts', text, ts.ScriptTarget.Latest);
|
|
40
|
-
if (file.statements.length === 1) {
|
|
41
|
-
let statement = file.statements[0];
|
|
42
|
-
if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
43
|
-
return statement.moduleSpecifier.text;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
// Fall through and return undefined
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function synthesizeCombinedImport(ts, oldImportText, newImportText) {
|
|
52
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
53
|
-
let parsed = parseImportsTogether(ts, oldImportText, newImportText);
|
|
54
|
-
if (!parsed)
|
|
55
|
-
return;
|
|
56
|
-
let { oldImport, newImport, sourceFile } = parsed;
|
|
57
|
-
let newName = (_a = newImport.importClause) === null || _a === void 0 ? void 0 : _a.name;
|
|
58
|
-
let oldName = (_b = oldImport.importClause) === null || _b === void 0 ? void 0 : _b.name;
|
|
59
|
-
let name;
|
|
60
|
-
if (!newName || !oldName) {
|
|
61
|
-
name = newName !== null && newName !== void 0 ? newName : oldName;
|
|
62
|
-
}
|
|
63
|
-
else if ((newName === null || newName === void 0 ? void 0 : newName.text) === (oldName === null || oldName === void 0 ? void 0 : oldName.text)) {
|
|
64
|
-
name = newName;
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
let newBindings = (_c = newImport.importClause) === null || _c === void 0 ? void 0 : _c.namedBindings;
|
|
70
|
-
let oldBindings = (_d = oldImport.importClause) === null || _d === void 0 ? void 0 : _d.namedBindings;
|
|
71
|
-
let bindings;
|
|
72
|
-
if (!oldBindings || !newBindings) {
|
|
73
|
-
bindings = newBindings !== null && newBindings !== void 0 ? newBindings : oldBindings;
|
|
74
|
-
}
|
|
75
|
-
else if (ts.isNamedImports(oldBindings) && ts.isNamedImports(newBindings)) {
|
|
76
|
-
bindings = ts.createNamedImports([
|
|
77
|
-
...((_e = oldBindings === null || oldBindings === void 0 ? void 0 : oldBindings.elements) !== null && _e !== void 0 ? _e : []),
|
|
78
|
-
...((_f = newBindings === null || newBindings === void 0 ? void 0 : newBindings.elements) !== null && _f !== void 0 ? _f : []),
|
|
79
|
-
]);
|
|
80
|
-
}
|
|
81
|
-
else if (ts.isNamespaceImport(oldBindings) && ts.isNamespaceImport(newBindings)) {
|
|
82
|
-
if ((oldBindings === null || oldBindings === void 0 ? void 0 : oldBindings.name.text) !== (newBindings === null || newBindings === void 0 ? void 0 : newBindings.name.text)) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
bindings = oldBindings;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
let combinedImport = ts.createImportDeclaration(oldImport.decorators, oldImport.modifiers, ts.createImportClause(name, bindings, ((_g = oldImport.importClause) === null || _g === void 0 ? void 0 : _g.isTypeOnly) && ((_h = newImport.importClause) === null || _h === void 0 ? void 0 : _h.isTypeOnly)), oldImport.moduleSpecifier);
|
|
93
|
-
let result = ts.createPrinter().printNode(ts.EmitHint.Unspecified, combinedImport, sourceFile);
|
|
94
|
-
if (/'/.test(oldImportText)) {
|
|
95
|
-
result = result.replace(/"/g, `'`);
|
|
96
|
-
}
|
|
97
|
-
return result;
|
|
98
|
-
}
|
|
99
|
-
function parseImportsTogether(ts, oldImportText, newImportText) {
|
|
100
|
-
let sourceFile = ts.createSourceFile('tmp.ts', `${oldImportText}\n${newImportText}`, ts.ScriptTarget.Latest);
|
|
101
|
-
let [oldImport, newImport] = sourceFile.statements;
|
|
102
|
-
if (ts.isImportDeclaration(oldImport) && ts.isImportDeclaration(newImport)) {
|
|
103
|
-
return { oldImport, newImport, sourceFile };
|
|
104
|
-
}
|
|
105
|
-
}
|
package/lib/util/logging.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export declare function loggerFor(info: ts.server.PluginCreateInfo): Logger;
|
|
2
|
-
export interface Logger {
|
|
3
|
-
readonly enabled: boolean;
|
|
4
|
-
log(...values: unknown[]): void;
|
|
5
|
-
}
|
|
6
|
-
export declare class FileLogger implements Logger {
|
|
7
|
-
private readonly logFile;
|
|
8
|
-
readonly enabled = true;
|
|
9
|
-
constructor(logFile: string);
|
|
10
|
-
log(...values: unknown[]): void;
|
|
11
|
-
}
|
package/lib/util/logging.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.FileLogger = exports.loggerFor = void 0;
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
|
-
const fs_1 = __importDefault(require("fs"));
|
|
9
|
-
const LOGGERS = new WeakMap();
|
|
10
|
-
function loggerFor(info) {
|
|
11
|
-
let logger = LOGGERS.get(info);
|
|
12
|
-
if (!logger) {
|
|
13
|
-
let logFile = resolveLogFile(info);
|
|
14
|
-
if (logFile) {
|
|
15
|
-
fs_1.default.mkdirSync(path_1.default.dirname(logFile), { recursive: true });
|
|
16
|
-
logger = new FileLogger(logFile);
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
logger = NOOP_LOGGER;
|
|
20
|
-
}
|
|
21
|
-
LOGGERS.set(info, logger);
|
|
22
|
-
}
|
|
23
|
-
return logger;
|
|
24
|
-
}
|
|
25
|
-
exports.loggerFor = loggerFor;
|
|
26
|
-
class FileLogger {
|
|
27
|
-
constructor(logFile) {
|
|
28
|
-
this.logFile = logFile;
|
|
29
|
-
this.enabled = true;
|
|
30
|
-
}
|
|
31
|
-
log(...values) {
|
|
32
|
-
fs_1.default.appendFileSync(this.logFile, stringToLog(values));
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.FileLogger = FileLogger;
|
|
36
|
-
const NOOP_LOGGER = {
|
|
37
|
-
enabled: false,
|
|
38
|
-
log() {
|
|
39
|
-
// Do nothing
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
function resolveLogFile(info) {
|
|
43
|
-
let { logFile } = info.config;
|
|
44
|
-
if (typeof logFile === 'string') {
|
|
45
|
-
return path_1.default.resolve(path_1.default.dirname(info.project.projectName), logFile);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
function stringToLog(values) {
|
|
49
|
-
return values.map(serialize).join(' ') + '\n';
|
|
50
|
-
}
|
|
51
|
-
function serialize(value) {
|
|
52
|
-
return typeof value === 'string' ? value : JSON.stringify(value, null, 2);
|
|
53
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
declare const TRANSFORMED: unique symbol;
|
|
2
|
-
declare const TRANSFORMABLE: unique symbol;
|
|
3
|
-
export declare type TransformedPath = string & {
|
|
4
|
-
[TRANSFORMED]: true;
|
|
5
|
-
};
|
|
6
|
-
export declare type TransformablePath = string & {
|
|
7
|
-
[TRANSFORMABLE]: true;
|
|
8
|
-
};
|
|
9
|
-
export declare function getTransformedPath(path: TransformablePath): TransformedPath;
|
|
10
|
-
export declare function getOriginalPath(path: TransformedPath): TransformablePath;
|
|
11
|
-
export declare function isTransformablePath(path: string): path is TransformablePath;
|
|
12
|
-
export declare function isTransformedPath(path: string): path is TransformedPath;
|
|
13
|
-
export declare function isExtensionlessTransformedPath(path: string): boolean;
|
|
14
|
-
export declare function getExtensionlessOriginalPath(path: string): string;
|
|
15
|
-
export {};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getExtensionlessOriginalPath = exports.isExtensionlessTransformedPath = exports.isTransformedPath = exports.isTransformablePath = exports.getOriginalPath = exports.getTransformedPath = void 0;
|
|
4
|
-
const EXTENSIONLESS_TRANSFORMED_SUFFIX = '❈';
|
|
5
|
-
const TRANSFORMED_SUFFIX = `${EXTENSIONLESS_TRANSFORMED_SUFFIX}.ts`;
|
|
6
|
-
function getTransformedPath(path) {
|
|
7
|
-
return path.replace('.ts', TRANSFORMED_SUFFIX);
|
|
8
|
-
}
|
|
9
|
-
exports.getTransformedPath = getTransformedPath;
|
|
10
|
-
function getOriginalPath(path) {
|
|
11
|
-
return path.replace(TRANSFORMED_SUFFIX, '.ts');
|
|
12
|
-
}
|
|
13
|
-
exports.getOriginalPath = getOriginalPath;
|
|
14
|
-
function isTransformablePath(path) {
|
|
15
|
-
return path.endsWith('.ts') && !path.endsWith('.d.ts') && !path.endsWith(TRANSFORMED_SUFFIX);
|
|
16
|
-
}
|
|
17
|
-
exports.isTransformablePath = isTransformablePath;
|
|
18
|
-
function isTransformedPath(path) {
|
|
19
|
-
return path.endsWith(TRANSFORMED_SUFFIX);
|
|
20
|
-
}
|
|
21
|
-
exports.isTransformedPath = isTransformedPath;
|
|
22
|
-
// When dealing with modules, paths often don't include the extension...
|
|
23
|
-
function isExtensionlessTransformedPath(path) {
|
|
24
|
-
return path.endsWith(EXTENSIONLESS_TRANSFORMED_SUFFIX);
|
|
25
|
-
}
|
|
26
|
-
exports.isExtensionlessTransformedPath = isExtensionlessTransformedPath;
|
|
27
|
-
function getExtensionlessOriginalPath(path) {
|
|
28
|
-
return path.replace(EXTENSIONLESS_TRANSFORMED_SUFFIX, '');
|
|
29
|
-
}
|
|
30
|
-
exports.getExtensionlessOriginalPath = getExtensionlessOriginalPath;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type ts from 'typescript/lib/tsserverlibrary';
|
|
2
|
-
import { TransformedModule } from '@glint/transform';
|
|
3
|
-
import { TransformablePath } from './util/path-transformation';
|
|
4
|
-
import { GlintConfig } from '@glint/config';
|
|
5
|
-
export default class VirtualModuleManager {
|
|
6
|
-
private configuredProjects;
|
|
7
|
-
private transformedModules;
|
|
8
|
-
private sysReadFile;
|
|
9
|
-
constructor(ts: typeof import('typescript/lib/tsserverlibrary'));
|
|
10
|
-
addProject(config: GlintConfig, project: ts.server.Project): void;
|
|
11
|
-
isTransformationCandidate(path: string): boolean;
|
|
12
|
-
getTransformedModule(path: TransformablePath): TransformedModule | undefined;
|
|
13
|
-
readFile(path: string, encoding?: string): string | undefined;
|
|
14
|
-
private findConfiguredProject;
|
|
15
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const transform_1 = require("@glint/transform");
|
|
4
|
-
const path_1 = require("path");
|
|
5
|
-
const path_transformation_1 = require("./util/path-transformation");
|
|
6
|
-
class VirtualModuleManager {
|
|
7
|
-
constructor(ts) {
|
|
8
|
-
this.configuredProjects = [];
|
|
9
|
-
this.transformedModules = new Map();
|
|
10
|
-
this.sysReadFile = ts.sys.readFile;
|
|
11
|
-
}
|
|
12
|
-
addProject(config, project) {
|
|
13
|
-
this.configuredProjects.push({ project, config });
|
|
14
|
-
}
|
|
15
|
-
isTransformationCandidate(path) {
|
|
16
|
-
return Boolean(this.findConfiguredProject(path));
|
|
17
|
-
}
|
|
18
|
-
getTransformedModule(path) {
|
|
19
|
-
var _a;
|
|
20
|
-
let scriptInfo = (_a = this.findConfiguredProject(path)) === null || _a === void 0 ? void 0 : _a.project.getScriptInfo(path);
|
|
21
|
-
return scriptInfo && this.transformedModules.get(scriptInfo);
|
|
22
|
-
}
|
|
23
|
-
readFile(path, encoding) {
|
|
24
|
-
var _a, _b;
|
|
25
|
-
if (!path_transformation_1.isTransformedPath(path)) {
|
|
26
|
-
return this.sysReadFile(path, encoding);
|
|
27
|
-
}
|
|
28
|
-
let configuredProject = this.findConfiguredProject(path);
|
|
29
|
-
if (!configuredProject) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
let originalPath = path_transformation_1.getOriginalPath(path);
|
|
33
|
-
let originalScriptInfo = configuredProject.project.getScriptInfo(originalPath);
|
|
34
|
-
let glintEnvironment = configuredProject.config.environment;
|
|
35
|
-
let snapshot = originalScriptInfo === null || originalScriptInfo === void 0 ? void 0 : originalScriptInfo.getSnapshot();
|
|
36
|
-
let length = (_a = snapshot === null || snapshot === void 0 ? void 0 : snapshot.getLength()) !== null && _a !== void 0 ? _a : 0;
|
|
37
|
-
let content = (_b = snapshot === null || snapshot === void 0 ? void 0 : snapshot.getText(0, length)) !== null && _b !== void 0 ? _b : this.sysReadFile(originalPath, encoding);
|
|
38
|
-
if (!content || !glintEnvironment.moduleMayHaveTagImports(content)) {
|
|
39
|
-
return content;
|
|
40
|
-
}
|
|
41
|
-
let transformedModule = transform_1.rewriteModule(originalPath, content, glintEnvironment);
|
|
42
|
-
if (transformedModule && originalScriptInfo) {
|
|
43
|
-
this.transformedModules.set(originalScriptInfo, transformedModule);
|
|
44
|
-
return transformedModule.transformedSource;
|
|
45
|
-
}
|
|
46
|
-
if (originalScriptInfo) {
|
|
47
|
-
this.transformedModules.delete(originalScriptInfo);
|
|
48
|
-
}
|
|
49
|
-
let originalModuleName = path_1.basename(originalPath, '.ts');
|
|
50
|
-
let exports = [`export * from './${originalModuleName}';`];
|
|
51
|
-
// This is far from perfect detection, but it's a reasonable approximation
|
|
52
|
-
// and the consequences of a false positive are minimal. This avoids needing
|
|
53
|
-
// to parse the entire module to discover whether there's a default export.
|
|
54
|
-
if (/export(\s+|\s*\{\s*)default/.test(content)) {
|
|
55
|
-
exports.push(`export { default } from './${originalModuleName}';`);
|
|
56
|
-
}
|
|
57
|
-
return exports.join('\n');
|
|
58
|
-
}
|
|
59
|
-
findConfiguredProject(fileName) {
|
|
60
|
-
for (let project of this.configuredProjects) {
|
|
61
|
-
if (project.config.includesFile(fileName)) {
|
|
62
|
-
return project;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
exports.default = VirtualModuleManager;
|