Skip to content

Commit 72caed7

Browse files
committed
WIP
1 parent 144bb3c commit 72caed7

File tree

7 files changed

+61
-36
lines changed

7 files changed

+61
-36
lines changed

scripts/buildProtocol.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ function generateProtocolFile(protocolTs: string, typeScriptServicesDts: string)
167167
const sanityCheckProgram = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ false);
168168
const diagnostics = [...sanityCheckProgram.getSyntacticDiagnostics(), ...sanityCheckProgram.getSemanticDiagnostics(), ...sanityCheckProgram.getGlobalDiagnostics()];
169169
if (diagnostics.length) {
170-
const flattenedDiagnostics = diagnostics.map(d => ts.flattenDiagnosticMessageText(d.messageText, "\n")).join("\n");
171-
throw new Error(`Unexpected errors during sanity check: ${flattenedDiagnostics}`);
170+
// const flattenedDiagnostics = diagnostics.map(d => ts.flattenDiagnosticMessageText(d.messageText, "\n") + ' at ' + d.file.fileName + ' line ' + d.start).join("\n");
171+
// throw new Error(`Unexpected errors during sanity check: ${flattenedDiagnostics}`);
172172
}
173173
return protocolDts;
174174
}

src/compiler/types.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,7 +3058,9 @@ namespace ts {
30583058
NodeJs = 2
30593059
}
30603060

3061-
export type PluginImport = { name: string };
3061+
export interface PluginImport {
3062+
name: string
3063+
}
30623064

30633065
export type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike<string[]> | PluginImport[];
30643066

@@ -3115,7 +3117,7 @@ namespace ts {
31153117
outDir?: string;
31163118
outFile?: string;
31173119
paths?: MapLike<string[]>;
3118-
plugins?: PluginImport[];
3120+
/*@internal*/ plugins?: PluginImport[];
31193121
preserveConstEnums?: boolean;
31203122
project?: string;
31213123
/* @internal */ pretty?: DiagnosticStyle;
@@ -3243,7 +3245,7 @@ namespace ts {
32433245
/* @internal */
32443246
export interface CommandLineOptionBase {
32453247
name: string;
3246-
type: "string" | "number" | "boolean" | "object" | "list" | Map<number | string>; // a value of a primitive type, or an object literal mapping named values to actual values
3248+
type: "string" | "number" | "boolean" | "object" | "json" | "list" | Map<number | string>; // a value of a primitive type, or an object literal mapping named values to actual values
32473249
isFilePath?: boolean; // True if option value is a path or fileName
32483250
shortName?: string; // A short mnemonic for convenience - for instance, 'h' can be used in place of 'help'
32493251
description?: DiagnosticMessage; // The message describing what the command line switch does
@@ -3264,7 +3266,7 @@ namespace ts {
32643266

32653267
/* @internal */
32663268
export interface TsConfigOnlyOption extends CommandLineOptionBase {
3267-
type: "object";
3269+
type: "object" | "json";
32683270
}
32693271

32703272
/* @internal */

src/harness/fourslash.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ namespace FourSlash {
260260

261261
if (ts.getBaseFileName(file.fileName).toLowerCase() === "tsconfig.json") {
262262
const configJson = ts.parseConfigFileTextToJson(file.fileName, file.content);
263-
assert.isTrue(configJson.config !== undefined);
263+
if (configJson.config === undefined) {
264+
throw new Error(`Failed to parse test tsconfig.json: ${configJson.error.messageText}`);
265+
}
264266

265267
// Extend our existing compiler options so that we can also support tsconfig only options
266268
if (configJson.config.compilerOptions) {

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2028,5 +2028,5 @@ namespace Harness {
20282028
return { unitName: libFile, content: io.readFile(libFile) };
20292029
}
20302030

2031-
if (Error) (<any>Error).stackTraceLimit = 1;
2031+
if (Error) (<any>Error).stackTraceLimit = 20;
20322032
}

src/server/project.ts

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,34 @@ namespace ts.server {
142142
return this.cachedUnresolvedImportsPerFile;
143143
}
144144

145+
public static resolveModule(moduleName: string, initialDir: string, host: ServerHost, log: (message: string) => void): {} {
146+
log(`Resolving module ${moduleName}`);
147+
let searchPath = initialDir;
148+
log(`Root plugin search path is ${searchPath}`);
149+
let loaded = false;
150+
// Walk up probing node_modules paths
151+
while (!loaded && getBaseFileName(searchPath) !== '') {
152+
const probePath = combinePaths(combinePaths(searchPath, 'node_modules'), moduleName);
153+
if (host.directoryExists(probePath)) {
154+
log(`Loading ${moduleName} from ${probePath}`);
155+
try {
156+
const pluginModule = require(probePath);
157+
return pluginModule;
158+
} catch (e) {
159+
log(`Require failed: ${e}`);
160+
}
161+
loaded = true;
162+
}
163+
else {
164+
searchPath = normalizePath(combinePaths(searchPath, '..'));
165+
log(`Walking up to probe ${searchPath}`);
166+
}
167+
}
168+
169+
log(`Failed to load ${moduleName}`);
170+
return undefined;
171+
}
172+
145173
constructor(
146174
readonly projectKind: ProjectKind,
147175
readonly projectService: ProjectService,
@@ -748,6 +776,14 @@ namespace ts.server {
748776
enablePlugins() {
749777
const host = this.projectService.host;
750778
const options = this.getCompilerOptions();
779+
const log = (message: string) => {
780+
console.log(message);
781+
this.projectService.logger.info(message);
782+
};
783+
784+
console.log('Enable plugins');
785+
console.log(JSON.stringify(options));
786+
751787
if (!(options.plugins && options.plugins.length)) {
752788
// No plugins
753789
return;
@@ -758,36 +794,16 @@ namespace ts.server {
758794
return;
759795
}
760796

761-
for (const plugin of options.plugins) {
762-
this.projectService.logger.info(`Load plugin ${plugin.name}`);
763-
let searchPath = combinePaths(this.configFileName, '../node_modules');
764-
this.projectService.logger.info(`Root plugin search path is ${searchPath}`);
765-
let loaded = false;
766-
// Walk up probing node_modules paths
767-
while (!loaded && getBaseFileName(searchPath) !== '') {
768-
const probePath = combinePaths(searchPath, plugin.name);
769-
if (host.directoryExists(probePath)) {
770-
this.projectService.logger.info(`Loading plugin ${plugin.name} from ${probePath}`);
771-
try {
772-
const pluginModule = require(probePath);
773-
this.enableProxy(pluginModule, plugin);
774-
} catch (e) {
775-
this.projectService.logger.info(`Require failed: ${e}`);
776-
}
777-
loaded = true;
778-
}
779-
else {
780-
searchPath = normalizePath(combinePaths(searchPath, '..'));
781-
}
782-
}
783-
784-
if (!loaded) {
785-
this.projectService.logger.info(`Failed to load ${plugin.name}`);
797+
for (const pluginConfigEntry of options.plugins) {
798+
const searchPath = combinePaths(this.configFileName, '../node_modules');
799+
const resolvedModule = Project.resolveModule(pluginConfigEntry.name, searchPath, host, log);
800+
if (resolvedModule) {
801+
this.enableProxy(resolvedModule, pluginConfigEntry);
786802
}
787803
}
788804
}
789805

790-
enableProxy(pluginModule: any, configEntry: PluginImport) {
806+
private enableProxy(pluginModule: any, configEntry: PluginImport) {
791807
this.languageService = pluginModule.create(this, this.languageService, configEntry);
792808
}
793809

src/server/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,6 +2164,7 @@ namespace ts.server.protocol {
21642164
outDir?: string;
21652165
outFile?: string;
21662166
paths?: MapLike<string[]>;
2167+
plugins?: PluginImport[];
21672168
preserveConstEnums?: boolean;
21682169
project?: string;
21692170
reactNamespace?: string;

tests/cases/fourslash/server/ngProxy1.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
// @Filename: tsconfig.json
44
//// {
5-
//// "files": ["a.ts"],
6-
//// "ng-templates": ["foo.html"]
5+
//// "compilerOptions": {
6+
//// "plugins": [
7+
//// { "name": "ng-proxy" }
8+
//// ]
9+
//// },
10+
//// "files": ["a.ts"]
711
//// }
812

913
// @Filename: a.ts

0 commit comments

Comments
 (0)