-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix gts type aware by emulating them to ts files
set our own ts.sys with ts.setSys typescript-eslint has handling for a lot of scenarios for file changes and project changes etc use mts extension to keep same offsets
- Loading branch information
Showing
10 changed files
with
768 additions
and
519 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const fs = require('node:fs'); | ||
const ts = require('typescript'); | ||
const { transformForLint } = require('./transform'); | ||
const babel = require('@babel/core'); | ||
const { replaceRange } = require('./transform'); | ||
|
||
module.exports.patchTs = function patchTs() { | ||
const sys = { ...ts.sys }; | ||
const newSys = { | ||
...ts.sys, | ||
readDirectory(...args) { | ||
const results = sys.readDirectory.call(this, ...args); | ||
return [ | ||
...results, | ||
...results.filter((x) => x.endsWith('.gts')).map((f) => f.replace(/\.gts$/, '.mts')), | ||
]; | ||
}, | ||
fileExists(fileName) { | ||
return fs.existsSync(fileName.replace(/\.mts$/, '.gts')) || fs.existsSync(fileName); | ||
}, | ||
readFile(fname) { | ||
let fileName = fname; | ||
let content = ''; | ||
try { | ||
content = fs.readFileSync(fileName).toString(); | ||
} catch { | ||
fileName = fileName.replace(/\.mts$/, '.gts'); | ||
content = fs.readFileSync(fileName).toString(); | ||
} | ||
if (fileName.endsWith('.gts')) { | ||
content = transformForLint(content).output; | ||
} | ||
if ((!fileName.endsWith('.d.ts') && fileName.endsWith('.ts')) || fileName.endsWith('.gts')) { | ||
content = replaceExtensions(content); | ||
} | ||
return content; | ||
}, | ||
}; | ||
ts.setSys(newSys); | ||
}; | ||
|
||
function replaceExtensions(code) { | ||
let jsCode = code; | ||
const babelParseResult = babel.parse(jsCode, { | ||
parserOpts: { ranges: true, plugins: ['typescript'] }, | ||
}); | ||
const length = jsCode.length; | ||
for (const b of babelParseResult.program.body) { | ||
if (b.type === 'ImportDeclaration' && b.source.value.endsWith('.gts')) { | ||
const value = b.source.value.replace(/\.gts$/, '.mts'); | ||
const strWrapper = jsCode[b.source.start]; | ||
jsCode = replaceRange(jsCode, b.source.start, b.source.end, strWrapper + value + strWrapper); | ||
} | ||
} | ||
if (length !== jsCode.length) { | ||
throw new Error('bad replacement'); | ||
} | ||
return jsCode; | ||
} | ||
|
||
module.exports.replaceExtensions = replaceExtensions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const fortyTwoFromGTS = '42'; | ||
|
||
<template> | ||
{{fortyTwoFromGTS}} | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const fortyTwoFromTS = '42'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { fortyTwoFromGTS } from './bar.gts'; | ||
import { fortyTwoFromTS } from './baz.ts'; | ||
|
||
export const fortyTwoLocal = '42'; | ||
|
||
const helloWorldFromTS = fortyTwoFromTS[0] === '4' ? 'hello' : 'world'; | ||
const helloWorldFromGTS = fortyTwoFromGTS[0] === '4' ? 'hello' : 'world'; | ||
const helloWorld = fortyTwoLocal[0] === '4' ? 'hello' : 'world'; | ||
// | ||
<template> | ||
{{helloWorldFromGTS}} | ||
{{helloWorldFromTS}} | ||
{{helloWorld}} | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"strictNullChecks": true | ||
}, | ||
"include": [ | ||
"*" | ||
] | ||
"**/*.ts", | ||
"**/*.gts" | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters