Skip to content

Commit 5b39dd2

Browse files
committed
Rework
1 parent 1f8c9d6 commit 5b39dd2

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

src/parser/gjs-gts-parser.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ module.exports = {
140140
parseForESLint(code, options) {
141141
const allowGjsWasSet = options.allowGjs !== undefined;
142142
const allowGjs = allowGjsWasSet ? options.allowGjs : getAllowJs(options);
143-
patchTs({ allowGjs });
143+
let actualAllowGjs;
144+
// Only patch TypeScript if we actually need it.
145+
if (options.programs || options.projectService || options.project) {
146+
({ allowGjs: actualAllowGjs } = patchTs({ allowGjs }));
147+
}
144148
registerParsedFile(options.filePath);
145149
let jsCode = code;
146150
const info = transformForLint(code, options.filePath);
@@ -189,10 +193,16 @@ module.exports = {
189193
if (result.services?.program) {
190194
// Compare allowJs with the actual program's compiler options
191195
const programAllowJs = result.services.program.getCompilerOptions?.()?.allowJs;
192-
if (!allowGjsWasSet && programAllowJs !== undefined && allowGjs !== programAllowJs) {
196+
if (
197+
!allowGjsWasSet &&
198+
programAllowJs !== undefined &&
199+
actualAllowGjs !== undefined &&
200+
actualAllowGjs !== programAllowJs
201+
) {
193202
// eslint-disable-next-line no-console
194203
console.warn(
195-
'[ember-eslint-parser] allowJs does not match the actual program. Consider setting allowGjs explicitly.'
204+
'[ember-eslint-parser] allowJs does not match the actual program. Consider setting allowGjs explicitly.\n' +
205+
` Current: ${allowGjs}, Program: ${programAllowJs}`
196206
);
197207
}
198208
syncMtsGtsSourceFiles(result.services.program);

src/parser/ts-patch.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ const fs = require('node:fs');
22
const { transformForLint } = require('./transforms');
33
const { replaceRange } = require('./transforms');
44

5-
let patchTs, replaceExtensions, syncMtsGtsSourceFiles, typescriptParser, isPatched;
6-
let globalAllowGjs = true;
5+
let patchTs, replaceExtensions, syncMtsGtsSourceFiles, typescriptParser, isPatched, allowGjs;
76

87
try {
98
const parserPath = require.resolve('@typescript-eslint/parser');
@@ -12,9 +11,9 @@ try {
1211
const ts = require(tsPath);
1312
typescriptParser = require('@typescript-eslint/parser');
1413
patchTs = function patchTs(options = {}) {
15-
if (isPatched) return;
14+
if (isPatched) return { allowGjs };
1615
isPatched = true;
17-
globalAllowGjs = options.allowGjs !== undefined ? options.allowGjs : true;
16+
allowGjs = options.allowGjs !== undefined ? options.allowGjs : true;
1817
const sys = { ...ts.sys };
1918
const newSys = {
2019
...ts.sys,
@@ -23,16 +22,14 @@ try {
2322
const gtsVirtuals = results
2423
.filter((x) => x.endsWith('.gts'))
2524
.map((f) => f.replace(/\.gts$/, '.mts'));
26-
const gjsVirtuals = globalAllowGjs
25+
const gjsVirtuals = allowGjs
2726
? results.filter((x) => x.endsWith('.gjs')).map((f) => f.replace(/\.gjs$/, '.mjs'))
2827
: [];
2928
return results.concat(gtsVirtuals, gjsVirtuals);
3029
},
3130
fileExists(fileName) {
3231
const gtsExists = fs.existsSync(fileName.replace(/\.m?ts$/, '.gts'));
33-
const gjsExists = globalAllowGjs
34-
? fs.existsSync(fileName.replace(/\.m?js$/, '.gjs'))
35-
: false;
32+
const gjsExists = allowGjs ? fs.existsSync(fileName.replace(/\.m?js$/, '.gjs')) : false;
3633
return gtsExists || gjsExists || fs.existsSync(fileName);
3734
},
3835
readFile(fname) {
@@ -47,12 +44,12 @@ try {
4744
} catch {
4845
if (fileName.match(/\.m?ts$/)) {
4946
fileName = fileName.replace(/\.m?ts$/, '.gts');
50-
} else if (globalAllowGjs && fileName.match(/\.m?js$/)) {
47+
} else if (allowGjs && fileName.match(/\.m?js$/)) {
5148
fileName = fileName.replace(/\.m?js$/, '.gjs');
5249
}
5350
content = fs.readFileSync(fileName).toString();
5451
}
55-
if (fileName.endsWith('.gts') || (globalAllowGjs && fileName.endsWith('.gjs'))) {
52+
if (fileName.endsWith('.gts') || (allowGjs && fileName.endsWith('.gjs'))) {
5653
try {
5754
content = transformForLint(content).output;
5855
} catch (e) {
@@ -63,7 +60,7 @@ try {
6360
if (
6461
(!fileName.endsWith('.d.ts') && fileName.endsWith('.ts')) ||
6562
fileName.endsWith('.gts') ||
66-
(globalAllowGjs && fileName.endsWith('.gjs'))
63+
(allowGjs && fileName.endsWith('.gjs'))
6764
) {
6865
try {
6966
content = replaceExtensions(content);
@@ -76,6 +73,7 @@ try {
7673
},
7774
};
7875
ts.setSys(newSys);
76+
return { allowGjs };
7977
};
8078

8179
replaceExtensions = function replaceExtensions(code) {

0 commit comments

Comments
 (0)