diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 06d20d60acf..756b2a12028 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,203 +1,203 @@ -# Instructions for Logging Issues - -## 1. Read the FAQ - -Please [read the FAQ](https://github.com/Microsoft/TypeScript/wiki/FAQ) before logging new issues, even if you think you have found a bug. - -Issues that ask questions answered in the FAQ will be closed without elaboration. - -## 2. Search for Duplicates - -[Search the existing issues](https://github.com/Microsoft/TypeScript/search?type=Issues) before logging a new one. - -Some search tips: - * *Don't* restrict your search to only open issues. An issue with a title similar to yours may have been closed as a duplicate of one with a less-findable title. - * Check for synonyms. For example, if your bug involves an interface, it likely also occurs with type aliases or classes. - * Search for the title of the issue you're about to log. This sounds obvious but 80% of the time this is sufficient to find a duplicate when one exists. - * Read more than the first page of results. Many bugs here use the same words so relevancy sorting is not particularly strong. - * If you have a crash, search for the first few topmost function names shown in the call stack. - -## 3. Do you have a question? - -The issue tracker is for **issues**, in other words, bugs and suggestions. -If you have a *question*, please use [Stack Overflow](http://stackoverflow.com/questions/tagged/typescript), [Gitter](https://gitter.im/Microsoft/TypeScript), your favorite search engine, or other resources. -Due to increased traffic, we can no longer answer questions in the issue tracker. - -## 4. Did you find a bug? - -When logging a bug, please be sure to include the following: - * What version of TypeScript you're using (run `tsc --v`) - * If at all possible, an *isolated* way to reproduce the behavior - * The behavior you expect to see, and the actual behavior - -You can try out the nightly build of TypeScript (`npm install typescript@next`) to see if the bug has already been fixed. - -## 5. Do you have a suggestion? - -We also accept suggestions in the issue tracker. -Be sure to [check the FAQ](https://github.com/Microsoft/TypeScript/wiki/FAQ) and [search](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93&q=is%3Aissue) first. - -In general, things we find useful when reviewing suggestions are: -* A description of the problem you're trying to solve -* An overview of the suggested solution -* Examples of how the suggestion would work in various places - * Code examples showing e.g. "this would be an error, this wouldn't" - * Code examples showing the generated JavaScript (if applicable) -* If relevant, precedent in other languages can be useful for establishing context and expected behavior - -# Instructions for Contributing Code - -## Tips - -### Faster clones - -The TypeScript repository is relatively large. To save some time, you might want to clone it without the repo's full history using `git clone --depth=1`. - -### Using local builds - -Run `gulp` to build a version of the compiler/language service that reflects changes you've made. You can then run `node /built/local/tsc.js` in place of `tsc` in your project. For example, to run `tsc --watch` from within the root of the repository on a file called `test.ts`, you can run `node ./built/local/tsc.js --watch test.ts`. - -## Contributing bug fixes - -TypeScript is currently accepting contributions in the form of bug fixes. A bug must have an issue tracking it in the issue tracker that has been approved (labelled ["help wanted"](https://github.com/Microsoft/TypeScript/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22) or in the "Backlog milestone") by the TypeScript team. Your pull request should include a link to the bug that you are fixing. If you've submitted a PR for a bug, please post a comment in the bug to avoid duplication of effort. - -## Contributing features - -Features (things that add new or improved functionality to TypeScript) may be accepted, but will need to first be approved (labelled ["help wanted"](https://github.com/Microsoft/TypeScript/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22) or in the "Backlog" milestone) by a TypeScript project maintainer) in the suggestion issue. Features with language design impact, or that are adequately satisfied with external tools, will not be accepted. - -Design changes will not be accepted at this time. If you have a design change proposal, please log a suggestion issue. - -## Legal - -You will need to complete a Contributor License Agreement (CLA). Briefly, this agreement testifies that you are granting us permission to use the submitted change according to the terms of the project's license, and that the work being submitted is under appropriate copyright. Upon submitting a pull request, you will automatically be given instructions on how to sign the CLA. - -## Housekeeping - -Your pull request should: - -* Include a description of what your change intends to do -* Be a child commit of a reasonably recent commit in the **master** branch - * Requests need not be a single commit, but should be a linear sequence of commits (i.e. no merge commits in your PR) -* It is desirable, but not necessary, for the tests to pass at each commit -* Have clear commit messages - * e.g. "Minor refactor in goToTypeDefinition", "Fix iterated type in for-await-of", "Add test for preserveWatchOutput on command line" -* Include adequate tests - * At least one test should fail in the absence of your non-test code changes. If your PR does not match this criteria, please specify why - * Tests should include reasonable permutations of the target fix/change - * Include baseline changes with your change - * All changed code must have 100% code coverage -* Follow the code conventions described in [Coding guidelines](https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines) -* To avoid line ending issues, set `autocrlf = input` and `whitespace = cr-at-eol` in your git configuration - -## Contributing `lib.d.ts` fixes - -There are three relevant locations to be aware of when it comes to TypeScript's library declaration files: - -* `src/lib`: the location of the sources themselves. -* `lib`: the location of the last-known-good (LKG) versions of the files which are updated periodically. -* `built/local`: the build output location, including where `src/lib` files will be copied to. - -Any changes should be made to [src/lib](https://github.com/Microsoft/TypeScript/tree/master/src/lib). **Most** of these files can be updated by hand, with the exception of any generated files (see below). - -Library files in `built/local/` are updated automatically by running the standard build task: - -```sh -gulp -``` - -The files in `lib/` are used to bootstrap compilation and usually **should not** be updated unless publishing a new version or updating the LKG. - -### Modifying generated library files - -The files `src/lib/dom.generated.d.ts` and `src/lib/webworker.generated.d.ts` both represent type declarations for the DOM and are auto-generated. To make any modifications to them, you will have to direct changes to https://github.com/Microsoft/TSJS-lib-generator - -## Running the Tests - -To run all tests, invoke the `runtests-parallel` target using gulp: - -```Shell -gulp runtests-parallel -``` - -This will run all tests; to run only a specific subset of tests, use: - -```Shell -gulp runtests --tests= -``` - -e.g. to run all compiler baseline tests: - -```Shell -gulp runtests --tests=compiler -``` - -or to run a specific test: `tests\cases\compiler\2dArrays.ts` - -```Shell -gulp runtests --tests=2dArrays -``` - -## Debugging the tests - -You can debug with VS Code or Node instead with `gulp runtests --inspect=true`: - -```Shell -gulp runtests --tests=2dArrays --inspect=true -``` - -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). - -## Adding a Test - -To add a new test case, simply place a `.ts` file in `tests\cases\compiler` containing code that exemplifies the bugfix or change you are making. - -These files support metadata tags in the format `// @metaDataName: value`. -The supported names and values are the same as those supported in the compiler itself, with the addition of the `fileName` flag. -`fileName` tags delimit sections of a file to be used as separate compilation units. -They are useful for tests relating to modules. -See below for examples. - -**Note** that if you have a test corresponding to a specific spec compliance item, you can place it in `tests\cases\conformance` in an appropriately-named subfolder. -**Note** that filenames here must be distinct from all other compiler testcase names, so you may have to work a bit to find a unique name if it's something common. - -### Tests for multiple files - -When one needs to test for scenarios which require multiple files, it is useful to use the `fileName` metadata tag as such: - -```TypeScript -// @fileName: file1.ts -export function f() { -} - -// @fileName: file2.ts -import { f as g } from "file1"; - -var x = g(); -``` - -One can also write a project test, but it is slightly more involved. - -## Managing the Baselines - -Compiler testcases generate baselines that track the emitted `.js`, the errors produced by the compiler, and the type of each expression in the file. Additionally, some testcases opt in to baselining the source map output. - -When a change in the baselines is detected, the test will fail. To inspect changes vs the expected baselines, use - -```Shell -gulp diff -``` - -After verifying that the changes in the baselines are correct, run - -```Shell -gulp baseline-accept -``` - -to establish the new baselines as the desired behavior. This will change the files in `tests\baselines\reference`, which should be included as part of your commit. It's important to carefully validate changes in the baselines. - -## Localization - -All strings the user may see are stored in [`diagnosticMessages.json`](./src/compiler/diagnosticMessages.json). -If you make changes to it, run `gulp generate-diagnostics` to push them to the `Diagnostic` interface in `diagnosticInformationMap.generated.ts`. - -See [coding guidelines on diagnostic messages](https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines#diagnostic-messages). +# Instructions for Logging Issues + +## 1. Read the FAQ + +Please [read the FAQ](https://github.com/Microsoft/TypeScript/wiki/FAQ) before logging new issues, even if you think you have found a bug. + +Issues that ask questions answered in the FAQ will be closed without elaboration. + +## 2. Search for Duplicates + +[Search the existing issues](https://github.com/Microsoft/TypeScript/search?type=Issues) before logging a new one. + +Some search tips: + * *Don't* restrict your search to only open issues. An issue with a title similar to yours may have been closed as a duplicate of one with a less-findable title. + * Check for synonyms. For example, if your bug involves an interface, it likely also occurs with type aliases or classes. + * Search for the title of the issue you're about to log. This sounds obvious but 80% of the time this is sufficient to find a duplicate when one exists. + * Read more than the first page of results. Many bugs here use the same words so relevancy sorting is not particularly strong. + * If you have a crash, search for the first few topmost function names shown in the call stack. + +## 3. Do you have a question? + +The issue tracker is for **issues**, in other words, bugs and suggestions. +If you have a *question*, please use [Stack Overflow](http://stackoverflow.com/questions/tagged/typescript), [Gitter](https://gitter.im/Microsoft/TypeScript), your favorite search engine, or other resources. +Due to increased traffic, we can no longer answer questions in the issue tracker. + +## 4. Did you find a bug? + +When logging a bug, please be sure to include the following: + * What version of TypeScript you're using (run `tsc --v`) + * If at all possible, an *isolated* way to reproduce the behavior + * The behavior you expect to see, and the actual behavior + +You can try out the nightly build of TypeScript (`npm install typescript@next`) to see if the bug has already been fixed. + +## 5. Do you have a suggestion? + +We also accept suggestions in the issue tracker. +Be sure to [check the FAQ](https://github.com/Microsoft/TypeScript/wiki/FAQ) and [search](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93&q=is%3Aissue) first. + +In general, things we find useful when reviewing suggestions are: +* A description of the problem you're trying to solve +* An overview of the suggested solution +* Examples of how the suggestion would work in various places + * Code examples showing e.g. "this would be an error, this wouldn't" + * Code examples showing the generated JavaScript (if applicable) +* If relevant, precedent in other languages can be useful for establishing context and expected behavior + +# Instructions for Contributing Code + +## Tips + +### Faster clones + +The TypeScript repository is relatively large. To save some time, you might want to clone it without the repo's full history using `git clone --depth=1`. + +### Using local builds + +Run `gulp` to build a version of the compiler/language service that reflects changes you've made. You can then run `node /built/local/tsc.js` in place of `tsc` in your project. For example, to run `tsc --watch` from within the root of the repository on a file called `test.ts`, you can run `node ./built/local/tsc.js --watch test.ts`. + +## Contributing bug fixes + +TypeScript is currently accepting contributions in the form of bug fixes. A bug must have an issue tracking it in the issue tracker that has been approved (labelled ["help wanted"](https://github.com/Microsoft/TypeScript/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22) or in the "Backlog milestone") by the TypeScript team. Your pull request should include a link to the bug that you are fixing. If you've submitted a PR for a bug, please post a comment in the bug to avoid duplication of effort. + +## Contributing features + +Features (things that add new or improved functionality to TypeScript) may be accepted, but will need to first be approved (labelled ["help wanted"](https://github.com/Microsoft/TypeScript/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22) or in the "Backlog" milestone) by a TypeScript project maintainer) in the suggestion issue. Features with language design impact, or that are adequately satisfied with external tools, will not be accepted. + +Design changes will not be accepted at this time. If you have a design change proposal, please log a suggestion issue. + +## Legal + +You will need to complete a Contributor License Agreement (CLA). Briefly, this agreement testifies that you are granting us permission to use the submitted change according to the terms of the project's license, and that the work being submitted is under appropriate copyright. Upon submitting a pull request, you will automatically be given instructions on how to sign the CLA. + +## Housekeeping + +Your pull request should: + +* Include a description of what your change intends to do +* Be a child commit of a reasonably recent commit in the **master** branch + * Requests need not be a single commit, but should be a linear sequence of commits (i.e. no merge commits in your PR) +* It is desirable, but not necessary, for the tests to pass at each commit +* Have clear commit messages + * e.g. "Minor refactor in goToTypeDefinition", "Fix iterated type in for-await-of", "Add test for preserveWatchOutput on command line" +* Include adequate tests + * At least one test should fail in the absence of your non-test code changes. If your PR does not match this criteria, please specify why + * Tests should include reasonable permutations of the target fix/change + * Include baseline changes with your change + * All changed code must have 100% code coverage +* Follow the code conventions described in [Coding guidelines](https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines) +* To avoid line ending issues, set `autocrlf = input` and `whitespace = cr-at-eol` in your git configuration + +## Contributing `lib.d.ts` fixes + +There are three relevant locations to be aware of when it comes to TypeScript's library declaration files: + +* `src/lib`: the location of the sources themselves. +* `lib`: the location of the last-known-good (LKG) versions of the files which are updated periodically. +* `built/local`: the build output location, including where `src/lib` files will be copied to. + +Any changes should be made to [src/lib](https://github.com/Microsoft/TypeScript/tree/master/src/lib). **Most** of these files can be updated by hand, with the exception of any generated files (see below). + +Library files in `built/local/` are updated automatically by running the standard build task: + +```sh +gulp +``` + +The files in `lib/` are used to bootstrap compilation and usually **should not** be updated unless publishing a new version or updating the LKG. + +### Modifying generated library files + +The files `src/lib/dom.generated.d.ts` and `src/lib/webworker.generated.d.ts` both represent type declarations for the DOM and are auto-generated. To make any modifications to them, you will have to direct changes to https://github.com/Microsoft/TSJS-lib-generator + +## Running the Tests + +To run all tests, invoke the `runtests-parallel` target using gulp: + +```Shell +gulp runtests-parallel +``` + +This will run all tests; to run only a specific subset of tests, use: + +```Shell +gulp runtests --tests= +``` + +e.g. to run all compiler baseline tests: + +```Shell +gulp runtests --tests=compiler +``` + +or to run a specific test: `tests\cases\compiler\2dArrays.ts` + +```Shell +gulp runtests --tests=2dArrays +``` + +## Debugging the tests + +You can debug with VS Code or Node instead with `gulp runtests --inspect`: + +```Shell +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). + +## Adding a Test + +To add a new test case, simply place a `.ts` file in `tests\cases\compiler` containing code that exemplifies the bugfix or change you are making. + +These files support metadata tags in the format `// @metaDataName: value`. +The supported names and values are the same as those supported in the compiler itself, with the addition of the `fileName` flag. +`fileName` tags delimit sections of a file to be used as separate compilation units. +They are useful for tests relating to modules. +See below for examples. + +**Note** that if you have a test corresponding to a specific spec compliance item, you can place it in `tests\cases\conformance` in an appropriately-named subfolder. +**Note** that filenames here must be distinct from all other compiler testcase names, so you may have to work a bit to find a unique name if it's something common. + +### Tests for multiple files + +When one needs to test for scenarios which require multiple files, it is useful to use the `fileName` metadata tag as such: + +```TypeScript +// @fileName: file1.ts +export function f() { +} + +// @fileName: file2.ts +import { f as g } from "file1"; + +var x = g(); +``` + +One can also write a project test, but it is slightly more involved. + +## Managing the Baselines + +Compiler testcases generate baselines that track the emitted `.js`, the errors produced by the compiler, and the type of each expression in the file. Additionally, some testcases opt in to baselining the source map output. + +When a change in the baselines is detected, the test will fail. To inspect changes vs the expected baselines, use + +```Shell +gulp diff +``` + +After verifying that the changes in the baselines are correct, run + +```Shell +gulp baseline-accept +``` + +to establish the new baselines as the desired behavior. This will change the files in `tests\baselines\reference`, which should be included as part of your commit. It's important to carefully validate changes in the baselines. + +## Localization + +All strings the user may see are stored in [`diagnosticMessages.json`](./src/compiler/diagnosticMessages.json). +If you make changes to it, run `gulp generate-diagnostics` to push them to the `Diagnostic` interface in `diagnosticInformationMap.generated.ts`. + +See [coding guidelines on diagnostic messages](https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines#diagnostic-messages). diff --git a/src/harness/fourslashImpl.ts b/src/harness/fourslashImpl.ts index 5436413701d..dd4135ca40c 100644 --- a/src/harness/fourslashImpl.ts +++ b/src/harness/fourslashImpl.ts @@ -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)); } diff --git a/src/harness/fourslashInterfaceImpl.ts b/src/harness/fourslashInterfaceImpl.ts index ca7ac8f58ff..f4905c00b84 100644 --- a/src/harness/fourslashInterfaceImpl.ts +++ b/src/harness/fourslashInterfaceImpl.ts @@ -560,6 +560,10 @@ namespace FourSlashInterface { public noMoveToNewFile(): void { this.state.noMoveToNewFile(); } + + public organizeImports(newContent: string) { + this.state.verifyOrganizeImports(newContent); + } } export class Edit { diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 31aff6b210a..951ad5e5bba 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -3,6 +3,7 @@ namespace ts.formatting { export interface FormatContext { readonly options: FormatCodeSettings; readonly getRules: RulesMap; + readonly host: FormattingHost; } export interface TextRangeWithKind 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; diff --git a/src/services/formatting/rulesMap.ts b/src/services/formatting/rulesMap.ts index f466b397d20..ccee491040f 100644 --- a/src/services/formatting/rulesMap.ts +++ b/src/services/formatting/rulesMap.ts @@ -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; diff --git a/src/services/services.ts b/src/services/services.ts index f50d99e55d1..6668ee14f37 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -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(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; @@ -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, }; diff --git a/src/services/types.ts b/src/services/types.ts index 13c68364664..fd8ade8f8e6 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -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. // diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 617855c584a..b1799167c43 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -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; } diff --git a/src/testRunner/unittests/services/convertToAsyncFunction.ts b/src/testRunner/unittests/services/convertToAsyncFunction.ts index 999e1580a98..78a2f43f6c3 100644 --- a/src/testRunner/unittests/services/convertToAsyncFunction.ts +++ b/src/testRunner/unittests/services/convertToAsyncFunction.ts @@ -306,7 +306,7 @@ interface Array {}` cancellationToken: { throwIfCancellationRequested: noop, isCancellationRequested: returnFalse }, preferences: emptyOptions, host: notImplementedHost, - formatContext: formatting.getFormatContext(testFormatSettings) + formatContext: formatting.getFormatContext(testFormatSettings, notImplementedHost) }; const diagnostics = languageService.getSuggestionDiagnostics(f.path); diff --git a/src/testRunner/unittests/services/extract/helpers.ts b/src/testRunner/unittests/services/extract/helpers.ts index a998bf6f510..a4787529994 100644 --- a/src/testRunner/unittests/services/extract/helpers.ts +++ b/src/testRunner/unittests/services/extract/helpers.ts @@ -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)); diff --git a/src/testRunner/unittests/services/textChanges.ts b/src/testRunner/unittests/services/textChanges.ts index 2d76b64696d..5c3a103c238 100644 --- a/src/testRunner/unittests/services/textChanges.ts +++ b/src/testRunner/unittests/services/textChanges.ts @@ -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. diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index d9b47adc1fb..d7d4935118d 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -394,6 +394,8 @@ declare namespace FourSlashInterface { noMoveToNewFile(): void; generateTypes(...options: GenerateTypesOptions[]): void; + + organizeImports(newContent: string): void; } class edit { backspace(count?: number): void; diff --git a/tests/cases/fourslash/organizeImportsNoFormatOptions.ts b/tests/cases/fourslash/organizeImportsNoFormatOptions.ts new file mode 100644 index 00000000000..24626441222 --- /dev/null +++ b/tests/cases/fourslash/organizeImportsNoFormatOptions.ts @@ -0,0 +1,22 @@ +/// + +// #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; +}`);