UNPKG

@textlint/ast-tester

Version:

Compliance tests for textlint's AST(Abstract Syntax Tree).

162 lines 5.97 kB
// LICENSE : MIT "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.test = exports.isTxtAST = void 0; const assert = __importStar(require("assert")); const unist_test_1 = require("./unist-test"); const debug_1 = __importDefault(require("debug")); const debug = (0, debug_1.default)("textlint/ast-tester"); const createMessage = ({ node, message }) => { return `${message}\n${JSON.stringify(node, null, 4)}`; }; function isTxtAST(node) { try { test(node); } catch (error) { debug("This is not TxtAST", error); return false; } return true; } exports.isTxtAST = isTxtAST; function test(node) { // test unist that is weak. (0, unist_test_1.test)(node); assert.strictEqual(typeof node, "object", createMessage({ message: `invalid node: node should be object`, node })); assert.strictEqual(typeof node.type, "string", createMessage({ message: `invalid type: type should be string`, node })); assert.ok(node.type.length >= 1, createMessage({ message: `invalid type: type is empty string`, node })); assert.doesNotThrow(function () { JSON.parse(JSON.stringify(node)); }, createMessage({ message: `invalid node: node should be serializable`, node })); // children if (node.children !== null && node.children !== undefined) { assert.ok(Array.isArray(node.children), createMessage({ message: `invalid children: children should be an array`, node })); node.children.forEach(test); } // value if (node.value !== null && node.value !== undefined) { assert.strictEqual(typeof node.value, "string", createMessage({ message: `invalid value: value should be string`, node })); } // raw assert.ok(node.raw !== null && node.raw !== undefined, createMessage({ message: `invalid raw: raw is undefined`, node })); assert.strictEqual(typeof node.raw, "string", createMessage({ message: `invalid raw: raw is not string`, node })); // loc const loc = node.loc; assert.ok(loc !== null && loc !== undefined, createMessage({ message: `invalid loc: node.loc is undefined`, node })); assert.strictEqual(typeof loc, "object", createMessage({ message: `invalid loc: node.loc should be object. it should have { start, end } property`, node })); const start = loc.start; const end = loc.end; if (start !== null && start !== undefined) { assert.strictEqual(typeof start, "object"); if (start.line !== null && start.line !== undefined) { assert.strictEqual(typeof start.line, "number"); assert.ok(start.line >= 0); // allow `0` for `null`. } if (start.column !== null && start.column !== undefined) { assert.strictEqual(typeof start.column, "number"); assert.ok(start.column >= 0); // allow `0` for `null`. } if (start.offset !== null && start.offset !== undefined) { assert.strictEqual(typeof start.offset, "number"); assert.ok(start.offset >= 0); } } if (end !== null && end !== undefined) { assert.strictEqual(typeof end, "object"); if (end.line !== null && end.line !== undefined) { assert.strictEqual(typeof end.line, "number"); assert.ok(end.line >= 0); // allow `0` for `null`. } if (end.column !== null && end.column !== undefined) { assert.strictEqual(typeof end.column, "number"); assert.ok(end.column >= 0); // allow `0` for `null`. } if (end.offset !== null && end.offset !== undefined) { assert.strictEqual(typeof end.offset, "number"); assert.ok(end.offset >= 0); } } // range const range = node.range; assert.ok(range !== null && range !== undefined, createMessage({ message: `invalid range: range should be an array`, node })); assert.ok(Array.isArray(range), createMessage({ message: `invalid range: range should be an array`, node })); range.forEach(function (index) { assert.strictEqual(typeof index, "number", createMessage({ message: `invalid index: index should be number`, node })); assert.ok(index >= 0, createMessage({ message: `invalid index: index >= 0`, node })); }); assert.ok(range[0] <= range[1], createMessage({ message: `invalid range: range[0] should be less than range[1]`, node })); } exports.test = test; //# sourceMappingURL=index.js.map