Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/ModuleResolverNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ export class ModuleResolver implements ModuleResolverInterface {
(await this.getPrettierInstance(fileName)) ??
(await getBundledPrettier());

// Untitled documents (unsaved files) should not use workspace configuration
// They should only use the user's global/personal VS Code settings
// Return null to indicate that VS Code settings should be used
if (doc.uri.scheme !== "file") {
this.loggingService.logInfo(
"Untitled document detected, using VS Code settings only (no workspace config)",
);
return null;
}

return this.resolveConfig(prettier, fileName, vscodeConfig);
}

Expand Down
107 changes: 107 additions & 0 deletions src/test/suite/untitled.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import * as assert from "assert";
import * as vscode from "vscode";
import { ensureExtensionActivated } from "./testUtils.js";

/**
* Tests for untitled documents (unsaved files)
*
* Untitled documents should NOT use workspace configuration.
* They should only use the user's global/personal VS Code settings.
*/
describe("Test untitled documents", () => {
before(async () => {
await ensureExtensionActivated();
});

it("formats untitled JavaScript document using VS Code settings", async () => {
// Create an untitled document with ugly JavaScript
const uglyCode = 'const x = 1 ; console.log( x ) ;';
const doc = await vscode.workspace.openTextDocument({
language: "javascript",
content: uglyCode,
});

const editor = await vscode.window.showTextDocument(doc);

// Format the document
await vscode.commands.executeCommand("editor.action.formatDocument");

// The document should be formatted using VS Code settings (not workspace config)
const formattedText = editor.document.getText();

assert.notEqual(
formattedText,
uglyCode,
"Untitled document should have been formatted using VS Code settings",
);

// Should not have excessive spacing
assert.ok(
!formattedText.includes(" "),
"Formatted code should not have excessive spacing",
);
});

it("formats untitled TypeScript document using VS Code settings", async () => {
// Create an untitled document with ugly TypeScript
const uglyCode = 'function test ( a : number ) : number { return a * 2 ; }';
const doc = await vscode.workspace.openTextDocument({
language: "typescript",
content: uglyCode,
});

const editor = await vscode.window.showTextDocument(doc);

// Format the document
await vscode.commands.executeCommand("editor.action.formatDocument");

// The document should be formatted using VS Code settings (not workspace config)
const formattedText = editor.document.getText();

assert.notEqual(
formattedText,
uglyCode,
"Untitled TypeScript document should have been formatted using VS Code settings",
);

// Should not have excessive spacing
assert.ok(
!formattedText.includes(" "),
"Formatted code should not have excessive spacing",
);
});

it("formats untitled document even when requireConfig is enabled in workspace", async () => {
// This test verifies that untitled documents bypass the requireConfig check
// Even if a workspace has prettier.requireConfig: true, untitled documents
// should still be formatted using VS Code settings

// Create an untitled document with code that needs formatting
const uglyCode = 'const foo = "bar" ;';
const doc = await vscode.workspace.openTextDocument({
language: "javascript",
content: uglyCode,
});

const editor = await vscode.window.showTextDocument(doc);

// Format the document
await vscode.commands.executeCommand("editor.action.formatDocument");

// The document should be formatted even though requireConfig may be enabled
// This proves that untitled documents bypass the requireConfig check
const formattedText = editor.document.getText();

assert.notEqual(
formattedText,
uglyCode,
"Untitled document should be formatted even with requireConfig enabled in workspace",
);

// Should not have excessive spacing
assert.ok(
!formattedText.includes(" "),
"Formatted code should not have excessive spacing",
);
});
});
4 changes: 4 additions & 0 deletions test-fixtures/require-config/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false,
"singleQuote": true
}
4 changes: 4 additions & 0 deletions test-fixtures/require-config/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"prettier.requireConfig": true,
"files.eol": "\n"
}
3 changes: 3 additions & 0 deletions test-fixtures/require-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "require-config-test"
}
3 changes: 3 additions & 0 deletions test-fixtures/test.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
{
"path": "monorepo-subfolder"
},
{
"path": "require-config"
},
{
"path": "yarn-pnp-sdk"
}
Expand Down
Loading