Skip to content

Commit

Permalink
Updates to satisfy linter
Browse files Browse the repository at this point in the history
Also makes the linter ignore the submodule
  • Loading branch information
takikawa committed Jun 6, 2024
1 parent 8afce96 commit 64050e6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
},
"license": "BSD-3-Clause",
"scripts": {
"lint": "eslint --fix *.js lib/ test/",
"lint": "eslint --fix *.js lib/ test/ --ignore-pattern 'test/source-map-tests/**'",
"test": "git submodule update --init --recursive; node test/run-tests.js",
"coverage": "c8 --reporter=text --reporter=html npm test",
"prettier": "prettier --write .",
Expand Down
123 changes: 91 additions & 32 deletions test/test-spec-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* http://opensource.org/licenses/BSD-3-Clause
*/

const fs = require('node:fs/promises');
const fs = require("node:fs/promises");
const SourceMapConsumer =
require("../lib/source-map-consumer").SourceMapConsumer;

Expand Down Expand Up @@ -50,7 +50,7 @@ const skippedTests = [
"ignoreListWrongType2",
"ignoreListWrongType3",
"ignoreListOutOfBounds",
]
];

// The source-map library converts null sources to the "null" URL in its
// sources list, so for equality checking we accept this as null.
Expand All @@ -66,76 +66,135 @@ function mapLine(line) {
}

async function testMappingAction(assert, rawSourceMap, action) {
return SourceMapConsumer.with(rawSourceMap, null, (consumer) => {
mappedPosition = consumer.originalPositionFor({
return SourceMapConsumer.with(rawSourceMap, null, consumer => {
let mappedPosition = consumer.originalPositionFor({
line: mapLine(action.generatedLine),
column: action.generatedColumn,
});

assert.equal(mappedPosition.line, mapLine(action.originalLine), `original line didn't match, expected ${mapLine(action.originalLine)} got ${mappedPosition.line}`);
assert.equal(mappedPosition.column, action.originalColumn, `original column didn't match, expected ${action.originalColumn} got ${mappedPosition.column}`);
assert.equal(nullish(mappedPosition.source), action.originalSource, `original source didn't match, expected ${action.originalSource} got ${mappedPosition.source}`);
if (action.mappedName)
assert.equal(mappedPosition.name, action.mappedName, `mapped name didn't match, expected ${action.mappedName} got ${mappedPosition.name}`);
assert.equal(
mappedPosition.line,
mapLine(action.originalLine),
`original line didn't match, expected ${mapLine(
action.originalLine
)} got ${mappedPosition.line}`
);
assert.equal(
mappedPosition.column,
action.originalColumn,
`original column didn't match, expected ${action.originalColumn} got ${mappedPosition.column}`
);
assert.equal(
nullish(mappedPosition.source),
action.originalSource,
`original source didn't match, expected ${action.originalSource} got ${mappedPosition.source}`
);
if (action.mappedName) {
assert.equal(
mappedPosition.name,
action.mappedName,
`mapped name didn't match, expected ${action.mappedName} got ${mappedPosition.name}`
);
}

// When the source is null, a reverse lookup may not make sense
// because there isn't a unique way to look it up.
if (action.originalSource !== null) {
let mappedPosition = consumer.generatedPositionFor({
mappedPosition = consumer.generatedPositionFor({
source: action.originalSource,
line: mapLine(action.originalLine),
column: action.originalColumn
column: action.originalColumn,
});

assert.equal(mappedPosition.line, mapLine(action.generatedLine), `generated line didn't match, expected ${mapLine(action.generatedLine)} got ${mappedPosition.line}`);
assert.equal(mappedPosition.column, action.generatedColumn, `generated column didn't match, expected ${action.generatedColumn} got ${mappedPosition.column}`);
assert.equal(
mappedPosition.line,
mapLine(action.generatedLine),
`generated line didn't match, expected ${mapLine(
action.generatedLine
)} got ${mappedPosition.line}`
);
assert.equal(
mappedPosition.column,
action.generatedColumn,
`generated column didn't match, expected ${action.generatedColumn} got ${mappedPosition.column}`
);
}

});
}

async function testTransitiveMappingAction(assert, rawSourceMap, action) {
return SourceMapConsumer.with(rawSourceMap, null, async (consumer) => {
assert.ok(Array.isArray(action.intermediateMaps), "transitive mapping case requires intermediate maps");
return SourceMapConsumer.with(rawSourceMap, null, async consumer => {
assert.ok(
Array.isArray(action.intermediateMaps),
"transitive mapping case requires intermediate maps"
);

let mappedPosition = consumer.originalPositionFor({
line: mapLine(action.generatedLine),
column: action.generatedColumn,
});

for (const intermediateMapPath of action.intermediateMaps) {
const intermediateMap = await readJSON(`./source-map-tests/resources/${intermediateMapPath}`);
await SourceMapConsumer.with(intermediateMap, null, (consumer) => {
mappedPosition = consumer.originalPositionFor({
line: mappedPosition.line,
column: mappedPosition.column,
});
});
const intermediateMap = await readJSON(
`./source-map-tests/resources/${intermediateMapPath}`
);
await SourceMapConsumer.with(
intermediateMap,
null,
consumerIntermediate => {
mappedPosition = consumerIntermediate.originalPositionFor({
line: mappedPosition.line,
column: mappedPosition.column,
});
}
);
}

assert.equal(mappedPosition.line, mapLine(action.originalLine), `original line didn't match, expected ${mapLine(action.originalLine)} got ${mappedPosition.line}`);
assert.equal(mappedPosition.column, action.originalColumn, `original column didn't match, expected ${action.originalColumn} got ${mappedPosition.column}`);
assert.equal(mappedPosition.source, action.originalSource, `original source didn't match, expected ${action.originalSource} got ${mappedPosition.source}`);
assert.equal(
mappedPosition.line,
mapLine(action.originalLine),
`original line didn't match, expected ${mapLine(
action.originalLine
)} got ${mappedPosition.line}`
);
assert.equal(
mappedPosition.column,
action.originalColumn,
`original column didn't match, expected ${action.originalColumn} got ${mappedPosition.column}`
);
assert.equal(
mappedPosition.source,
action.originalSource,
`original source didn't match, expected ${action.originalSource} got ${mappedPosition.source}`
);
});
}

for (const testCase of sourceMapSpecTests.tests) {
if (skippedTests.includes(testCase.name))
if (skippedTests.includes(testCase.name)) {
continue;
}
exports[`test from source map spec tests, name: ${testCase.name}`] =
async function (assert) {
const json = await readJSON(`./source-map-tests/resources/${testCase.sourceMapFile}`);
const json = await readJSON(
`./source-map-tests/resources/${testCase.sourceMapFile}`
);
try {
const map = await new SourceMapConsumer(json);
map.eachMapping(() => {});
map.destroy();
} catch (exn) {
if (testCase.sourceMapIsValid)
assert.fail("Expected valid source map but failed to load successfully: " + exn.message);
if (testCase.sourceMapIsValid) {
assert.fail(
"Expected valid source map but failed to load successfully: " +
exn.message
);
}
return;
}
if (!testCase.sourceMapIsValid)
if (!testCase.sourceMapIsValid) {
assert.fail("Expected invalid source map but loaded successfully");
}
if (testCase.testActions) {
for (const testAction of testCase.testActions) {
if (testAction.actionType == "checkMapping") {
Expand All @@ -146,4 +205,4 @@ for (const testCase of sourceMapSpecTests.tests) {
}
}
};
};
}

0 comments on commit 64050e6

Please sign in to comment.