Skip to content

Commit

Permalink
refactor: Re-add (debug) output of transformed resources
Browse files Browse the repository at this point in the history
This functionality was accidentially removed in 5abbfc1
  • Loading branch information
RandomByte committed Apr 12, 2024
1 parent 5abbfc1 commit 132bc0d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
42 changes: 42 additions & 0 deletions src/linter/ui5Types/TypeLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {getLogger} from "@ui5/logger";
import LinterContext, {LinterParameters} from "../LinterContext.js";
// import {Project} from "@ui5/project";
import {AbstractAdapter} from "@ui5/fs";
import {createAdapter, createResource} from "@ui5/fs/resourceFactory";

const log = getLogger("linter:ui5Types:TypeLinter");

Expand Down Expand Up @@ -114,5 +115,46 @@ export default class TypeChecker {
}
}
typeCheckDone();

if (process.env.UI5LINT_WRITE_TRANSFORMED_SOURCES) {
// If requested, write out every resource that has a source map (which indicates it has been transformed)
// Loop over sourceMaps set
for (const [resourcePath, sourceMap] of sourceMaps) {
let fileContent = files.get(resourcePath);

if (typeof fileContent === "function") {
fileContent = fileContent();
}
if (fileContent) {
await writeTransformedSources(process.env.UI5LINT_WRITE_TRANSFORMED_SOURCES,
resourcePath, fileContent, sourceMap);
}
}
}
}
}

async function writeTransformedSources(fsBasePath: string,
originalResourcePath: string,
source: string, map: string | undefined) {
const transformedWriter = createAdapter({
fsBasePath,
virBasePath: "/",
});

await transformedWriter.write(
createResource({
path: originalResourcePath + ".ui5lint.transformed.js",
string: source,
})
);

if (map) {
await transformedWriter.write(
createResource({
path: originalResourcePath + ".ui5lint.transformed.js.map",
string: JSON.stringify(JSON.parse(map), null, "\t"),
})
);
}
}
2 changes: 1 addition & 1 deletion src/linter/ui5Types/amdTranspiler/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function createProgram(inputFileNames: string[], host: ts.CompilerHost): ts.Prog
return ts.createProgram(inputFileNames, compilerOptions, host);
}

export function transpileFile(fileName: string, content: string, strict?: boolean): TranspileResult {
export default function transpileAmdToEsm(fileName: string, content: string, strict?: boolean): TranspileResult {
// This is heavily inspired by the TypesScript "transpileModule" API

const taskDone = taskStart("Transpiling AMD to ESM", fileName, true);
Expand Down
4 changes: 2 additions & 2 deletions src/linter/ui5Types/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "node:path";
import posixPath from "node:path/posix";
import fs from "node:fs/promises";
import {createRequire} from "node:module";
import {transpileFile} from "./amdTranspiler/transpiler.js";
import transpileAmdToEsm from "./amdTranspiler/transpiler.js";
import {ResourcePath} from "../LinterContext.js";
const require = createRequire(import.meta.url);

Expand Down Expand Up @@ -119,7 +119,7 @@ export async function createVirtualCompilerHost(
}
if (fileContent && fileName.endsWith(".js") && !sourceMaps.get(fileName)) {
// No source map indicates no transpilation was done yet
const res = transpileFile(path.basename(fileName), fileContent);
const res = transpileAmdToEsm(path.basename(fileName), fileContent);
files.set(fileName, res.source);
sourceMaps.set(fileName, res.map);
fileContent = res.source;
Expand Down
4 changes: 2 additions & 2 deletions test/lib/linter/js/_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "node:path";
import util from "util";
import {readdirSync} from "node:fs";
import fs from "node:fs/promises";
import {transpileFile} from "../../../../src/linter/ui5Types/amdTranspiler/transpiler.js";
import transpileAmdToEsm from "../../../../src/linter/ui5Types/amdTranspiler/transpiler.js";

util.inspect.defaultOptions.depth = 4; // Increase AVA's printing depth since coverageInfo objects are on level 4

Expand Down Expand Up @@ -41,7 +41,7 @@ export function createTestsForFixtures(fixturesPath: string) {
defineTest(`Transpile ${testName}`, async (t) => {
const filePath = path.join(fixturesPath, fileName);
const fileContent = await fs.readFile(filePath);
const {source, map} = transpileFile(testName, fileContent.toString(), true);
const {source, map} = transpileAmdToEsm(testName, fileContent.toString(), true);
t.snapshot(source);
t.snapshot(map && JSON.parse(map));
});
Expand Down

0 comments on commit 132bc0d

Please sign in to comment.