Skip to content

Commit 7e496d9

Browse files
committed
use mts extension
1 parent 374ff54 commit 7e496d9

File tree

5 files changed

+106
-7
lines changed

5 files changed

+106
-7
lines changed

lib/parsers/gjs-gts-parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const babelParser = require('@babel/eslint-parser');
22
const typescriptParser = require('@typescript-eslint/parser');
33
const { registerParsedFile } = require('../preprocessors/noop');
4-
const { patchTs } = require('./ts-utils');
4+
const { patchTs, replaceExtensions } = require('./ts-utils');
55
const { transformForLint, preprocessGlimmerTemplates, convertAst } = require('./transform');
66

77
patchTs();
@@ -23,13 +23,13 @@ module.exports = {
2323
let jsCode = code;
2424
const info = transformForLint(code);
2525
jsCode = info.output;
26-
jsCode = jsCode.replaceAll(/\.gts(["'])/g, '.ts$1 ');
2726

2827
const isTypescript = options.filePath.endsWith('.gts');
2928

3029
let result = null;
3130
let filePath = options.filePath;
3231
if (options.project) {
32+
jsCode = replaceExtensions(jsCode);
3333
filePath = options.filePath.replace(/\.gts$/, '.ts');
3434
}
3535

lib/parsers/transform.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,10 @@ module.exports.convertAst = function convertAst(result, preprocessedResult, visi
454454
}
455455
};
456456

457-
function replaceRange(s, start, end, substitute) {
457+
const replaceRange = function replaceRange(s, start, end, substitute) {
458458
return s.slice(0, start) + substitute + s.slice(end);
459-
}
459+
};
460+
module.exports.replaceRange = replaceRange;
460461

461462
const processor = new ContentTag.Preprocessor();
462463

lib/parsers/ts-utils.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const fs = require('node:fs');
22
const ts = require('typescript');
33
const { transformForLint } = require('./transform');
4+
const babel = require('@babel/core');
5+
const { replaceRange } = require('./transform');
46

57
module.exports.patchTs = function patchTs() {
68
const readDirectory = ts.sys.readDirectory;
@@ -9,21 +11,44 @@ module.exports.patchTs = function patchTs() {
911
return results.map((f) => f.replace(/\.gts$/, '.ts'));
1012
};
1113
ts.sys.fileExists = function (fileName) {
12-
return fs.existsSync(fileName.replace(/\.ts$/, '.gts')) || fs.existsSync(fileName);
14+
return fs.existsSync(fileName.replace(/\.mts$/, '.gts')) || fs.existsSync(fileName);
1315
};
1416
ts.sys.readFile = function (fname) {
1517
let fileName = fname;
1618
let content = '';
1719
try {
1820
content = fs.readFileSync(fileName).toString();
1921
} catch {
20-
fileName = fileName.replace(/\.ts$/, '.gts');
22+
fileName = fileName.replace(/\.mts$/, '.gts');
2123
content = fs.readFileSync(fileName).toString();
2224
}
2325
if (fileName.endsWith('.gts')) {
2426
content = transformForLint(content).output;
2527
}
26-
content = content.replaceAll(/\.gts(["'])/g, '.ts$1 ');
28+
if (fileName.endsWith('.ts') || fileName.endsWith('.gts')) {
29+
content = replaceExtensions(content);
30+
}
2731
return content;
2832
};
2933
};
34+
35+
function replaceExtensions(code) {
36+
let jsCode = code;
37+
const babelParseResult = babel.parse(jsCode, {
38+
parserOpts: { ranges: true, plugins: ['typescript'] },
39+
});
40+
const length = jsCode.length;
41+
for (const b of babelParseResult.program.body) {
42+
if (b.type === 'ImportDeclaration' && b.source.value.endsWith('.gts')) {
43+
const value = b.source.value.replace(/\.gts$/, '.mts');
44+
const strWrapper = jsCode[b.source.start];
45+
jsCode = replaceRange(jsCode, b.source.start, b.source.end, strWrapper + value + strWrapper);
46+
}
47+
}
48+
if (length !== jsCode.length) {
49+
throw new Error('bad replacement');
50+
}
51+
return jsCode;
52+
}
53+
54+
module.exports.replaceExtensions = replaceExtensions;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
]
6969
},
7070
"dependencies": {
71+
"@babel/core": "^7.23.3",
7172
"@babel/eslint-parser": "^7.22.15",
7273
"@ember-data/rfc395-data": "^0.0.4",
7374
"@glimmer/syntax": "^0.85.12",

yarn.lock

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@
4949
json5 "^2.2.3"
5050
semver "^6.3.1"
5151

52+
"@babel/core@^7.23.3":
53+
version "7.23.3"
54+
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.23.3.tgz#5ec09c8803b91f51cc887dedc2654a35852849c9"
55+
integrity sha512-Jg+msLuNuCJDyBvFv5+OKOUjWMZgd85bKjbICd3zWrKAo+bJ49HJufi7CQE0q0uR8NGyO6xkCACScNqyjHSZew==
56+
dependencies:
57+
"@ampproject/remapping" "^2.2.0"
58+
"@babel/code-frame" "^7.22.13"
59+
"@babel/generator" "^7.23.3"
60+
"@babel/helper-compilation-targets" "^7.22.15"
61+
"@babel/helper-module-transforms" "^7.23.3"
62+
"@babel/helpers" "^7.23.2"
63+
"@babel/parser" "^7.23.3"
64+
"@babel/template" "^7.22.15"
65+
"@babel/traverse" "^7.23.3"
66+
"@babel/types" "^7.23.3"
67+
convert-source-map "^2.0.0"
68+
debug "^4.1.0"
69+
gensync "^1.0.0-beta.2"
70+
json5 "^2.2.3"
71+
semver "^6.3.1"
72+
5273
"@babel/eslint-parser@^7.22.15":
5374
version "7.22.15"
5475
resolved "https://registry.yarnpkg.com/@babel/eslint-parser/-/eslint-parser-7.22.15.tgz#263f059c476e29ca4972481a17b8b660cb025a34"
@@ -68,6 +89,16 @@
6889
"@jridgewell/trace-mapping" "^0.3.17"
6990
jsesc "^2.5.1"
7091

92+
"@babel/generator@^7.23.3":
93+
version "7.23.3"
94+
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.23.3.tgz#86e6e83d95903fbe7613f448613b8b319f330a8e"
95+
integrity sha512-keeZWAV4LU3tW0qRi19HRpabC/ilM0HRBBzf9/k8FFiG4KVpiv0FIy4hHfLfFQZNhziCTPTmd59zoyv6DNISzg==
96+
dependencies:
97+
"@babel/types" "^7.23.3"
98+
"@jridgewell/gen-mapping" "^0.3.2"
99+
"@jridgewell/trace-mapping" "^0.3.17"
100+
jsesc "^2.5.1"
101+
71102
"@babel/helper-annotate-as-pure@^7.22.5":
72103
version "7.22.5"
73104
resolved "https://registry.yarnpkg.com/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.22.5.tgz#e7f06737b197d580a01edf75d97e2c8be99d3882"
@@ -146,6 +177,17 @@
146177
"@babel/helper-split-export-declaration" "^7.22.6"
147178
"@babel/helper-validator-identifier" "^7.22.20"
148179

180+
"@babel/helper-module-transforms@^7.23.3":
181+
version "7.23.3"
182+
resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.23.3.tgz#d7d12c3c5d30af5b3c0fcab2a6d5217773e2d0f1"
183+
integrity sha512-7bBs4ED9OmswdfDzpz4MpWgSrV7FXlc3zIagvLFjS5H+Mk7Snr21vQ6QwrsoCGMfNC4e4LQPdoULEt4ykz0SRQ==
184+
dependencies:
185+
"@babel/helper-environment-visitor" "^7.22.20"
186+
"@babel/helper-module-imports" "^7.22.15"
187+
"@babel/helper-simple-access" "^7.22.5"
188+
"@babel/helper-split-export-declaration" "^7.22.6"
189+
"@babel/helper-validator-identifier" "^7.22.20"
190+
149191
"@babel/helper-optimise-call-expression@^7.22.5":
150192
version "7.22.5"
151193
resolved "https://registry.yarnpkg.com/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.22.5.tgz#f21531a9ccbff644fdd156b4077c16ff0c3f609e"
@@ -226,6 +268,11 @@
226268
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.23.0.tgz#da950e622420bf96ca0d0f2909cdddac3acd8719"
227269
integrity sha512-vvPKKdMemU85V9WE/l5wZEmImpCtLqbnTvqDS2U1fJ96KrxoW7KrXhNsNCblQlg8Ck4b85yxdTyelsMUgFUXiw==
228270

271+
"@babel/parser@^7.23.3":
272+
version "7.23.3"
273+
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.23.3.tgz#0ce0be31a4ca4f1884b5786057cadcb6c3be58f9"
274+
integrity sha512-uVsWNvlVsIninV2prNz/3lHCb+5CJ+e+IUBfbjToAHODtfGYLfCFuY4AU7TskI+dAKk+njsPiBjq1gKTvZOBaw==
275+
229276
"@babel/plugin-proposal-class-properties@^7.18.6":
230277
version "7.18.6"
231278
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.18.6.tgz#b110f59741895f7ec21a6fff696ec46265c446a3"
@@ -375,6 +422,22 @@
375422
debug "^4.1.0"
376423
globals "^11.1.0"
377424

425+
"@babel/traverse@^7.23.3":
426+
version "7.23.3"
427+
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.23.3.tgz#26ee5f252e725aa7aca3474aa5b324eaf7908b5b"
428+
integrity sha512-+K0yF1/9yR0oHdE0StHuEj3uTPzwwbrLGfNOndVJVV2TqA5+j3oljJUb4nmB954FLGjNem976+B+eDuLIjesiQ==
429+
dependencies:
430+
"@babel/code-frame" "^7.22.13"
431+
"@babel/generator" "^7.23.3"
432+
"@babel/helper-environment-visitor" "^7.22.20"
433+
"@babel/helper-function-name" "^7.23.0"
434+
"@babel/helper-hoist-variables" "^7.22.5"
435+
"@babel/helper-split-export-declaration" "^7.22.6"
436+
"@babel/parser" "^7.23.3"
437+
"@babel/types" "^7.23.3"
438+
debug "^4.1.0"
439+
globals "^11.1.0"
440+
378441
"@babel/types@^7.0.0", "@babel/types@^7.20.7", "@babel/types@^7.22.15", "@babel/types@^7.22.5", "@babel/types@^7.23.0", "@babel/types@^7.3.3":
379442
version "7.23.0"
380443
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.23.0.tgz#8c1f020c9df0e737e4e247c0619f58c68458aaeb"
@@ -384,6 +447,15 @@
384447
"@babel/helper-validator-identifier" "^7.22.20"
385448
to-fast-properties "^2.0.0"
386449

450+
"@babel/types@^7.23.3":
451+
version "7.23.3"
452+
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.23.3.tgz#d5ea892c07f2ec371ac704420f4dcdb07b5f9598"
453+
integrity sha512-OZnvoH2l8PK5eUvEcUyCt/sXgr/h+UWpVuBbOljwcrAgUl6lpchoQ++PHGyQy1AtYnVA6CEq3y5xeEI10brpXw==
454+
dependencies:
455+
"@babel/helper-string-parser" "^7.22.5"
456+
"@babel/helper-validator-identifier" "^7.22.20"
457+
to-fast-properties "^2.0.0"
458+
387459
"@bcoe/v8-coverage@^0.2.3":
388460
version "0.2.3"
389461
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"

0 commit comments

Comments
 (0)