|
| 1 | +"use strict" |
| 2 | + |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | +// Requirements |
| 5 | +//------------------------------------------------------------------------------ |
| 6 | + |
| 7 | +const assert = require("assert") |
| 8 | +const fs = require("fs") |
| 9 | +const path = require("path") |
| 10 | +const parser = require("..") |
| 11 | + |
| 12 | +//------------------------------------------------------------------------------ |
| 13 | +// Helpers |
| 14 | +//------------------------------------------------------------------------------ |
| 15 | + |
| 16 | +const ROOT = path.join(__dirname, "fixtures/document-fragment") |
| 17 | +const TARGETS = fs.readdirSync(ROOT) |
| 18 | +const PARSER_OPTIONS = { |
| 19 | + comment: true, |
| 20 | + ecmaVersion: 6, |
| 21 | + loc: true, |
| 22 | + range: true, |
| 23 | + tokens: true, |
| 24 | + sourceType: "module", |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Remove `parent` proeprties from the given AST. |
| 29 | + * @param {string} key The key. |
| 30 | + * @param {any} value The value of the key. |
| 31 | + * @returns {any} The value of the key to output. |
| 32 | + */ |
| 33 | +function replacer(key, value) { |
| 34 | + if (key === "parent") { |
| 35 | + return undefined |
| 36 | + } |
| 37 | + if (key === "errors" && Array.isArray(value)) { |
| 38 | + return value.map(e => ({ |
| 39 | + message: e.message, |
| 40 | + index: e.index, |
| 41 | + lineNumber: e.lineNumber, |
| 42 | + column: e.column, |
| 43 | + })) |
| 44 | + } |
| 45 | + return value |
| 46 | +} |
| 47 | + |
| 48 | +//------------------------------------------------------------------------------ |
| 49 | +// Main |
| 50 | +//------------------------------------------------------------------------------ |
| 51 | + |
| 52 | +describe("services.getDocumentFragment", () => { |
| 53 | + for (const name of TARGETS) { |
| 54 | + const sourceFileName = fs |
| 55 | + .readdirSync(path.join(ROOT, name)) |
| 56 | + .find(f => f.startsWith("source.")) |
| 57 | + const sourcePath = path.join(ROOT, `${name}/${sourceFileName}`) |
| 58 | + const source = fs.readFileSync(sourcePath, "utf8") |
| 59 | + const result = parser.parseForESLint( |
| 60 | + source, |
| 61 | + Object.assign({ filePath: sourcePath }, PARSER_OPTIONS) |
| 62 | + ) |
| 63 | + const actual = result.services.getDocumentFragment() |
| 64 | + |
| 65 | + describe(`'test/fixtures/document-fragment/${name}/${sourceFileName}'`, () => { |
| 66 | + it("should be parsed to valid document fragment.", () => { |
| 67 | + const resultPath = path.join( |
| 68 | + ROOT, |
| 69 | + `${name}/document-fragment.json` |
| 70 | + ) |
| 71 | + const expected = fs.readFileSync(resultPath, "utf8") |
| 72 | + |
| 73 | + assert.strictEqual( |
| 74 | + JSON.stringify(actual, replacer, 4), |
| 75 | + expected |
| 76 | + ) |
| 77 | + }) |
| 78 | + }) |
| 79 | + } |
| 80 | +}) |
0 commit comments