diff --git a/prettier/configuration/jest.config.cjs b/prettier/configuration/jest.config.cjs deleted file mode 100644 index f3cf9d6..0000000 --- a/prettier/configuration/jest.config.cjs +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - preset: "ts-jest", - setupFilesAfterEnv: ["./configuration/run_spec.js"], - testEnvironment: "node", - testRegex: "run_tests.js$", - rootDir: "..", - transform: {}, -}; diff --git a/prettier/configuration/raw-serializer.js b/prettier/configuration/raw-serializer.js deleted file mode 100644 index 5c44edb..0000000 --- a/prettier/configuration/raw-serializer.js +++ /dev/null @@ -1,17 +0,0 @@ -// source: https://github.com/prettier/prettier/blob/ee2839bacbf6a52d004fa2f0373b732f6f191ccc/tests_config/raw-serializer.js -"use strict"; - -const RAW = Symbol.for("raw"); - -module.exports = { - print(val) { - return val[RAW]; - }, - test(val) { - return ( - val && - Object.prototype.hasOwnProperty.call(val, RAW) && - typeof val[RAW] === "string" - ); - }, -}; diff --git a/prettier/configuration/run_spec.js b/prettier/configuration/run_spec.js deleted file mode 100644 index 3f38fcd..0000000 --- a/prettier/configuration/run_spec.js +++ /dev/null @@ -1,117 +0,0 @@ -// source: https://github.com/prettier/prettier/blob/ee2839bacbf6a52d004fa2f0373b732f6f191ccc/tests_config/run_spec.js -"use strict"; - -import fs from "fs"; -import path from "path"; -import prettier from "prettier"; -import { fileURLToPath } from "url"; -import { expect, test } from "@jest/globals"; - -const testDirectory = path.dirname(fileURLToPath(import.meta.url)); - -const RAW = Symbol.for("raw"); -expect.addSnapshotSerializer({ - print(val) { - return val[RAW]; - }, - test(val) { - return ( - val && - Object.prototype.hasOwnProperty.call(val, RAW) && - typeof val[RAW] === "string" - ); - }, -}); - -async function run_spec(dirname, options) { - fs.readdirSync(dirname).forEach(async (filename) => { - const filepath = `${dirname}${filename}`; - if ( - path.extname(filename) !== ".snap" && - fs.lstatSync(filepath).isFile() && - filename[0] !== "." && - filename !== "run_tests.js" - ) { - let rangeStart = 0; - let rangeEnd = Infinity; - let cursorOffset; - const source = read(filepath) - .replace(/\r\n/g, "\n") - .replace("<<>>", (match, offset) => { - rangeStart = offset; - return ""; - }) - .replace("<<>>", (match, offset) => { - rangeEnd = offset; - return ""; - }); - - const input = source.replace("<|>", (match, offset) => { - cursorOffset = offset; - return ""; - }); - - const mergedOptions = Object.assign(mergeDefaultOptions(options || {}), { - filepath, - rangeStart, - rangeEnd, - cursorOffset, - parser: "hubl", - }); - - const output = prettyprint(input, mergedOptions); - - test(filename, async () => { - expect( - raw( - source + - "~".repeat(mergedOptions.printWidth) + - "\n" + - (await output), - ), - ).toMatchSnapshot(); - }); - } - }); -} - -async function prettyprint(src, options) { - const result = await prettier.formatWithCursor(src, options); - if (options.cursorOffset >= 0) { - result.formatted = - result.formatted.slice(0, result.cursorOffset) + - "<|>" + - result.formatted.slice(result.cursorOffset); - } - return result.formatted; -} - -global.run_spec = run_spec; - -function read(filename) { - return fs.readFileSync(filename, "utf8"); -} - -/** - * Wraps a string in a marker object that is used by `./raw-serializer.js` to - * directly print that string in a snapshot without escaping all double quotes. - * Backticks will still be escaped. - */ -function raw(string) { - if (typeof string !== "string") { - throw new Error("Raw snapshots have to be strings."); - } - return { [Symbol.for("raw")]: string }; -} - -function mergeDefaultOptions(parserConfig) { - return Object.assign( - { - plugins: [ - path.resolve(path.join(testDirectory, ".."), "dist/src/index.js"), - ], - printWidth: 80, - }, - parserConfig, - ); -} diff --git a/prettier/package.json b/prettier/package.json index 47f7b39..8a8e17d 100644 --- a/prettier/package.json +++ b/prettier/package.json @@ -8,7 +8,7 @@ "scripts": { "build": "rm -rf ./dist && tsc", "start": "nodemon --config ./configuration/nodemon.inline.json", - "test": "node --experimental-vm-modules ../node_modules/jest/bin/jest.js -c ./configuration/jest.config.cjs", + "test": "node --experimental-vm-modules ../node_modules/jest/bin/jest.js -c ./tests/configuration/jest.config.ts", "watch": "nodemon", "update-test": "tsc && jest -c ./configuration/jest.config.js --update-snapshot", "test-inline": "tsc && node ./src/test-inline.ts" diff --git a/prettier/tests/__snapshots__/run_tests.js.snap b/prettier/tests/__snapshots__/run_tests.js.snap index 2087c51..40449d1 100644 --- a/prettier/tests/__snapshots__/run_tests.js.snap +++ b/prettier/tests/__snapshots__/run_tests.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`404.html 1`] = ` +exports[`formats 404.html correctly 1`] = ` @@ -600,7 +600,7 @@ templateType: none `; -exports[`contact.html 1`] = ` +exports[`formats contact.html correctly 1`] = ` @@ -4338,7 +4338,7 @@ This is a simple dialog rendered by using a macro and a call block. `; -exports[`ternary.html 1`] = ` +exports[`formats ternary.html correctly 1`] = ` {{ "x" == 0 ? "hello" : "bye" }} {% set x = blue === red ? yellow : orange %} @@ -4361,7 +4361,7 @@ exports[`ternary.html 1`] = ` `; -exports[`website-header.html 1`] = ` +exports[`formats website-header.html correctly 1`] = `