Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default newLine, forceConsistentCasingInFileNames #52298

Merged
merged 20 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
paramType: Diagnostics.NEWLINE,
category: Diagnostics.Emit,
description: Diagnostics.Set_the_newline_character_for_emitting_files,
defaultValueDescription: Diagnostics.Platform_specific
defaultValueDescription: "lf"
},
{
name: "noErrorTruncation",
Expand Down Expand Up @@ -1461,7 +1461,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
affectsModuleResolution: true,
category: Diagnostics.Interop_Constraints,
description: Diagnostics.Ensure_that_casing_is_correct_in_imports,
defaultValueDescription: false,
defaultValueDescription: true,
},
{
name: "maxNodeModuleJsDepth",
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
const sourceMapDataList: SourceMapEmitResult[] | undefined = (compilerOptions.sourceMap || compilerOptions.inlineSourceMap || getAreDeclarationMapsEnabled(compilerOptions)) ? [] : undefined;
const emittedFilesList: string[] | undefined = compilerOptions.listEmittedFiles ? [] : undefined;
const emitterDiagnostics = createDiagnosticCollection();
const newLine = getNewLineCharacter(compilerOptions, () => host.getNewLine());
const newLine = getNewLineCharacter(compilerOptions);
const writer = createTextWriter(newLine);
const { enter, exit } = performance.createTimer("printTime", "beforePrint", "afterPrint");
let bundleBuildInfo: BundleBuildInfo | undefined;
Expand Down Expand Up @@ -1270,7 +1270,6 @@ function emitUsingBuildInfoWorker(
getCommonSourceDirectory: () => getNormalizedAbsolutePath(buildInfo.bundle!.commonSourceDirectory, buildInfoDirectory),
getCompilerOptions: () => config.options,
getCurrentDirectory: () => host.getCurrentDirectory(),
getNewLine: () => host.getNewLine(),
getSourceFile: returnUndefined,
getSourceFileByPath: returnUndefined,
getSourceFiles: () => sourceFilesForJsEmit,
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export function createCompilerHostWorker(options: CompilerOptions, setParentNode
return getDirectoryPath(normalizePath(system.getExecutingFilePath()));
}

const newLine = getNewLineCharacter(options, () => system.newLine);
const newLine = getNewLineCharacter(options);
const realpath = system.realpath && ((path: string) => system.realpath!(path));
const compilerHost: CompilerHost = {
getSourceFile: createGetSourceFile(fileName => compilerHost.readFile(fileName), () => options, setParentNodes),
Expand Down Expand Up @@ -2482,7 +2482,6 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
getCommonSourceDirectory: program.getCommonSourceDirectory,
getCompilerOptions: program.getCompilerOptions,
getCurrentDirectory: () => currentDirectory,
getNewLine: () => host.getNewLine(),
getSourceFile: program.getSourceFile,
getSourceFileByPath: program.getSourceFileByPath,
getSourceFiles: program.getSourceFiles,
Expand Down Expand Up @@ -3450,7 +3449,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
addFileIncludeReason(file || undefined, reason);
// try to check if we've already seen this file but with a different casing in path
// NOTE: this only makes sense for case-insensitive file systems, and only on files which are not redirected
if (file && options.forceConsistentCasingInFileNames) {
if (file && options.forceConsistentCasingInFileNames !== false) {
const checkedName = file.fileName;
const isRedirect = toPath(checkedName) !== toPath(fileName);
if (isRedirect) {
Expand Down
1 change: 0 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8009,7 +8009,6 @@ export interface EmitHost extends ScriptReferenceHost, ModuleSpecifierResolution

getCommonSourceDirectory(): string;
getCanonicalFileName(fileName: string): string;
getNewLine(): string;

isEmitBlocked(emitFileName: string): boolean;

Expand Down
7 changes: 3 additions & 4 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,6 @@ import {
SymbolTable,
SyntaxKind,
SyntaxList,
sys,
TaggedTemplateExpression,
TemplateLiteral,
TemplateLiteralLikeNode,
Expand Down Expand Up @@ -6870,14 +6869,14 @@ export function directoryProbablyExists(directoryName: string, host: { directory
const carriageReturnLineFeed = "\r\n";
const lineFeed = "\n";
/** @internal */
export function getNewLineCharacter(options: CompilerOptions | PrinterOptions, getNewLine?: () => string): string {
export function getNewLineCharacter(options: CompilerOptions | PrinterOptions): string {
switch (options.newLine) {
case NewLineKind.CarriageReturnLineFeed:
return carriageReturnLineFeed;
case NewLineKind.LineFeed:
return lineFeed;
case undefined:
return lineFeed;
}
return getNewLine ? getNewLine() : sys ? sys.newLine : carriageReturnLineFeed;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,6 @@ export function createWatchFactory<Y = undefined>(host: WatchFactoryHost & { tra
/** @internal */
export function createCompilerHostFromProgramHost(host: ProgramHost<any>, getCompilerOptions: () => CompilerOptions, directoryStructureHost: DirectoryStructureHost = host): CompilerHost {
const useCaseSensitiveFileNames = host.useCaseSensitiveFileNames();
const hostGetNewLine = memoize(() => host.getNewLine());
const compilerHost: CompilerHost = {
getSourceFile: createGetSourceFile(
(fileName, encoding) => !encoding ? compilerHost.readFile(fileName) : host.readFile(fileName, encoding),
Expand All @@ -762,7 +761,7 @@ export function createCompilerHostFromProgramHost(host: ProgramHost<any>, getCom
getCurrentDirectory: memoize(() => host.getCurrentDirectory()),
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
getCanonicalFileName: createGetCanonicalFileName(useCaseSensitiveFileNames),
getNewLine: () => getNewLineCharacter(getCompilerOptions(), hostGetNewLine),
getNewLine: () => getNewLineCharacter(getCompilerOptions()),
fileExists: f => host.fileExists(f),
readFile: f => host.readFile(f),
trace: maybeBind(host, host.trace),
Expand Down Expand Up @@ -867,7 +866,7 @@ function createWatchCompilerHost<T extends BuilderProgram = EmitAndSemanticDiagn
copyProperties(result, createWatchHost(system, reportWatchStatus));
result.afterProgramCreate = builderProgram => {
const compilerOptions = builderProgram.getCompilerOptions();
const newLine = getNewLineCharacter(compilerOptions, () => system.newLine);
const newLine = getNewLineCharacter(compilerOptions);

emitFilesAndReportErrors(
builderProgram,
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/watchPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
}
reportWatchDiagnostic(Diagnostics.Starting_compilation_in_watch_mode);
if (configFileName && !host.configFileParsingResult) {
newLine = getNewLineCharacter(optionsToExtendForConfigFile, () => host.getNewLine());
newLine = getNewLineCharacter(optionsToExtendForConfigFile);
Debug.assert(!rootFileNames);
parseConfigFile();
newLine = updateNewLine();
Expand Down Expand Up @@ -668,7 +668,7 @@ export function createWatchProgram<T extends BuilderProgram>(host: WatchCompiler
}

function updateNewLine() {
return getNewLineCharacter(compilerOptions || optionsToExtendForConfigFile, () => host.getNewLine());
return getNewLineCharacter(compilerOptions || optionsToExtendForConfigFile);
}

function toPath(fileName: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/harness/fakesHosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export class CompilerHost implements ts.CompilerHost {
if (sys instanceof vfs.FileSystem) sys = new System(sys);
this.sys = sys;
this.defaultLibLocation = sys.vfs.meta.get("defaultLibLocation") || "";
this._newLine = ts.getNewLineCharacter(options, () => this.sys.newLine);
this._newLine = ts.getNewLineCharacter(options);
this._sourceFiles = new collections.SortedMap<string, ts.SourceFile>({ comparer: sys.vfs.stringComparer, sort: "insertion" });
this._setParentNodes = setParentNodes;
this._outputsMap = new collections.SortedMap(this.vfs.stringComparer);
Expand Down
2 changes: 1 addition & 1 deletion src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const enum MetadataOptionNames {
const fileMetadataNames = [MetadataOptionNames.fileName, MetadataOptionNames.emitThisFile, MetadataOptionNames.resolveReference, MetadataOptionNames.symlink];

function convertGlobalOptionsToCompilerOptions(globalOptions: Harness.TestCaseParser.CompilerSettings): ts.CompilerOptions {
const settings: ts.CompilerOptions = { target: ts.ScriptTarget.ES5 };
const settings: ts.CompilerOptions = { target: ts.ScriptTarget.ES5, newLine: ts.NewLineKind.CarriageReturnLineFeed };
Harness.Compiler.setCompilerOptionsFromHarnessSetting(globalOptions, settings);
return settings;
}
Expand Down
4 changes: 2 additions & 2 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import * as collections from "./_namespaces/collections";
import * as vpath from "./_namespaces/vpath";
import {
Compiler,
harnessNewLine,
mockHash,
virtualFileSystemRoot,
} from "./_namespaces/Harness";
import { getNewLineCharacter } from "./_namespaces/ts";

export function makeDefaultProxy(info: ts.server.PluginCreateInfo): ts.LanguageService {
const proxy = Object.create(/*prototype*/ null); // eslint-disable-line no-null/no-null
Expand Down Expand Up @@ -149,7 +149,7 @@ export abstract class LanguageServiceAdapterHost {
}

public getNewLine(): string {
return harnessNewLine;
return getNewLineCharacter(this.settings);
}

public getFilenames(): string[] {
Expand Down
10 changes: 5 additions & 5 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import {
getNameTable,
getNewLineCharacter,
getNewLineKind,
getNewLineOrDefaultFromHost,
getPropertyNameForPropertyNameNode,
getQuotePreference,
getReplacementSpanForContextToken,
Expand Down Expand Up @@ -261,7 +262,6 @@ import {
LiteralTypeNode,
map,
mapDefined,
maybeBind,
MemberOverrideStatus,
memoize,
memoizeOne,
Expand Down Expand Up @@ -1068,12 +1068,12 @@ function getExhaustiveCaseSnippets(
}

const newClauses = map(elements, element => factory.createCaseClause(element, []));
const newLineChar = getNewLineCharacter(options, maybeBind(host, host.getNewLine));
const newLineChar = getNewLineOrDefaultFromHost(host, formatContext?.options);
const printer = createSnippetPrinter({
removeComments: true,
module: options.module,
target: options.target,
newLine: getNewLineKind(newLineChar),
newLine: getNewLineKind(newLineChar)
});
const printNode = formatContext
? (node: Node) => printer.printAndFormatNode(EmitHint.Unspecified, node, sourceFile, formatContext)
Expand Down Expand Up @@ -1571,7 +1571,7 @@ function getEntryForMemberCompletion(
module: options.module,
target: options.target,
omitTrailingSemicolon: false,
newLine: getNewLineKind(getNewLineCharacter(options, maybeBind(host, host.getNewLine))),
newLine: getNewLineKind(getNewLineOrDefaultFromHost(host, formatContext?.options)),
});
const importAdder = codefix.createImportAdder(sourceFile, program, preferences, host);

Expand Down Expand Up @@ -1737,7 +1737,7 @@ function getEntryForObjectLiteralMethodCompletion(
module: options.module,
target: options.target,
omitTrailingSemicolon: false,
newLine: getNewLineKind(getNewLineCharacter(options, maybeBind(host, host.getNewLine))),
newLine: getNewLineKind(getNewLineOrDefaultFromHost(host, formatContext?.options)),
});
if (formatContext) {
insertText = printer.printAndFormatSnippetList(ListFormat.CommaDelimited | ListFormat.AllowTrailingComma, factory.createNodeArray([method], /*hasTrailingComma*/ true), sourceFile, formatContext);
Expand Down
4 changes: 2 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ export function createLanguageService(
getCancellationToken: () => cancellationToken,
getCanonicalFileName,
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
getNewLine: () => getNewLineCharacter(newSettings, () => getNewLineOrDefaultFromHost(host)),
getNewLine: () => getNewLineCharacter(newSettings),
getDefaultLibFileName: options => host.getDefaultLibFileName(options),
writeFile: noop,
getCurrentDirectory: () => currentDirectory,
Expand Down Expand Up @@ -2437,7 +2437,7 @@ export function createLanguageService(
}

function getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined {
return JsDoc.getDocCommentTemplateAtPosition(getNewLineOrDefaultFromHost(host), syntaxTreeCache.getCurrentSourceFile(fileName), position, options);
return JsDoc.getDocCommentTemplateAtPosition(getNewLineOrDefaultFromHost(host, /*formatSettings*/ undefined), syntaxTreeCache.getCurrentSourceFile(fileName), position, options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that you should fix it in this PR, but it seems like a bug that we don't have this for JSDoc comments.

}

function isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/services/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ class LanguageServiceShimObject extends ShimBase implements LanguageServiceShim
}

private realizeDiagnostics(diagnostics: readonly Diagnostic[]): { message: string; start: number; length: number; category: string; }[] {
const newLine = getNewLineOrDefaultFromHost(this.host);
const newLine = getNewLineOrDefaultFromHost(this.host, /*formatSettings*/ undefined);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one is probably okay.

return realizeDiagnostics(diagnostics, newLine);
}

Expand Down
8 changes: 4 additions & 4 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2930,16 +2930,16 @@ function findLinkNameEnd(text: string) {
return 0;
}

const carriageReturnLineFeed = "\r\n";
const lineFeed = "\n";
/**
* The default is CRLF.
* The default is LF.
*
* @internal
*/
export function getNewLineOrDefaultFromHost(host: FormattingHost, formatSettings?: FormatCodeSettings) {
export function getNewLineOrDefaultFromHost(host: FormattingHost, formatSettings: FormatCodeSettings | undefined) {
return formatSettings?.newLineCharacter ||
host.getNewLine?.() ||
carriageReturnLineFeed;
lineFeed;
}

/** @internal */
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/services/languageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function Component(x: Config): any;`
diagnostics: ts.emptyArray,
outputFiles: [{
name: "foo.d.ts",
text: "export {};\r\n",
text: "export {};\n",
writeByteOrderMark: false
}],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
"kind": "method",
"kindModifiers": "",
"sortText": "17",
"insertText": "method(): void {\r\n}",
"insertText": "method(): void {\n}",
"displayParts": [
{
"text": "(",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var X = /** @class */ (function () {
function X() {
}
X.prototype.foobar = function (x) { return x; };
return X;
}());
var X = /** @class */ (function () {
function X() {
}
X.prototype.foobar = function (x) { return x; };
return X;
}());
Loading