Merge branch 'master' into patch-3

This commit is contained in:
Nathan Shively-Sanders 2020-05-19 13:20:25 -07:00 committed by GitHub
commit 2227ba749c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 264 additions and 224 deletions

View file

@ -139,10 +139,10 @@ gulp runtests --tests=2dArrays
## Debugging the tests
You can debug with VS Code or Node instead with `gulp runtests --inspect=true`:
You can debug with VS Code or Node instead with `gulp runtests --inspect`:
```Shell
gulp runtests --tests=2dArrays --inspect=true
gulp runtests --tests=2dArrays --inspect
```
You can also use the [provided VS Code launch configuration](./.vscode/launch.template.json) to launch a debug session for an open test file. Rename the file 'launch.json', open the test file of interest, and launch the debugger from the debug panel (or press F5).

View file

@ -515,6 +515,12 @@ namespace FourSlash {
}
}
public verifyOrganizeImports(newContent: string) {
const changes = this.languageService.organizeImports({ fileName: this.activeFile.fileName, type: "file" }, this.formatCodeSettings, ts.emptyOptions);
this.applyChanges(changes);
this.verifyFileContent(this.activeFile.fileName, newContent);
}
private raiseError(message: string): never {
throw new Error(this.messageAtLastKnownMarker(message));
}

View file

@ -560,6 +560,10 @@ namespace FourSlashInterface {
public noMoveToNewFile(): void {
this.state.noMoveToNewFile();
}
public organizeImports(newContent: string) {
this.state.verifyOrganizeImports(newContent);
}
}
export class Edit {

View file

@ -3,6 +3,7 @@ namespace ts.formatting {
export interface FormatContext {
readonly options: FormatCodeSettings;
readonly getRules: RulesMap;
readonly host: FormattingHost;
}
export interface TextRangeWithKind<T extends SyntaxKind = SyntaxKind> extends TextRange {
@ -394,7 +395,7 @@ namespace ts.formatting {
initialIndentation: number,
delta: number,
formattingScanner: FormattingScanner,
{ options, getRules }: FormatContext,
{ options, getRules, host }: FormatContext,
requestKind: FormattingRequestKind,
rangeContainsError: (r: TextRange) => boolean,
sourceFile: SourceFileLike): TextChange[] {
@ -1193,7 +1194,7 @@ namespace ts.formatting {
previousRange: TextRangeWithKind,
previousStartLine: number,
currentRange: TextRangeWithKind,
currentStartLine: number,
currentStartLine: number
): LineAction {
const onLaterLine = currentStartLine !== previousStartLine;
switch (rule.action) {
@ -1221,7 +1222,7 @@ namespace ts.formatting {
// edit should not be applied if we have one line feed between elements
const lineDelta = currentStartLine - previousStartLine;
if (lineDelta !== 1) {
recordReplace(previousRange.end, currentRange.pos - previousRange.end, options.newLineCharacter!);
recordReplace(previousRange.end, currentRange.pos - previousRange.end, getNewLineOrDefaultFromHost(host, options));
return onLaterLine ? LineAction.None : LineAction.LineAdded;
}
break;

View file

@ -1,7 +1,7 @@
/* @internal */
namespace ts.formatting {
export function getFormatContext(options: FormatCodeSettings): FormatContext {
return { options, getRules: getRulesMap() };
export function getFormatContext(options: FormatCodeSettings, host: FormattingHost): FormatContext {
return { options, getRules: getRulesMap(), host };
}
let rulesMapCache: RulesMap | undefined;

View file

@ -1502,7 +1502,7 @@ namespace ts {
position,
{ name, source },
host,
(formattingOptions && formatting.getFormatContext(formattingOptions))!, // TODO: GH#18217
(formattingOptions && formatting.getFormatContext(formattingOptions, host))!, // TODO: GH#18217
preferences,
cancellationToken,
);
@ -1840,16 +1840,16 @@ namespace ts {
function getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[] {
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
return formatting.formatSelection(start, end, sourceFile, formatting.getFormatContext(toEditorSettings(options)));
return formatting.formatSelection(start, end, sourceFile, formatting.getFormatContext(toEditorSettings(options), host));
}
function getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[] {
return formatting.formatDocument(syntaxTreeCache.getCurrentSourceFile(fileName), formatting.getFormatContext(toEditorSettings(options)));
return formatting.formatDocument(syntaxTreeCache.getCurrentSourceFile(fileName), formatting.getFormatContext(toEditorSettings(options), host));
}
function getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[] {
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
const formatContext = formatting.getFormatContext(toEditorSettings(options));
const formatContext = formatting.getFormatContext(toEditorSettings(options), host);
if (!isInComment(sourceFile, position)) {
switch (key) {
@ -1871,7 +1871,7 @@ namespace ts {
synchronizeHostData();
const sourceFile = getValidSourceFile(fileName);
const span = createTextSpanFromBounds(start, end);
const formatContext = formatting.getFormatContext(formatOptions);
const formatContext = formatting.getFormatContext(formatOptions, host);
return flatMap(deduplicate<number>(errorCodes, equateValues, compareValues), errorCode => {
cancellationToken.throwIfCancellationRequested();
@ -1883,7 +1883,7 @@ namespace ts {
synchronizeHostData();
Debug.assert(scope.type === "file");
const sourceFile = getValidSourceFile(scope.fileName);
const formatContext = formatting.getFormatContext(formatOptions);
const formatContext = formatting.getFormatContext(formatOptions, host);
return codefix.getAllFixes({ fixId, sourceFile, program, host, cancellationToken, formatContext, preferences });
}
@ -1892,13 +1892,13 @@ namespace ts {
synchronizeHostData();
Debug.assert(scope.type === "file");
const sourceFile = getValidSourceFile(scope.fileName);
const formatContext = formatting.getFormatContext(formatOptions);
const formatContext = formatting.getFormatContext(formatOptions, host);
return OrganizeImports.organizeImports(sourceFile, formatContext, host, program, preferences);
}
function getEditsForFileRename(oldFilePath: string, newFilePath: string, formatOptions: FormatCodeSettings, preferences: UserPreferences = emptyOptions): readonly FileTextChanges[] {
return ts.getEditsForFileRename(getProgram()!, oldFilePath, newFilePath, host, formatting.getFormatContext(formatOptions), preferences, sourceMapper);
return ts.getEditsForFileRename(getProgram()!, oldFilePath, newFilePath, host, formatting.getFormatContext(formatOptions, host), preferences, sourceMapper);
}
function applyCodeActionCommand(action: CodeActionCommand, formatSettings?: FormatCodeSettings): Promise<ApplyCodeActionCommandResult>;
@ -2141,7 +2141,7 @@ namespace ts {
endPosition,
program: getProgram()!,
host,
formatContext: formatting.getFormatContext(formatOptions!), // TODO: GH#18217
formatContext: formatting.getFormatContext(formatOptions!, host), // TODO: GH#18217
cancellationToken,
preferences,
};

View file

@ -203,6 +203,11 @@ namespace ts {
has(dependencyName: string, inGroups?: PackageJsonDependencyGroup): boolean;
}
/** @internal */
export interface FormattingHost {
getNewLine?(): string;
}
//
// Public interface of the host of a language service instance.
//

View file

@ -2085,9 +2085,9 @@ namespace ts {
/**
* The default is CRLF.
*/
export function getNewLineOrDefaultFromHost(host: LanguageServiceHost | LanguageServiceShimHost, formatSettings?: FormatCodeSettings) {
return (formatSettings && formatSettings.newLineCharacter) ||
(host.getNewLine && host.getNewLine()) ||
export function getNewLineOrDefaultFromHost(host: FormattingHost, formatSettings?: FormatCodeSettings) {
return formatSettings?.newLineCharacter ||
host.getNewLine?.() ||
carriageReturnLineFeed;
}

View file

@ -306,7 +306,7 @@ interface Array<T> {}`
cancellationToken: { throwIfCancellationRequested: noop, isCancellationRequested: returnFalse },
preferences: emptyOptions,
host: notImplementedHost,
formatContext: formatting.getFormatContext(testFormatSettings)
formatContext: formatting.getFormatContext(testFormatSettings, notImplementedHost)
};
const diagnostics = languageService.getSuggestionDiagnostics(f.path);

View file

@ -102,7 +102,7 @@ namespace ts {
startPosition: selectionRange.pos,
endPosition: selectionRange.end,
host: notImplementedHost,
formatContext: formatting.getFormatContext(testFormatSettings),
formatContext: formatting.getFormatContext(testFormatSettings, notImplementedHost),
preferences: emptyOptions,
};
const rangeToExtract = refactor.extractSymbol.getRangeToExtract(sourceFile, createTextSpanFromRange(selectionRange));
@ -164,7 +164,7 @@ namespace ts {
startPosition: selectionRange.pos,
endPosition: selectionRange.end,
host: notImplementedHost,
formatContext: formatting.getFormatContext(testFormatSettings),
formatContext: formatting.getFormatContext(testFormatSettings, notImplementedHost),
preferences: emptyOptions,
};
const rangeToExtract = refactor.extractSymbol.getRangeToExtract(sourceFile, createTextSpanFromRange(selectionRange));

View file

@ -19,7 +19,7 @@ namespace ts {
const newLineCharacter = getNewLineCharacter(printerOptions);
function getRuleProvider(placeOpenBraceOnNewLineForFunctions: boolean): formatting.FormatContext {
return formatting.getFormatContext(placeOpenBraceOnNewLineForFunctions ? { ...testFormatSettings, placeOpenBraceOnNewLineForFunctions: true } : testFormatSettings);
return formatting.getFormatContext(placeOpenBraceOnNewLineForFunctions ? { ...testFormatSettings, placeOpenBraceOnNewLineForFunctions: true } : testFormatSettings, notImplementedHost);
}
// validate that positions that were recovered from the printed text actually match positions that will be created if the same text is parsed.

View file

@ -394,6 +394,8 @@ declare namespace FourSlashInterface {
noMoveToNewFile(): void;
generateTypes(...options: GenerateTypesOptions[]): void;
organizeImports(newContent: string): void;
}
class edit {
backspace(count?: number): void;

View file

@ -0,0 +1,22 @@
/// <reference path="fourslash.ts" />
// #38548
////import {
//// stat,
//// statSync,
////} from "fs";
////export function fakeFn() {
//// stat;
//// statSync;
////}
format.setFormatOptions({});
// Default newline is carriage return.
// See `getNewLineOrDefaultFromHost()` in services/utilities.ts.
verify.organizeImports(
`import {\r\nstat,\r\nstatSync\r\n} from "fs";\r\nexport function fakeFn() {
stat;
statSync;
}`);