-
-
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 we also sync the mts with the gts files
- Loading branch information
Showing
10 changed files
with
903 additions
and
522 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,117 @@ | ||
const fs = require('node:fs'); | ||
const { transformForLint } = require('./transform'); | ||
const babel = require('@babel/core'); | ||
const { replaceRange } = require('./transform'); | ||
|
||
let patchTs, replaceExtensions, syncMtsGtsSourceFiles, typescriptParser; | ||
|
||
try { | ||
const ts = require('typescript'); | ||
typescriptParser = require('@typescript-eslint/parser'); | ||
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); | ||
}; | ||
|
||
replaceExtensions = 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; | ||
}; | ||
|
||
/** | ||
* | ||
* @param program {ts.Program} | ||
*/ | ||
syncMtsGtsSourceFiles = function syncMtsGtsSourceFiles(program) { | ||
const sourceFiles = program.getSourceFiles(); | ||
for (const sourceFile of sourceFiles) { | ||
// check for deleted gts files, need to remove mts as well | ||
if (sourceFile.path.endsWith('.mts') && sourceFile.isVirtualGts) { | ||
const gtsFile = program.getSourceFile(sourceFile.path.replace(/\.mts$/, '.gts')); | ||
if (!gtsFile) { | ||
sourceFile.version = null; | ||
} | ||
} | ||
if (sourceFile.path.endsWith('.gts')) { | ||
/** | ||
* @type {ts.SourceFile} | ||
*/ | ||
const mtsSourceFile = program.getSourceFile(sourceFile.path.replace(/\.gts$/, '.mts')); | ||
if (mtsSourceFile) { | ||
const keep = { | ||
fileName: mtsSourceFile.fileName, | ||
path: mtsSourceFile.path, | ||
originalFileName: mtsSourceFile.originalFileName, | ||
resolvedPath: mtsSourceFile.resolvedPath, | ||
}; | ||
Object.assign(mtsSourceFile, sourceFile, keep); | ||
mtsSourceFile.isVirtualGts = true; | ||
} | ||
} | ||
} | ||
}; | ||
} catch { | ||
// typescript not available | ||
patchTs = () => null; | ||
replaceExtensions = (code) => code; | ||
syncMtsGtsSourceFiles = () => null; | ||
} | ||
|
||
module.exports = { | ||
patchTs, | ||
replaceExtensions, | ||
syncMtsGtsSourceFiles, | ||
typescriptParser | ||
Check failure on line 116 in lib/parsers/ts-utils.js GitHub Actions / build (ubuntu, 18.x)
Check failure on line 116 in lib/parsers/ts-utils.js GitHub Actions / build (ubuntu, 20.x)
|
||
}; |
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" | ||
], | ||
} |
Oops, something went wrong.