Skip to content

Commit

Permalink
feat: Updated to support TS 4.9
Browse files Browse the repository at this point in the history
  • Loading branch information
nonara committed Dec 8, 2022
1 parent 060c6de commit 26f6099
Show file tree
Hide file tree
Showing 8 changed files with 3,490 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@
"standard-version": "^9.3.2",
"ts-jest": "^28.0.7",
"ts-node": "^10.4.0",
"ts-patch": "^1.4.5",
"ts-patch": "^2.0.2",
"ts-transform-img": "^0.4.2",
"ts-transform-react-intl": "^0.4.1",
"tsconfig-paths": "^3.11.0",
"typescript": "^4.7.4"
"typescript": "4.8.4"
},
"directories": {
"resources": "./dist/resources"
Expand Down
37 changes: 25 additions & 12 deletions src/installer/lib/patcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ function validate(tsModule?: TSModule, tsPackage?: TSPackage) {
return true;
}

const patchModule = (tsModule: TSModule, tsPackage: TSPackage, source?: string) => {
const patchModuleDiagnostics = (tsModule: TSModule, tsPackage: TSPackage, source?: string) => {
const src = source || tsModule.moduleSrc!;

const funcPos = src.search(/function emitFilesAndReportErrors\(/);
if (funcPos < 0) throw new Error(`Bad TS Code. Could not find function emitFilesAndReportErrors in ${tsModule.filename}`);

Expand Down Expand Up @@ -95,7 +96,7 @@ export function patchTSModule(tsModule: TSModule, tsPackage: TSPackage) {
const patchSrc = generatePatch(isTSC);

/* Add diagnostic modification support */
const moduleSrc = patchModule(tsModule, tsPackage);
const moduleSrc = patchModuleDiagnostics(tsModule, tsPackage);

try {
if (isTSC) {
Expand All @@ -107,17 +108,29 @@ export function patchTSModule(tsModule: TSModule, tsPackage: TSPackage) {
]
.filter(f => fs.existsSync(f))[0];

/* Get TSC-specific module slice */
const ver = tsPackage.version.split('.')
const majorVer = +ver[0];
const minorVer = +ver[1];

let tscSlice = majorVer >= 4 && minorVer >= 9
? moduleSrc.replace(/^[\s\S]+(\(function \(ts\) {\s+var StatisticType;[\s\S]+)$/, '$1')
: moduleSrc.replace(/^[\s\S]+(\(function \(ts\) {\s+function countLines[\s\S]+)$/, '$1');
const execCmdPost = tscSlice.lastIndexOf('\nts.executeCommandLine\(');
if (execCmdPost < 0) throw new Error(`Could not find tsc executeCommandLine`);
const execCmd = tscSlice.slice(execCmdPost);
tscSlice = tscSlice.slice(0, execCmdPost);

/* Expand TSC with full typescript library (splice tsc part on top of typescript.ts code) */
fs.writeFileSync(file,
Buffer.concat([
Buffer.from(getHeader()),
Buffer.from(patchModule(tsModule, tsPackage, fs.readFileSync(tsFile, 'utf-8'))),
Buffer.from(!getTSModule(tsFile).patchVersion ? patchSrc : ''),
Buffer.from(
moduleSrc.replace(/^[\s\S]+(\(function \(ts\) {\s+function countLines[\s\S]+)$/, '$1')
)
])
);
const content = Buffer.concat([
Buffer.from(getHeader()),
Buffer.from(patchModuleDiagnostics(tsModule, tsPackage, fs.readFileSync(tsFile, 'utf-8'))),
Buffer.from(!getTSModule(tsFile).patchVersion ? patchSrc : ''),
Buffer.from(tscSlice),
Buffer.from(execCmd)
]);

fs.writeFileSync(file, content);
} else fs.writeFileSync(file, Buffer.concat([
Buffer.from(getHeader()),
Buffer.from(moduleSrc),
Expand Down
2 changes: 1 addition & 1 deletion src/installer/lib/system/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K> {
/**
* Fully typed Object.keys
*/
export const getKeys = <T>(obj: T): Array<keyof T> => Object.keys(obj) as Array<keyof T>;
export const getKeys = <T extends object>(obj: T): Array<keyof T> => Object.keys(obj) as Array<keyof T>;

type GetDescriptorType<T extends PropertyDescriptor & { initializer?: (...args: any[]) => any }> =
'value' extends keyof T ? T['value'] :
Expand Down
1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"private": true,
"dependencies": {
"ts-40": "npm:typescript@4.0.*",
"ts-48": "npm:typescript@4.8.*",
"ts-latest": "npm:typescript@latest",
"ts-node": "latest",
"ts-patch": "link:..",
Expand Down
1 change: 1 addition & 0 deletions test/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const assetsDir = normalizeSlashes(path.resolve(__dirname, '../assets'));

export const tsModules = [
getTsModule('latest', 'ts-latest'),
getTsModule('4.8', 'ts-48'),
getTsModule('4.0', 'ts-40')
]

Expand Down
4 changes: 3 additions & 1 deletion test/tests/installer/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ describe(`Actions`, () => {
});

test(`Patches glob`, () => {
patch(joinPaths(tsLibDir, '*.js'), TSP_OPTIONS);
const srcFileNames = SRC_FILES.map(f => f.split('.')[0]);
const globStr = joinPaths(tsLibDir, `{${srcFileNames.join(',')}}.js`);
patch(globStr, TSP_OPTIONS);
checkModules(tspPackageJSON.version, tsLibDir);
});

Expand Down
12 changes: 7 additions & 5 deletions test/tests/patch/tsc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,25 @@ describe(`TSC`, () => {
beforeAll(() => {
tscPath = path.join(tmpDir, moduleSpecifier, 'lib/tsc.js');
const tscCode = fs.readFileSync(tscPath, 'utf8');
tscScript = new vm.Script(
`(function (exports, require, module, __filename, __dirname, tscArgs, process, mockWriteFile, console) { \n` +
const code = `(function (exports, require, module, __filename, __dirname, tscArgs, process, mockWriteFile, console) { \n` +
tscCode.replace(
/(^\s*?ts\.executeCommandLine\(ts\.sys)/m,
`\nObject.assign(ts.sys, { args: tscArgs, writeFile: mockWriteFile });\n$1`
) +
`})`
, { filename: tscPath, displayErrors: false });
`})`;

tscScript = new vm.Script(code, { filename: tscPath, displayErrors: false });
});

test('tsc transforms code & outputs standard diagnostic', () => {
const {
code,
message,
files
files,
err
} = execTsc(tscPath, tscScript, `--noEmit false -p ${path.join(assetsDir, 'src-files')}`);

expect(err).toBe('');
expect(code).toBe('2');
expect(message).toMatch(/TS2339/);
expect(files[transformedFile]).toMatch(expectedOut);
Expand Down

0 comments on commit 26f6099

Please sign in to comment.