release: WRNexusJS 0.2.77
This commit is contained in:
@@ -173,6 +173,71 @@ class Lexer {
|
||||
v += src[this.pos++];
|
||||
return v.trim();
|
||||
}
|
||||
readPropInitializer() {
|
||||
const { src } = this;
|
||||
let value = "";
|
||||
let square = 0;
|
||||
let brace = 0;
|
||||
let paren = 0;
|
||||
let quote = null;
|
||||
const beginsPropDeclaration = (position) => {
|
||||
let cursor = position;
|
||||
while (cursor < src.length && (src[cursor] === " " || src[cursor] === "\t"))
|
||||
cursor++;
|
||||
if (!isIdentStart(src[cursor] ?? ""))
|
||||
return false;
|
||||
cursor++;
|
||||
while (cursor < src.length && isIdentPart(src[cursor]))
|
||||
cursor++;
|
||||
while (cursor < src.length && (src[cursor] === " " || src[cursor] === "\t"))
|
||||
cursor++;
|
||||
return src[cursor] === "=" || src[cursor] === ":";
|
||||
};
|
||||
while (this.pos < src.length) {
|
||||
const c = src[this.pos];
|
||||
if (quote) {
|
||||
value += c;
|
||||
this.pos++;
|
||||
if (c === "\\" && this.pos < src.length)
|
||||
value += src[this.pos++];
|
||||
else if (c === quote)
|
||||
quote = null;
|
||||
continue;
|
||||
}
|
||||
if (c === '"' || c === "'" || c === "`") {
|
||||
quote = c;
|
||||
value += c;
|
||||
this.pos++;
|
||||
continue;
|
||||
}
|
||||
if (c === "[")
|
||||
square++;
|
||||
else if (c === "]" && square > 0)
|
||||
square--;
|
||||
else if (c === "{")
|
||||
brace++;
|
||||
else if (c === "}" && brace > 0)
|
||||
brace--;
|
||||
else if (c === "(")
|
||||
paren++;
|
||||
else if (c === ")" && paren > 0)
|
||||
paren--;
|
||||
const topLevel = square === 0 && brace === 0 && paren === 0;
|
||||
if (topLevel) {
|
||||
if (c === `
|
||||
` || c === "\r" || c === "}")
|
||||
break;
|
||||
if ((c === " " || c === "\t") && beginsPropDeclaration(this.pos))
|
||||
break;
|
||||
}
|
||||
value += c;
|
||||
this.pos++;
|
||||
}
|
||||
const result = value.trim();
|
||||
if (!result)
|
||||
throw new LexError(`Expected a prop initializer at offset ${this.pos}`);
|
||||
return result;
|
||||
}
|
||||
readTypeAnnotation() {
|
||||
const { src } = this;
|
||||
let value = "";
|
||||
@@ -471,7 +536,7 @@ function parse(source) {
|
||||
expect("eq");
|
||||
hasDefault = true;
|
||||
}
|
||||
const defaultValue = hasDefault ? lx.readToLineEnd() : "undefined";
|
||||
const defaultValue = hasDefault ? lx.readPropInitializer() : "undefined";
|
||||
props.push({ name: pName, valueType, required: !hasDefault, default: defaultValue });
|
||||
}
|
||||
expect("rbrace");
|
||||
|
||||
@@ -17,6 +17,91 @@ const VOID_ELEMENTS = new Set([
|
||||
"wbr",
|
||||
]);
|
||||
|
||||
function splitPropDeclarations(value) {
|
||||
const declarations = [];
|
||||
let start = 0;
|
||||
let index = 0;
|
||||
let quote = null;
|
||||
let escaped = false;
|
||||
let square = 0;
|
||||
let brace = 0;
|
||||
let paren = 0;
|
||||
|
||||
const isIdentifierStart = (character) => /[A-Za-z_]/.test(character || "");
|
||||
const isIdentifierPart = (character) => /[A-Za-z0-9_]/.test(character || "");
|
||||
|
||||
const beginsDeclaration = (position) => {
|
||||
let cursor = position;
|
||||
while (cursor < value.length && /[ \t]/.test(value[cursor])) cursor += 1;
|
||||
if (!isIdentifierStart(value[cursor])) return false;
|
||||
cursor += 1;
|
||||
while (cursor < value.length && isIdentifierPart(value[cursor])) cursor += 1;
|
||||
while (cursor < value.length && /[ \t]/.test(value[cursor])) cursor += 1;
|
||||
return value[cursor] === "=" || value[cursor] === ":";
|
||||
};
|
||||
|
||||
while (index < value.length) {
|
||||
const character = value[index];
|
||||
|
||||
if (quote !== null) {
|
||||
if (escaped) escaped = false;
|
||||
else if (character === "\\") escaped = true;
|
||||
else if (character === quote) quote = null;
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (character === '"' || character === "'" || character === "`") {
|
||||
quote = character;
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (character === "[") square += 1;
|
||||
else if (character === "]" && square > 0) square -= 1;
|
||||
else if (character === "{") brace += 1;
|
||||
else if (character === "}" && brace > 0) brace -= 1;
|
||||
else if (character === "(") paren += 1;
|
||||
else if (character === ")" && paren > 0) paren -= 1;
|
||||
|
||||
if (
|
||||
square === 0 &&
|
||||
brace === 0 &&
|
||||
paren === 0 &&
|
||||
/\s/.test(character) &&
|
||||
beginsDeclaration(index)
|
||||
) {
|
||||
const declaration = value.slice(start, index).trim();
|
||||
if (declaration) declarations.push(declaration);
|
||||
while (index < value.length && /\s/.test(value[index])) index += 1;
|
||||
start = index;
|
||||
continue;
|
||||
}
|
||||
|
||||
index += 1;
|
||||
}
|
||||
|
||||
const declaration = value.slice(start).trim();
|
||||
if (declaration) declarations.push(declaration);
|
||||
return declarations;
|
||||
}
|
||||
|
||||
function formatInlinePropsBlock(value, unit, depth) {
|
||||
const match = /^props\s*\{([\s\S]*)\}$/.exec(value.trim());
|
||||
if (!match) return null;
|
||||
|
||||
const declarations = splitPropDeclarations(match[1].trim());
|
||||
if (declarations.length === 0) {
|
||||
return [`${unit.repeat(depth)}props {`, `${unit.repeat(depth)}}`];
|
||||
}
|
||||
|
||||
return [
|
||||
`${unit.repeat(depth)}props {`,
|
||||
...declarations.map((declaration) => `${unit.repeat(depth + 1)}${declaration}`),
|
||||
`${unit.repeat(depth)}}`,
|
||||
];
|
||||
}
|
||||
|
||||
function findOpeningTagEnd(value) {
|
||||
let quote = null;
|
||||
let escaped = false;
|
||||
@@ -347,6 +432,13 @@ function formatWrn(source, options = {}) {
|
||||
index = collected.endIndex;
|
||||
}
|
||||
|
||||
const inlineProps = formatInlinePropsBlock(value, unit, codeDepth + htmlDepth);
|
||||
if (inlineProps) {
|
||||
output.push(...inlineProps);
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const leadingClosingBraces = countLeadingClosingBraces(value);
|
||||
|
||||
const lineCodeDepth = Math.max(0, codeDepth - leadingClosingBraces);
|
||||
@@ -405,4 +497,5 @@ module.exports = {
|
||||
formatWrn,
|
||||
parseAttributes,
|
||||
parseOpeningTag,
|
||||
splitPropDeclarations,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user