Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 60 additions & 15 deletions napi/parser/test/parse-raw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import { describe, expect, it } from 'vitest';

import { parseSync } from '../index.js';

const TARGET_DIR_PATH = pathJoin(import.meta.dirname, '../../../target');
const TEST262_DIR_PATH = pathJoin(import.meta.dirname, '../../../tasks/coverage/test262/test');
const ACORN_TEST262_DIR_PATH = pathJoin(import.meta.dirname, '../../../tasks/coverage/acorn-test262/test');
const ESTREE_SNAPSHOT_PATH = pathJoin(import.meta.dirname, '../../../tasks/coverage/snapshots/estree_test262.snap');
const ROOT_DIR = pathJoin(import.meta.dirname, '../../..');
const TARGET_DIR_PATH = pathJoin(ROOT_DIR, 'target');
const TEST262_SHORT_DIR_PATH = 'tasks/coverage/test262/test';
const TEST262_DIR_PATH = pathJoin(ROOT_DIR, TEST262_SHORT_DIR_PATH);
const ACORN_TEST262_DIR_PATH = pathJoin(ROOT_DIR, 'tasks/coverage/acorn-test262/test');
const JSX_SHORT_DIR_PATH = 'tasks/coverage/acorn-test262/test-acorn-jsx/pass';
const JSX_DIR_PATH = pathJoin(ROOT_DIR, JSX_SHORT_DIR_PATH);
const TEST262_SNAPSHOT_PATH = pathJoin(ROOT_DIR, 'tasks/coverage/snapshots/estree_test262.snap');
const JSX_SNAPSHOT_PATH = pathJoin(ROOT_DIR, 'tasks/coverage/snapshots/estree_acorn_jsx.snap');

const INFINITY_PLACEHOLDER = '__INFINITY__INFINITY__INFINITY__';
const INFINITY_REGEXP = new RegExp(`"${INFINITY_PLACEHOLDER}"`, 'g');
Expand Down Expand Up @@ -46,26 +51,20 @@ const benchFixtures = await Promise.all(benchFixtureUrls.map(async (url) => {
return [filename, sourceText];
}));

// Test raw transfer output matches JSON snapshots for Test262 test cases.
//
// Only test Test262 fixtures which Acorn is able to parse.
// Skip tests which we know we can't pass (listed as failing in `ESTree` snapshot file),
// Skip tests which we know we can't pass (listed as failing in `estree_test262.snap` snapshot file),
// and skip tests related to hashbangs (where output is correct, but Acorn doesn't parse hashbangs).
const SNAPSHOT_FAIL_PREFIX = 'Mismatch: tasks/coverage/test262/test/';
const snapshotFailPaths = new Set(
(await readFile(ESTREE_SNAPSHOT_PATH, 'utf8'))
.split('\n')
.filter(line => line.startsWith(SNAPSHOT_FAIL_PREFIX))
.map(line => line.slice(SNAPSHOT_FAIL_PREFIX.length)),
);

const test262FailPaths = await getTestFailurePaths(TEST262_SNAPSHOT_PATH, TEST262_SHORT_DIR_PATH);
const test262FixturePaths = [];
for (let path of await readdir(ACORN_TEST262_DIR_PATH, { recursive: true })) {
if (!path.endsWith('.json')) continue;
path = path.slice(0, -2);
if (snapshotFailPaths.has(path) || path.startsWith('language/comments/hashbang/')) continue;
if (test262FailPaths.has(path) || path.startsWith('language/comments/hashbang/')) continue;
test262FixturePaths.push(path);
}

// Test raw transfer output matches standard (via JSON) output for Test262 test cases
describe('test262', () => {
it.each(test262FixturePaths)('%s', async (path) => {
const filename = basename(path);
Expand All @@ -90,6 +89,39 @@ describe('test262', () => {
});
});

// Test raw transfer output matches JSON snapshots for Acorn-JSX test cases.
//
// Only test Acorn-JSX fixtures which Acorn is able to parse.
// Skip tests which we know we can't pass (listed as failing in `estree_acron_jsx.snap` snapshot file).
const jsxFailPaths = await getTestFailurePaths(JSX_SNAPSHOT_PATH, JSX_SHORT_DIR_PATH);
const jsxFixturePaths = (await readdir(JSX_DIR_PATH, { recursive: true }))
.filter(path => path.endsWith('.jsx') && !jsxFailPaths.has(path));

describe('JSX', () => {
it.each(jsxFixturePaths)('%s', async (filename) => {
const sourcePath = pathJoin(JSX_DIR_PATH, filename),
jsonPath = sourcePath.slice(0, -1) + 'on'; // `.jsx` -> `.json`
const [sourceText, acornJson] = await Promise.all([
readFile(sourcePath, 'utf8'),
readFile(jsonPath, 'utf8'),
]);

// Acorn JSON files always end with:
// ```
// "sourceType": "script",
// "hashbang": null
// }
// ```
// For speed, extract `sourceType` with a slice, rather than parsing the JSON.
const sourceType = acornJson.slice(-29, -23);

// @ts-ignore
const { program } = parseSync(filename, sourceText, { sourceType, experimentalRawTransfer: true });
const json = stringifyAcornTest262Style(program);
expect(json).toEqual(acornJson);
});
});

// Test raw transfer output matches standard (via JSON) output for edge cases not covered by Test262
describe('edge cases', () => {
it.each([
Expand Down Expand Up @@ -142,6 +174,19 @@ function assertRawAndStandardMatch(filename, sourceText) {
expect(jsonRaw).toEqual(jsonStandard);
}

// Get `Set` containing test paths which failed from snapshot file
async function getTestFailurePaths(snapshotPath, pathPrefix) {
const mismatchPrefix = `Mismatch: ${pathPrefix}/`,
mismatchPrefixLen = mismatchPrefix.length;

const snapshot = await readFile(snapshotPath, 'utf8');
return new Set(
snapshot.split('\n')
.filter(line => line.startsWith(mismatchPrefix))
.map(line => line.slice(mismatchPrefixLen)),
);
}

// Stringify to JSON, removing values which are invalid in JSON
function stringify(obj) {
return JSON.stringify(obj, (_key, value) => {
Expand Down
Loading