importassert from 'node:assert'; import {createHash} from 'node:crypto'; import {mkdtemp, readFile, rm, writeFile} from 'node:fs/promises'; import {basename, dirname, join, relative, resolve} from 'node:path'; import {test} from 'node:test'; import {pathToFileURL} from 'node:url';
import {transform, type Output} from '@swc/core'; import {parse as parseJs} from 'acorn'; import {parse, type Tag} from 'doctrine'; import {Glob} from 'glob'; import {packageDirectory} from 'package-directory'; import {
SourceMapConsumer,
SourceMapGenerator,
type RawSourceMap,
} from 'source-map'; import yargs from 'yargs'; import {hideBin} from 'yargs/helpers';
// This is 1-indexed. interface Position {
line: number;
column: number;
}
interface ExtractedSourceLocation { // File path to the original source code.
origin: string; // Mappings from the extracted code to the original code.
positions: Array<{ // The 1-indexed line number for the extracted code.
extracted: number; // The position in the original code.
original: Position;
}>;
}
for await (const file of new Glob(files, {})) { void test(file, async context => { const testDirectory = await createTestDirectory(file);
context.after(async () => { if (!process.env['KEEP_TESTS']) {
await rm(testDirectory, {force: true, recursive: true});
}
}); const tests = []; for (const example of await extractJSDocComments(file).then(
extractExampleCode,
)) {
tests.push(
context.test(
`${file}:${example.positions[0]!.original.line}:${
example.positions[0]!.original.column
}`,
async () => {
await run(testDirectory, example);
},
),
);
}
await Promise.all(tests);
});
}
async function createTestDirectory(file: string) { const dir = await packageDirectory({cwd: dirname(file)}); if (!dir) { thrownew Error(`Could not find package root for ${file}.`);
}
async function getExtractSourceMap(
map: string,
generatedFile: string,
location: ExtractedSourceLocation,
) { const sourceMap = JSON.parse(map) as RawSourceMap;
sourceMap.file = basename(generatedFile);
sourceMap.sourceRoot = '';
sourceMap.sources = [
relative(dirname(generatedFile), resolve(location.origin)),
]; const consumer = await new SourceMapConsumer(sourceMap); const generator = new SourceMapGenerator({
file: consumer.file,
sourceRoot: consumer.sourceRoot,
}); // We want descending order of the `generated` property. const positions = [...location.positions].reverse();
consumer.eachMapping(mapping => { // Note `mapping.originalLine` is the line number with respect to the // extracted, raw code. const {extracted, original} = positions.find(({extracted}) => { return mapping.originalLine >= extracted;
})!;
// `original.line` will account for `extracted`, so we need to subtract // `extracted` to avoid duplicity. We also subtract 1 because `extracted` is // 1-indexed.
mapping.originalLine -= extracted - 1;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.