Skip to content

Commit ae9f83d

Browse files
committed
WIP
1 parent 6c684f9 commit ae9f83d

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
@@ -3122,7 +3122,9 @@ namespace ts {
31223122
NodeJs = 2
31233123
}
31243124

3125-
export type PluginImport = { name: string };
3125+
export interface PluginImport {
3126+
name: string
3127+
}
31263128

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

@@ -3179,7 +3181,7 @@ namespace ts {
31793181
outDir?: string;
31803182
outFile?: string;
31813183
paths?: MapLike<string[]>;
3182-
plugins?: PluginImport[];
3184+
/*@internal*/ plugins?: PluginImport[];
31833185
preserveConstEnums?: boolean;
31843186
project?: string;
31853187
/* @internal */ pretty?: DiagnosticStyle;
@@ -3307,7 +3309,7 @@ namespace ts {
33073309
/* @internal */
33083310
export interface CommandLineOptionBase {
33093311
name: string;
3310-
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
3312+
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
33113313
isFilePath?: boolean; // True if option value is a path or fileName
33123314
shortName?: string; // A short mnemonic for convenience - for instance, 'h' can be used in place of 'help'
33133315
description?: DiagnosticMessage; // The message describing what the command line switch does
@@ -3328,7 +3330,7 @@ namespace ts {
33283330

33293331
/* @internal */
33303332
export interface TsConfigOnlyOption extends CommandLineOptionBase {
3331-
type: "object";
3333+
type: "object" | "json";
33323334
}
33333335

33343336
/* @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
@@ -228,6 +228,34 @@ namespace ts.server {
228228
return this.cachedUnresolvedImportsPerFile;
229229
}
230230

231+
public static resolveModule(moduleName: string, initialDir: string, host: ServerHost, log: (message: string) => void): {} {
232+
log(`Resolving module ${moduleName}`);
233+
let searchPath = initialDir;
234+
log(`Root plugin search path is ${searchPath}`);
235+
let loaded = false;
236+
// Walk up probing node_modules paths
237+
while (!loaded && getBaseFileName(searchPath) !== '') {
238+
const probePath = combinePaths(combinePaths(searchPath, 'node_modules'), moduleName);
239+
if (host.directoryExists(probePath)) {
240+
log(`Loading ${moduleName} from ${probePath}`);
241+
try {
242+
const pluginModule = require(probePath);
243+
return pluginModule;
244+
} catch (e) {
245+
log(`Require failed: ${e}`);
246+
}
247+
loaded = true;
248+
}
249+
else {
250+
searchPath = normalizePath(combinePaths(searchPath, '..'));
251+
log(`Walking up to probe ${searchPath}`);
252+
}
253+
}
254+
255+
log(`Failed to load ${moduleName}`);
256+
return undefined;
257+
}
258+
231259
constructor(
232260
private readonly projectName: string,
233261
readonly projectKind: ProjectKind,
@@ -844,6 +872,14 @@ namespace ts.server {
844872
enablePlugins() {
845873
const host = this.projectService.host;
846874
const options = this.getCompilerOptions();
875+
const log = (message: string) => {
876+
console.log(message);
877+
this.projectService.logger.info(message);
878+
};
879+
880+
console.log('Enable plugins');
881+
console.log(JSON.stringify(options));
882+
847883
if (!(options.plugins && options.plugins.length)) {
848884
// No plugins
849885
return;
@@ -854,36 +890,16 @@ namespace ts.server {
854890
return;
855891
}
856892

857-
for (const plugin of options.plugins) {
858-
this.projectService.logger.info(`Load plugin ${plugin.name}`);
859-
let searchPath = combinePaths(this.configFileName, '../node_modules');
860-
this.projectService.logger.info(`Root plugin search path is ${searchPath}`);
861-
let loaded = false;
862-
// Walk up probing node_modules paths
863-
while (!loaded && getBaseFileName(searchPath) !== '') {
864-
const probePath = combinePaths(searchPath, plugin.name);
865-
if (host.directoryExists(probePath)) {
866-
this.projectService.logger.info(`Loading plugin ${plugin.name} from ${probePath}`);
867-
try {
868-
const pluginModule = require(probePath);
869-
this.enableProxy(pluginModule, plugin);
870-
} catch (e) {
871-
this.projectService.logger.info(`Require failed: ${e}`);
872-
}
873-
loaded = true;
874-
}
875-
else {
876-
searchPath = normalizePath(combinePaths(searchPath, '..'));
877-
}
878-
}
879-
880-
if (!loaded) {
881-
this.projectService.logger.info(`Failed to load ${plugin.name}`);
893+
for (const pluginConfigEntry of options.plugins) {
894+
const searchPath = combinePaths(this.configFileName, '../node_modules');
895+
const resolvedModule = Project.resolveModule(pluginConfigEntry.name, searchPath, host, log);
896+
if (resolvedModule) {
897+
this.enableProxy(resolvedModule, pluginConfigEntry);
882898
}
883899
}
884900
}
885901

886-
enableProxy(pluginModule: any, configEntry: PluginImport) {
902+
private enableProxy(pluginModule: any, configEntry: PluginImport) {
887903
this.languageService = pluginModule.create(this, this.languageService, configEntry);
888904
}
889905

src/server/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,7 @@ namespace ts.server.protocol {
21942194
outDir?: string;
21952195
outFile?: string;
21962196
paths?: MapLike<string[]>;
2197+
plugins?: PluginImport[];
21972198
preserveConstEnums?: boolean;
21982199
project?: string;
21992200
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)