Skip to content
Merged
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
8 changes: 6 additions & 2 deletions cmd/tsgolint/headless.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ func runHeadless(args []string) int {
return 1
}

fs := bundled.WrapFS(cachedvfs.From(osvfs.FS()))

configRaw, err := io.ReadAll(os.Stdin)
if err != nil {
writeErrorMessage(fmt.Sprintf("error reading from stdin: %v", err))
Expand All @@ -153,6 +151,12 @@ func runHeadless(args []string) int {
return 1
}

baseFS := osvfs.FS()
if len(payload.SourceOverrides) > 0 {
baseFS = newOverlayFS(baseFS, payload.SourceOverrides)
}
fs := bundled.WrapFS(cachedvfs.From(baseFS))

workload := linter.Workload{
Programs: make(map[string][]string),
UnmatchedFiles: []string{},
Expand Down
69 changes: 69 additions & 0 deletions cmd/tsgolint/overlayfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"time"

"github.com/microsoft/typescript-go/shim/vfs"
)

type overlayFS struct {
underlying vfs.FS
overrides map[string]string
}

func newOverlayFS(underlying vfs.FS, overrides map[string]string) vfs.FS {
return &overlayFS{
underlying: underlying,
overrides: overrides,
}
}

func (o *overlayFS) UseCaseSensitiveFileNames() bool {
return o.underlying.UseCaseSensitiveFileNames()
}

func (o *overlayFS) FileExists(path string) bool {
if _, ok := o.overrides[path]; ok {
return true
}
return o.underlying.FileExists(path)
}

func (o *overlayFS) ReadFile(path string) (string, bool) {
if content, ok := o.overrides[path]; ok {
return content, true
}
return o.underlying.ReadFile(path)
}

func (o *overlayFS) WriteFile(path string, data string, writeByteOrderMark bool) error {
return o.underlying.WriteFile(path, data, writeByteOrderMark)
}

func (o *overlayFS) Remove(path string) error {
return o.underlying.Remove(path)
}

func (o *overlayFS) Chtimes(path string, aTime time.Time, mTime time.Time) error {
return o.underlying.Chtimes(path, aTime, mTime)
}

func (o *overlayFS) DirectoryExists(path string) bool {
return o.underlying.DirectoryExists(path)
}

func (o *overlayFS) GetAccessibleEntries(path string) vfs.Entries {
return o.underlying.GetAccessibleEntries(path)
}

func (o *overlayFS) Stat(path string) vfs.FileInfo {
return o.underlying.Stat(path)
}

func (o *overlayFS) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
return o.underlying.WalkDir(root, walkFn)
}

func (o *overlayFS) Realpath(path string) string {
return o.underlying.Realpath(path)
}
47 changes: 47 additions & 0 deletions cmd/tsgolint/overlayfs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"testing"

"github.com/microsoft/typescript-go/shim/vfs/osvfs"
)

func TestOverlayFS(t *testing.T) {
baseFS := osvfs.FS()
overrides := map[string]string{
"/tmp/test.ts": "const x: number = 42;",
}

overlay := newOverlayFS(baseFS, overrides)

content, ok := overlay.ReadFile("/tmp/test.ts")
if !ok {
t.Fatal("Expected to read overridden file")
}

if content != "const x: number = 42;" {
t.Errorf("Expected 'const x: number = 42;', got %q", content)
}

if !overlay.FileExists("/tmp/test.ts") {
t.Error("Expected file to exist")
}

if overlay.UseCaseSensitiveFileNames() != baseFS.UseCaseSensitiveFileNames() {
t.Error("Expected UseCaseSensitiveFileNames to match base FS")
}
}

func TestOverlayFSFallthrough(t *testing.T) {
baseFS := osvfs.FS()
overrides := map[string]string{
"/tmp/override.ts": "overridden",
}

overlay := newOverlayFS(baseFS, overrides)

exists := overlay.FileExists("/nonexistent/file.ts")
if exists {
t.Error("Expected non-overridden non-existent file to not exist")
}
}
5 changes: 3 additions & 2 deletions cmd/tsgolint/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type headlessPayloadV1 struct {

// V2 (current) Headless payload format
type headlessPayload struct {
Version int `json:"version"` // version must be 2
Configs []headlessConfig `json:"configs"`
Version int `json:"version"` // version must be 2
Configs []headlessConfig `json:"configs"`
SourceOverrides map[string]string `json:"source_overrides,omitempty"`
}

type headlessConfig struct {
Expand Down
3 changes: 3 additions & 0 deletions e2e/fixtures/source-overrides/src/original.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This is the original file on disk without errors
const x: number = 42;
console.log(x);
11 changes: 11 additions & 0 deletions e2e/fixtures/source-overrides/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"]
}
70 changes: 70 additions & 0 deletions e2e/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,74 @@ describe('TSGoLint E2E Snapshot Tests', () => {

expect(v1Diagnostics).toStrictEqual(v2Diagnostics);
});

it('should use source overrides instead of reading from disk', async () => {
const testFiles = await getTestFiles('source-overrides');
expect(testFiles.length).toBeGreaterThan(0);
const testFile = testFiles[0];

const overriddenContent = `const promise = new Promise((resolve, _reject) => resolve("value"));
promise;
`;

const config = {
version: 2,
configs: [
{
file_paths: [testFile],
rules: [{ name: 'no-floating-promises' }],
},
],
source_overrides: {
[testFile]: overriddenContent,
},
};

const env = { ...process.env, GOMAXPROCS: '1' };
const output = execFileSync(TSGOLINT_BIN, ['headless'], {
input: JSON.stringify(config),
env,
});

let diagnostics = parseHeadlessOutput(output);
diagnostics = sortDiagnostics(diagnostics);

expect(diagnostics.length).toBe(1);
expect(diagnostics[0].rule).toBe('no-floating-promises');
expect(diagnostics[0].file_path).toContain('original.ts');
});

it('should not report errors when source override is valid', async () => {
const testFiles = await getTestFiles('source-overrides');
expect(testFiles.length).toBeGreaterThan(0);
const testFile = testFiles[0];

const validOverride = `// Valid code with no errors
const x: number = 42;
console.log(x);
`;

const config = {
version: 2,
configs: [
{
file_paths: [testFile],
rules: [{ name: 'no-floating-promises' }, { name: 'no-unsafe-assignment' }],
},
],
source_overrides: {
[testFile]: validOverride,
},
};

const env = { ...process.env, GOMAXPROCS: '1' };
const output = execFileSync(TSGOLINT_BIN, ['headless'], {
input: JSON.stringify(config),
env,
});

const diagnostics = parseHeadlessOutput(output);

expect(diagnostics.length).toBe(0);
});
});