UNPKG

eslint-mdx

Version:
136 lines 6.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.restoreTokens = void 0; const assert_1 = require("uvu/assert"); const helpers_1 = require("./helpers"); const restoreTokens = (text, root, tokens, tt, visit) => { tokens = [...tokens]; const getPositionAt = (0, helpers_1.getPositionAtFactory)(text); const prevCharOffset = (0, helpers_1.prevCharOffsetFactory)(text); const nextCharOffset = (0, helpers_1.nextCharOffsetFactory)(text); const newToken = (type, start, end, value) => ({ type, value, start, end, loc: { start: getPositionAt(start), end: getPositionAt(end), }, range: [start, end], }); visit(root, node => { var _a; if (node.type !== 'mdxFlowExpression' && node.type !== 'mdxJsxFlowElement' && node.type !== 'mdxJsxTextElement' && node.type !== 'text') { return; } const nodePos = node.position; (0, assert_1.ok)(nodePos); const nodeStart = nodePos.start.offset; const nodeEnd = nodePos.end.offset; const lastCharOffset = prevCharOffset(nodeEnd - 2); (0, assert_1.ok)(nodeStart != null); (0, assert_1.ok)(nodeEnd != null); if (node.type === 'mdxFlowExpression') { tokens.push(newToken(tt.braceL, nodeStart, nodeStart + 1), newToken(tt.braceR, nodeEnd - 1, nodeEnd)); return; } if (node.type === 'text') { tokens.push(newToken(tt.jsxText, nodeStart, nodeEnd, node.value)); return; } tokens.push(newToken(tt.jsxTagStart, nodeStart, nodeStart + 1)); const nodeName = node.name; const nodeNameLength = (_a = nodeName === null || nodeName === void 0 ? void 0 : nodeName.length) !== null && _a !== void 0 ? _a : 0; const selfClosing = text[lastCharOffset] === '/'; let nodeNameStart = nodeStart + 1; if (nodeName) { nodeNameStart = nextCharOffset(nodeStart + 1); (0, assert_1.ok)(nodeNameStart); tokens.push(newToken(tt.jsxName, nodeNameStart, nodeNameStart + nodeNameLength, nodeName)); } let lastAttrOffset = nodeNameStart + nodeNameLength - 1; for (const attr of node.attributes) { if (attr.type === 'mdxJsxExpressionAttribute') { (0, assert_1.ok)(attr.data); (0, assert_1.ok)(attr.data.estree); (0, assert_1.ok)(attr.data.estree.range); let [attrValStart, attrValEnd] = attr.data.estree.range; attrValStart = prevCharOffset(attrValStart - 1); attrValEnd = nextCharOffset(attrValEnd); (0, assert_1.ok)(text[attrValStart] === '{'); (0, assert_1.ok)(text[attrValEnd] === '}'); lastAttrOffset = attrValEnd; tokens.push(newToken(tt.braceL, attrValStart, attrValStart + 1), newToken(tt.braceR, attrValEnd, attrValEnd + 1)); continue; } const attrStart = nextCharOffset(lastAttrOffset + 1); (0, assert_1.ok)(attrStart != null); const attrName = attr.name; const attrNameLength = attrName.length; tokens.push(newToken(tt.jsxName, attrStart, attrStart + attrNameLength, attrName)); const attrValue = attr.value; if (attrValue == null) { lastAttrOffset = attrStart + attrNameLength; continue; } const attrEqualOffset = nextCharOffset(attrStart + attrNameLength); (0, assert_1.ok)(text[attrEqualOffset] === '='); tokens.push(newToken(tt.eq, attrEqualOffset, attrEqualOffset + 1, '=')); if (typeof attrValue === 'object') { const data = attrValue.data; let [attrValStart, attrValEnd] = data.estree.range; attrValStart = prevCharOffset(attrValStart - 1); attrValEnd = nextCharOffset(attrValEnd); (0, assert_1.ok)(text[attrValStart] === '{'); (0, assert_1.ok)(text[attrValEnd] === '}'); lastAttrOffset = attrValEnd; tokens.push(newToken(tt.braceL, attrValStart, attrValStart + 1), newToken(tt.braceR, attrValEnd, attrValEnd + 1)); continue; } const attrQuoteOffset = nextCharOffset(attrEqualOffset + 1); const attrQuote = text[attrQuoteOffset]; (0, assert_1.ok)(attrQuote === '"' || attrQuote === "'"); tokens.push(newToken(tt.string, attrQuoteOffset, attrQuoteOffset + attrValue.length + 2, attrValue)); lastAttrOffset = nextCharOffset(attrQuoteOffset + attrValue.length + 1); (0, assert_1.ok)(text[lastAttrOffset] === attrQuote); } let nextOffset = nextCharOffset(lastAttrOffset + 1); let nextChar = text[nextOffset]; const expectedNextChar = selfClosing ? '/' : '>'; if (nextChar !== expectedNextChar) { nextOffset = nextCharOffset(lastAttrOffset); nextChar = text[nextOffset]; } if (selfClosing) { tokens.push(newToken(tt.slash, nextOffset, nextOffset + 1, '/')); } else { (0, assert_1.ok)(nextChar === '>', `\`nextChar\` must be '>' but actually is '${nextChar}'`); const prevOffset = prevCharOffset(nodeEnd - 2); if (nodeName) { tokens.push(newToken(tt.jsxName, prevOffset + 1 - nodeNameLength, prevOffset + 1, nodeName)); } const slashOffset = prevCharOffset(prevOffset - nodeNameLength); (0, assert_1.ok)(text[slashOffset] === '/'); tokens.push(newToken(tt.slash, slashOffset, slashOffset + 1, '/')); const tagStartOffset = prevCharOffset(slashOffset - 1); (0, assert_1.ok)(text[tagStartOffset] === '<'); tokens.push(newToken(tt.jsxTagStart, tagStartOffset, tagStartOffset + 1)); } tokens.push(newToken(tt.jsxTagEnd, nodeEnd - 1, nodeEnd)); }); tokens.push(newToken(tt.eof, text.length, text.length)); return tokens .filter(t => !(t.start === t.end && (t.type === tt.braceL || t.type === tt.braceR || t.type === tt.parenL || t.type === tt.parenR))) .sort((a, b) => a.start - b.start); }; exports.restoreTokens = restoreTokens; //# sourceMappingURL=tokens.js.map