Skip to content

Commit

Permalink
test: more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Oct 25, 2023
1 parent ceb2ada commit e71a1e6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let foo: string = "f1";
let bar: string = "b1";
let baz: string = "b2";
const boo = <string>"abc";
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require("./a");

it("should generate correct sourceMap", async () => {
const path = require("path");
const fs = require("fs");
const source = fs.readFileSync(__filename + ".map", "utf-8");
const map = JSON.parse(source);
const sourceContent = fs.readFileSync(
path.resolve(__dirname, "../a.ts"),
"utf-8"
);
expect(map.sources).toContain("./a.ts");
expect(map.sourcesContent[1]).toEqual(sourceContent);

checkStub("fo" + "o", sourceContent);
checkStub("ba" + "r", sourceContent);
checkStub("ba" + "z", sourceContent);
checkStub(wrap("f" + 1), sourceContent);
checkStub(wrap("b" + 1), sourceContent);
checkStub(wrap("b" + 2), sourceContent);
checkStub(wrap("ab" + "c"), sourceContent);
});

const wrap = v => `"${v}"`;
const checkStub = async (stub, sourceContent) => {
const fs = require("fs");
const { SourceMapConsumer } = require("source-map");

const source = fs.readFileSync(__filename + ".map", "utf-8");
const map = JSON.parse(source);
const consumer = await new SourceMapConsumer(map);
const generated = fs.readFileSync(__filename, "utf-8");
const { line, column } = consumer.originalPositionFor(
positionFor(generated, stub)
);
const { line: originalLine, column: originalColumn } = positionFor(
sourceContent,
stub
);
expect(line).toBe(originalLine);
expect(column).toBe(originalColumn);
};

const positionFor = (content, text) => {
let lines = content.split(/\r?\n/);
for (let i = 0; i < lines.length; i++) {
const column = lines[i].indexOf(text);
if (column >= 0) return { line: i + 1, column };
}
return null;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
devtool: "source-map",
externals: ["source-map"],
externalsType: "commonjs",
module: {
rules: [
{
test: /\.ts$/,
use: [
{
loader: "builtin:swc-loader",
options: {
jsc: {
parser: {
syntax: "typescript"
}
}
}
}
],
type: "javascript/auto"
}
]
},
experiments: {
rspackFuture: {
disableTransformByDefault: true
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ it("should map to the original content if `module` enabled", async () => {
const consumer = await new sourceMap.SourceMapConsumer(map);
expect(map.sources).toContain("./App.jsx");
expect(map.sourcesContent[1]).toEqual(app);
const STUB =
"\u0048\u0065\u006c\u006c\u006f\u0020\u0052\u0073\u0070\u0061\u0063\u006b\u0021";
const STUB = "Hello" + " " + "Rspack!";
const { line, column } = consumer.originalPositionFor(
positionFor(generated, STUB)
);
Expand Down

0 comments on commit e71a1e6

Please sign in to comment.