Skip to content

Commit fbe3f8a

Browse files
authored
fix: error when include does not contain *.tsx (#5)
* fix: error when `include` does not contain `*.tsx` * Create six-beans-decide.md
1 parent 41da537 commit fbe3f8a

File tree

14 files changed

+121
-20
lines changed

14 files changed

+121
-20
lines changed

Diff for: .changeset/six-beans-decide.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"typescript-eslint-parser-for-extra-files": patch
3+
---
4+
5+
fix: error when `include` does not contain `*.tsx`

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"preversion": "yarn lint && yarn test",
4545
"release": "changeset publish",
4646
"test": "yarn mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
47-
"ts": "node -r esbuild-register"
47+
"ts": "node -r esbuild-register",
48+
"update-fixture": "yarn mocha \"tests/src/**/*.ts\" --update --reporter dot --timeout 60000"
4849
},
4950
"peerDependencies": {
5051
"@typescript-eslint/parser": ">=5.41.0",

Diff for: src/ts.ts

+63-15
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export class TSService {
5454
private readonly fileWatchCallbacks = new Map<string, () => void>();
5555

5656
public constructor(tsconfigPath: string, extraFileExtensions: string[]) {
57-
this.watch = this.createWatch(tsconfigPath, extraFileExtensions);
5857
this.extraFileExtensions = extraFileExtensions;
58+
this.watch = this.createWatch(tsconfigPath, extraFileExtensions);
5959
}
6060

6161
public getProgram(code: string, filePath: string): ts.Program {
@@ -84,6 +84,7 @@ export class TSService {
8484
tsconfigPath: string,
8585
extraFileExtensions: string[]
8686
): ts.WatchOfConfigFile<ts.BuilderProgram> {
87+
const normalizedTsconfigPath = normalizeFileName(tsconfigPath);
8788
const watchCompilerHost = ts.createWatchCompilerHost(
8889
tsconfigPath,
8990
{
@@ -103,18 +104,31 @@ export class TSService {
103104
() => {
104105
// Not reported in reportWatchStatus.
105106
},
106-
undefined,
107-
extraFileExtensions.map((extension) => ({
108-
extension,
109-
isMixedContent: true,
110-
scriptKind: ts.ScriptKind.Deferred,
111-
}))
107+
undefined
108+
// extraFileExtensions.map((extension) => ({
109+
// extension,
110+
// isMixedContent: true,
111+
// scriptKind: ts.ScriptKind.Deferred,
112+
// }))
112113
);
113114
const original = {
114115
// eslint-disable-next-line @typescript-eslint/unbound-method -- Store original
115116
readFile: watchCompilerHost.readFile,
116117
// eslint-disable-next-line @typescript-eslint/unbound-method -- Store original
117118
fileExists: watchCompilerHost.fileExists,
119+
// eslint-disable-next-line @typescript-eslint/unbound-method -- Store original
120+
readDirectory: watchCompilerHost.readDirectory,
121+
};
122+
watchCompilerHost.readDirectory = (...args) => {
123+
const results = original.readDirectory.call(watchCompilerHost, ...args);
124+
125+
return [
126+
...new Set(
127+
results.map((result) =>
128+
toVirtualTSXlFileName(result, extraFileExtensions)
129+
)
130+
),
131+
];
118132
};
119133
watchCompilerHost.readFile = (fileName, ...args) => {
120134
const realFileName = toRealFileName(fileName, extraFileExtensions);
@@ -127,19 +141,40 @@ export class TSService {
127141
});
128142
}
129143

130-
const code = original.readFile.call(this, realFileName, ...args);
131-
return (
132-
code &&
133-
transformExtraFile(code, {
134-
filePath: normalized,
135-
current: false,
136-
})
144+
const code = original.readFile.call(
145+
watchCompilerHost,
146+
realFileName,
147+
...args
137148
);
149+
if (!code) {
150+
return code;
151+
}
152+
if (normalizedTsconfigPath === normalized) {
153+
const configJson = ts.parseConfigFileTextToJson(realFileName, code);
154+
if (!configJson.config) {
155+
return code;
156+
}
157+
let include = undefined;
158+
159+
if (configJson.config.include) {
160+
include = [configJson.config.include]
161+
.flat()
162+
.map((s) => toVirtualTSXlFileName(s, extraFileExtensions));
163+
}
164+
return JSON.stringify({
165+
...configJson.config,
166+
include,
167+
});
168+
}
169+
return transformExtraFile(code, {
170+
filePath: normalized,
171+
current: false,
172+
});
138173
};
139174
// Modify it so that it can be determined that the virtual file actually exists.
140175
watchCompilerHost.fileExists = (fileName, ...args) =>
141176
original.fileExists.call(
142-
this,
177+
watchCompilerHost,
143178
toRealFileName(fileName, extraFileExtensions),
144179
...args
145180
);
@@ -197,6 +232,19 @@ function getFileNamesIncludingVirtualTSX(
197232
return [fileName];
198233
}
199234

235+
/** If the given filename has extra file extensions, returns the real virtual filename. */
236+
function toVirtualTSXlFileName(
237+
fileName: string,
238+
extraFileExtensions: string[]
239+
) {
240+
for (const extraFileExtension of extraFileExtensions) {
241+
if (fileName.endsWith(extraFileExtension)) {
242+
return `${fileName}.tsx`;
243+
}
244+
}
245+
return fileName;
246+
}
247+
200248
/** If the given filename is a virtual filename (.vue.tsx), returns the real filename. */
201249
function toRealFileName(fileName: string, extraFileExtensions: string[]) {
202250
for (const extraFileExtension of extraFileExtensions) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
interface Props {
3+
a: string;
4+
b: number;
5+
}
6+
let { a, b } = Astro.props;
7+
---
8+
9+
{a}{b}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
interface Props { // Props: Props
3+
a: string; // a: string
4+
b: number; // b: number
5+
}
6+
let { a, b } = Astro.props; // a: any, a: any, b: any, b: any, Astro.props: any
7+
---
8+
9+
{a}{b} // a: any, b: any

Diff for: tests/fixtures/types/astro/astro-component01/source.astro

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface Props {
77
b: TypeFoo;
88
foo: string;
99
}
10-
let { b, foo }: number = Astro.props;
10+
let { b, foo } = Astro.props;
1111
const t = numberValue * 2;
1212
---
1313

Diff for: tests/fixtures/types/astro/astro-component01/types.astro

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface Props { // Props: Props
77
b: TypeFoo; // TypeFoo: string, b: string
88
foo: string; // foo: string
99
}
10-
let { b, foo }: number = Astro.props; // b: any, b: any, foo: any, foo: any, Astro.props: any
10+
let { b, foo } = Astro.props; // b: any, b: any, foo: any, foo: any, Astro.props: any
1111
const t = numberValue * 2; // t: number, numberValue: 1
1212
---
1313

Diff for: tests/fixtures/types/astro/astro-component02/source.astro

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type TypeFoo = string;
44
interface Props {
55
a?: number;
66
}
7-
let { a = 1 }: number = Astro.props;
7+
let { a = 1 } = Astro.props;
88
let foo: number = 42;
99
---
1010

Diff for: tests/fixtures/types/astro/astro-component02/types.astro

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type TypeFoo = string; // TypeFoo: string
44
interface Props { // Props: Props
55
a?: number; // a: number | undefined
66
}
7-
let { a = 1 }: number = Astro.props; // a: any, a: any, Astro.props: any
7+
let { a = 1 } = Astro.props; // a: any, a: any, Astro.props: any
88
let foo: number = 42; // foo: number
99
---
1010

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script lang="ts">
2+
export let a: string;
3+
export let b: number;
4+
</script>
5+
6+
{a}{b}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script lang="ts">
2+
export let a: string; // a: string
3+
export let b: number; // b: number
4+
</script>
5+
6+
{a}{b} // a: string, b: number
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup lang="ts">
2+
let a = 1;
3+
</script>
4+
5+
<template>
6+
<div>{{ a }}</div>
7+
</template>
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup lang="ts">
2+
let a = 1; // a: number
3+
</script>
4+
5+
<template>
6+
<div>{{ a }}</div>
7+
</template>

Diff for: tests/src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ describe("Template Types", () => {
145145
const actual = buildTypes(source, result as any);
146146
const resultPath = sourcePath.replace(/source\.([a-z]+)$/u, "types.$1");
147147

148+
if (process.argv.includes("--update")) {
149+
fs.writeFileSync(resultPath, actual);
150+
}
148151
if (!fs.existsSync(resultPath)) {
149152
fs.writeFileSync(resultPath, actual);
150153
}

0 commit comments

Comments
 (0)