Skip to content

Commit

Permalink
Avoid using source-map-support/register
Browse files Browse the repository at this point in the history
This was highly unperformant.
  • Loading branch information
badeball committed Oct 21, 2022
1 parent 06b3f6e commit f0ec1b7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function fail(message: string) {
}

export function assert(value: unknown, message: string): asserts value {
if (value) {
if (value != null) {
return;
}

Expand Down
31 changes: 29 additions & 2 deletions lib/diagnostics/diagnose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "source-map-support/register";
import fs from "fs/promises";
import path from "path";
import util from "util";
Expand All @@ -14,6 +13,7 @@ import {
import { generateMessages } from "@cucumber/gherkin";
import { IdGenerator, SourceMediaType } from "@cucumber/messages";
import * as esbuild from "esbuild";
import sourceMap from "source-map";
import { assert, assertAndReturn } from "../assertions";
import { createAstIdMap } from "../ast-helpers";
import { ensureIsRelative } from "../helpers/paths";
Expand Down Expand Up @@ -138,7 +138,7 @@ export async function diagnose(configuration: {
const esbuildResult = await esbuild.build({
entryPoints: [inputFileName],
bundle: true,
sourcemap: true,
sourcemap: "external",
outfile: outputFileName,
});

Expand Down Expand Up @@ -175,6 +175,33 @@ export async function diagnose(configuration: {
await fs.rm(outputFileName).catch(() => true);
}

const consumer = await new sourceMap.SourceMapConsumer(
(await fs.readFile(outputFileName + ".map")).toString()
);

for (const stepDefinition of registry.stepDefinitions) {
const originalPosition = position(stepDefinition);

const newPosition = consumer.originalPositionFor(originalPosition);

stepDefinition.position = {
line: assertAndReturn(
newPosition.line,
"Expected to find a line number"
),
column: assertAndReturn(
newPosition.column,
"Expected to find a column number"
),
source: assertAndReturn(
newPosition.source,
"Expected to find a source"
),
};
}

consumer.destroy();

const options = {
includeSource: false,
includeGherkinDocument: true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"module-alias": "^2.2.2",
"node-hook": "^1.0.0",
"resolve-pkg": "^2.0.0",
"source-map-support": "^0.5.21",
"source-map": "^0.7.4",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand Down

0 comments on commit f0ec1b7

Please sign in to comment.