Skip to content

Commit

Permalink
fix: Eslint findings
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Feb 7, 2025
1 parent 634073d commit 5205e53
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/linter/ui5Types/TypeLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ export default class TypeChecker {
const sourceMaps = new Map<string, string>(); // Maps a source path to source map content
const apiExtract = await loadApiExtract();
const host = await createVirtualCompilerHost(this.#compilerOptions, files, sourceMaps, this.#context);
let {program, checker, pathsToLint} = await this.getProgram(host, files, sourceMaps);
const programInfo = await this.getProgram(host, files, sourceMaps);
let {program, checker} = programInfo;
const {pathsToLint} = programInfo;

const reportCoverage = this.#context.getReportCoverage();
const messageDetails = this.#context.getIncludeMessageDetails();
Expand Down
28 changes: 14 additions & 14 deletions src/linter/xmlTemplate/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import transpileXml from "./transpiler.js";
import {LinterParameters} from "../LinterContext.js";
import ControllerByIdInfo from "./ControllerByIdInfo.js";
import {ControllerByIdDtsGenerator} from "./generator/ControllerByIdDtsGenerator.js";
import { decodedMap, DecodedSourceMap, SourceMapInput, TraceMap } from "@jridgewell/trace-mapping";
import {decodedMap, DecodedSourceMap, SourceMapInput, TraceMap} from "@jridgewell/trace-mapping";

// For usage in TypeLinter to write the file as part of the internal WRITE_TRANSFORMED_SOURCES debug mode
export const CONTROLLER_BY_ID_DTS_PATH = "/types/@ui5/linter/virtual/ControllerById.d.ts";
Expand All @@ -24,17 +24,17 @@ export default async function lintXml({filePathsWorkspace, workspace, context}:

const metadata = context.getMetadata(resourcePath);
let xmlFromJsResourceMap;
// if (metadata.jsToXmlPosMapping) {
// xmlFromJsResourceMap = JSON.parse(map) as DecodedSourceMap;
// const {pos} = metadata.jsToXmlPosMapping;
// // If it's an XML snippet extracted from a JS file, adjust the source map positions
// // as they positions are relative to the extracted string, not to the real position in the JS file.
// // Add that missing line shift from the original JS file to the source map.
// xmlFromJsResourceMap = fixSourceMapIndices(xmlFromJsResourceMap, pos.line);
// // Replace the name of the source file in the source map with the original JS file name,
// // so that reporter will lead to the correct file.
// xmlFromJsResourceMap.sources.splice(0, 1, metadata.jsToXmlPosMapping.originalPath.split("/").pop() ?? null);
// }
if (metadata.jsToXmlPosMapping) {
xmlFromJsResourceMap = JSON.parse(map) as DecodedSourceMap;
const {pos} = metadata.jsToXmlPosMapping;
// If it's an XML snippet extracted from a JS file, adjust the source map positions
// as they positions are relative to the extracted string, not to the real position in the JS file.
// Add that missing line shift from the original JS file to the source map.
xmlFromJsResourceMap = fixSourceMapIndices(xmlFromJsResourceMap, pos.line);
// Replace the name of the source file in the source map with the original JS file name,
// so that reporter will lead to the correct file.
xmlFromJsResourceMap.sources.splice(0, 1, metadata.jsToXmlPosMapping.originalPath.split("/").pop() ?? null);
}

// Write transpiled resource to workspace
// TODO: suffix name to prevent clashes with existing files?
Expand Down Expand Up @@ -77,10 +77,10 @@ function fixSourceMapIndices(map: SourceMapInput, lineShift = 0, columnShift = 0
const newMappings = mappings.map((segment) =>
segment.map(([genCol, srcIndex, origLine, origCol, nameIndex]) => {
return (srcIndex !== undefined ?
[genCol + columnShift, srcIndex, origLine! + lineShift, origCol! + columnShift, nameIndex] :
[genCol + columnShift, srcIndex, origLine! + lineShift, origCol! + columnShift, nameIndex ?? 0] :
[genCol + columnShift]);
})
);

return {...decodedTraceMap, mappings: newMappings};
return {...decodedTraceMap, mappings: newMappings as DecodedSourceMap["mappings"]};
}
4 changes: 3 additions & 1 deletion test/lib/linter/_linterHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SourceFileLinter from "../../../src/linter/ui5Types/SourceFileLinter.js";
import {SourceFile, TypeChecker} from "typescript";
import LinterContext, {LinterOptions, LintResult} from "../../../src/linter/LinterContext.js";
import {ApiExtract} from "../../../src/utils/ApiExtract.js";
import {AbstractAdapter} from "@ui5/fs";

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

Expand All @@ -33,12 +34,13 @@ export async function esmockDeprecationText() {
reportCoverage: boolean | undefined = false,
messageDetails: boolean | undefined = false,
apiExtract: ApiExtract,
filePathsWorkspace: AbstractAdapter,
manifestContent?: string
) {
// Don't use sinon's stubs as it's hard to clean after them in this case and it leaks memory.
const linter = new SourceFileLinter(
context, filePath, sourceFile, sourceMaps, checker, reportCoverage,
messageDetails, apiExtract, manifestContent
messageDetails, apiExtract, filePathsWorkspace, manifestContent
);
linter.getDeprecationText = () => "Deprecated test message";
return linter;
Expand Down
11 changes: 4 additions & 7 deletions test/lib/linter/xmlInJs/_helper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import anyTest, {TestFn} from "ava";
import sinonGlobal from "sinon";
import path from "node:path";
import util from "util";
import {readdirSync} from "node:fs";
import fs from "node:fs/promises";
import {lintFile} from "../../../../src/linter/linter.js";
import {extractXMLFromJs} from "../../../../src/linter/xmlInJs/transpile.js";

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

Expand Down Expand Up @@ -40,9 +37,9 @@ export function createTestsForFixtures(fixturesPath: string) {
// Executing linting in parallel might lead to OOM errors in the CI
// Therefore always use serial
defineTest(`Transpile ${testName}`, async (t) => {
const filePath = path.join(fixturesPath, fileName);
const fileContent = await fs.readFile(filePath);
const extractedResource = extractXMLFromJs(testName, fileContent.toString());
// const filePath = path.join(fixturesPath, fileName);
// const fileContent = await fs.readFile(filePath);
// const extractedResource = extractXMLFromJs(testName, fileContent.toString());

const resources = await lintFile({
rootDir: fixturesPath,
Expand All @@ -51,7 +48,7 @@ export function createTestsForFixtures(fixturesPath: string) {
details: true,
});

extractedResource?.forEach((resource) => t.snapshot(resource.xmlSnippet));
// extractedResource?.forEach((resource) => t.snapshot(resource.xmlSnippet));
resources.forEach((res) => {
res.messages.sort(
(a, b) => (a.line! - b.line!) + (a.column! - b.column!));
Expand Down

0 comments on commit 5205e53

Please sign in to comment.