function resolveSettings()

in server/src/eslintServer.ts [1073:1250]


function resolveSettings(document: TextDocument): Promise<TextDocumentSettings> {
	const uri = document.uri;
	let resultPromise = document2Settings.get(uri);
	if (resultPromise) {
		return resultPromise;
	}
	resultPromise = connection.workspace.getConfiguration({ scopeUri: uri, section: '' }).then((configuration: ConfigurationSettings) => {
		const settings: TextDocumentSettings = Object.assign(
			{},
			configuration,
			{ silent: false, library: undefined, resolvedGlobalPackageManagerPath: undefined },
			{ workingDirectory: undefined}
		);
		if (settings.validate === Validate.off) {
			return settings;
		}
		settings.resolvedGlobalPackageManagerPath = globalPathGet(settings.packageManager);
		const filePath = getFilePath(document);
		const workspaceFolderPath = settings.workspaceFolder !== undefined ? getFilePath(settings.workspaceFolder.uri) : undefined;
		const hasUserDefinedWorkingDirectories: boolean = configuration.workingDirectory !== undefined;
		const workingDirectoryConfig = configuration.workingDirectory ?? { mode: ModeEnum.location };
		if (ModeItem.is(workingDirectoryConfig)) {
			let candidate: string | undefined;
			if (workingDirectoryConfig.mode === ModeEnum.location) {
				if (workspaceFolderPath !== undefined) {
					candidate = workspaceFolderPath;
				} else if (filePath !== undefined && !isUNC(filePath)) {
					candidate = path.dirname(filePath);
				}
			} else if (workingDirectoryConfig.mode === ModeEnum.auto) {
				if (workspaceFolderPath !== undefined) {
					candidate = findWorkingDirectory(workspaceFolderPath, filePath);
				} else if (filePath !== undefined && !isUNC(filePath)) {
					candidate = path.dirname(filePath);
				}
			}
			if (candidate !== undefined && fs.existsSync(candidate)) {
				settings.workingDirectory = { directory: candidate };
			}
		} else {
			settings.workingDirectory = workingDirectoryConfig;
		}
		let promise: Promise<string>;
		let nodePath: string | undefined;
		if (settings.nodePath !== null) {
			nodePath = settings.nodePath;
			if (!path.isAbsolute(nodePath) && workspaceFolderPath !== undefined) {
				nodePath = path.join(workspaceFolderPath, nodePath);
			}
		}
		let moduleResolveWorkingDirectory: string | undefined;
		if (!hasUserDefinedWorkingDirectories && filePath !== undefined) {
			moduleResolveWorkingDirectory = path.dirname(filePath);
		}
		if (moduleResolveWorkingDirectory === undefined && settings.workingDirectory !== undefined && !settings.workingDirectory['!cwd']) {
			moduleResolveWorkingDirectory = settings.workingDirectory.directory;
		}
		if (nodePath !== undefined) {
			promise = Files.resolve('eslint', nodePath, nodePath, trace).then<string, string>(undefined, () => {
				return Files.resolve('eslint', settings.resolvedGlobalPackageManagerPath, moduleResolveWorkingDirectory, trace);
			});
		} else {
			promise = Files.resolve('eslint', settings.resolvedGlobalPackageManagerPath, moduleResolveWorkingDirectory, trace);
		}

		settings.silent = settings.validate === Validate.probe;
		return promise.then(async (libraryPath) => {
			let library = path2Library.get(libraryPath);
			if (library === undefined) {
				library = loadNodeModule(libraryPath);
				if (library === undefined) {
					settings.validate = Validate.off;
					if (!settings.silent) {
						connection.console.error(`Failed to load eslint library from ${libraryPath}. See output panel for more information.`);
					}
				} else if (library.CLIEngine === undefined && library.ESLint === undefined) {
					settings.validate = Validate.off;
					connection.console.error(`The eslint library loaded from ${libraryPath} doesn\'t neither exports a CLIEngine nor an ESLint class. You need at least eslint@1.0.0`);
				} else {
					connection.console.info(`ESLint library loaded from: ${libraryPath}`);
					settings.library = library;
					path2Library.set(libraryPath, library);
				}
			} else {
				settings.library = library;
			}
			if (settings.validate === Validate.probe && TextDocumentSettings.hasLibrary(settings)) {
				settings.validate = Validate.off;
				let filePath = getESLintFilePath(document, settings);
				if (filePath !== undefined) {
					const parserRegExps = languageId2ParserRegExp.get(document.languageId);
					const pluginName = languageId2PluginName.get(document.languageId);
					const parserOptions = languageId2ParserOptions.get(document.languageId);
					if (defaultLanguageIds.has(document.languageId)) {
						settings.validate = Validate.on;
					} else if (parserRegExps !== undefined || pluginName !== undefined || parserOptions !== undefined) {
						const eslintConfig: ESLintConfig | undefined = await withESLintClass((eslintClass) => {
							try {
								return eslintClass.calculateConfigForFile(filePath!);
							} catch (err) {
								return undefined;
							}
						}, settings);
						if (eslintConfig !== undefined) {
							const parser: string | undefined =  eslintConfig.parser !== null
								? normalizePath(eslintConfig.parser)
								: undefined;
							if (parser !== undefined) {
								if (parserRegExps !== undefined) {
									for (const regExp of parserRegExps) {
										if (regExp.test(parser)) {
											settings.validate = Validate.on;
											break;
										}
									}
								}
								if (settings.validate !== Validate.on && parserOptions !== undefined && typeof eslintConfig.parserOptions?.parser === 'string') {
									const eslintConfigParserOptionsParser = normalizePath(eslintConfig.parserOptions.parser);
									for (const regExp of parserOptions.regExps) {
										if (regExp.test(parser) && (
											parserOptions.parsers.has(eslintConfig.parserOptions.parser) ||
											parserOptions.parserRegExps !== undefined && parserOptions.parserRegExps.some(parserRegExp => parserRegExp.test(eslintConfigParserOptionsParser))
										)) {
											settings.validate = Validate.on;
											break;
										}
									}
								}
							}
							if (settings.validate !== Validate.on && Array.isArray(eslintConfig.plugins) && eslintConfig.plugins.length > 0 && pluginName !== undefined) {
								for (const name of eslintConfig.plugins) {
									if (name === pluginName) {
										settings.validate = Validate.on;
										break;
									}
								}
							}
						}
					}
				}
				if (settings.validate === Validate.off) {
					const params: ProbeFailedParams = { textDocument: { uri: document.uri } };
					void connection.sendRequest(ProbeFailedRequest.type, params);
				}
			}
			if (settings.format && settings.validate === Validate.on && TextDocumentSettings.hasLibrary(settings)) {
				const Uri = URI.parse(uri);
				const isFile = Uri.scheme === 'file';
				let pattern: string = isFile
					? Uri.fsPath.replace(/\\/g, '/')
					: Uri.fsPath;
				pattern = pattern.replace(/[\[\]\{\}]/g, '?');

				const filter: DocumentFilter = { scheme: Uri.scheme, pattern: pattern };
				const options: DocumentFormattingRegistrationOptions = { documentSelector: [filter] };
				if (!isFile) {
					formatterRegistrations.set(uri, connection.client.register(DocumentFormattingRequest.type, options));
				} else {
					const filePath = getFilePath(uri)!;
					await withESLintClass(async (eslintClass) => {
						if (!await eslintClass.isPathIgnored(filePath)) {
							formatterRegistrations.set(uri, connection.client.register(DocumentFormattingRequest.type, options));
						}
					}, settings);
				}
			}
			return settings;
		}, () => {
			settings.validate = Validate.off;
			if (!settings.silent) {
				void connection.sendRequest(NoESLintLibraryRequest.type, { source: { uri: document.uri } });
			}
			return settings;
		});
	});
	document2Settings.set(uri, resultPromise);
	return resultPromise;
}