Skip to content

Commit

Permalink
docs: update detection of AST specific config properties (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
zOadT committed Feb 24, 2024
1 parent 982bcf6 commit 9924edf
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 54 deletions.
16 changes: 14 additions & 2 deletions website/playground/src/plugins/getPluginDefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,22 @@ export async function getPluginDefaultConfig(configSchemaUrl: string, signal: Ab
continue;
}
const property = json.properties[propertyName];
const derivedPropName = property["$ref"]?.replace("#/definitions/", "");

const lastSegment = propertyName.split(".").pop()!;
const astSpecific = (derivedPropName !== propertyName && derivedPropName in json.properties)
|| (lastSegment !== propertyName && lastSegment in json.properties);
if (astSpecific) {
continue;
}

let defaultValue: string | boolean | number | undefined;

if (property["$ref"]) {
defaultValue = json.definitions[propertyName]?.default;
if (derivedPropName) {
const definition = json.definitions[derivedPropName];
if (definition != null) {
defaultValue = definition?.default;
}
} else {
defaultValue = property.default;
}
Expand Down
131 changes: 79 additions & 52 deletions website/src/scripts/plugin-config-table-replacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,8 @@ export function replaceConfigTable() {
propertyTitle.textContent = property.name;
propertyContainer.appendChild(propertyTitle);

// description
const propertyDesc = document.createElement("p");
propertyDesc.textContent = property.description;
propertyContainer.appendChild(propertyDesc);

const infoContainer = document.createElement("ul");
propertyContainer.appendChild(infoContainer);

if (property.oneOf) {
property.oneOf.forEach(function(oneOf) {
const oneOfContainer = document.createElement("li");
infoContainer.appendChild(oneOfContainer);
const prefix = document.createElement("strong");
prefix.textContent = valueToText(oneOf.const);
oneOfContainer.appendChild(prefix);
if (oneOf.description != null && oneOf.description.length > 0) {
oneOfContainer.append(" - " + oneOf.description);
}
if (oneOf.const === property.default) {
oneOfContainer.append(" (Default)");
}
});
} else {
// type
const typeContainer = document.createElement("li");
infoContainer.appendChild(typeContainer);
const typePrefix = document.createElement("strong");
typePrefix.textContent = "Type: ";
typeContainer.appendChild(typePrefix);
typeContainer.append(property.type);

// default
const defaultContainer = document.createElement("li");
infoContainer.appendChild(defaultContainer);
const defaultPrefix = document.createElement("strong");
defaultPrefix.textContent = "Default: ";
defaultContainer.appendChild(defaultPrefix);
defaultContainer.append(valueToText(property.default));
}
addDescription(propertyContainer, property);
addInfoContainer(propertyContainer, property);

if (property.astSpecificProperties != null && property.astSpecificProperties.length > 0) {
const astSpecificPropertiesPrefix = document.createElement("p");
Expand All @@ -66,9 +29,22 @@ export function replaceConfigTable() {
const astSpecificPropertyNamesContainer = document.createElement("ul");
propertyContainer.appendChild(astSpecificPropertyNamesContainer);

property.astSpecificProperties.forEach(function(propName) {
property.astSpecificProperties.forEach(function({ propertyName, definition }) {
const propertyNameLi = document.createElement("li");
propertyNameLi.textContent = valueToText(propName);

const labelSpan = document.createElement("span");
labelSpan.textContent = valueToText(propertyName);
propertyNameLi.appendChild(labelSpan);

if (definition != null) {
const definitionDiv = document.createElement("div");
if (definition.description !== property.description) {
addDescription(definitionDiv, definition);
}
addInfoContainer(definitionDiv, definition);
propertyNameLi.appendChild(definitionDiv);
}

astSpecificPropertyNamesContainer.appendChild(propertyNameLi);
});
}
Expand All @@ -81,6 +57,49 @@ export function replaceConfigTable() {
}
});

function addDescription(propertyContainer, property) {
const propertyDesc = document.createElement("p");
propertyDesc.textContent = property.description;
propertyContainer.appendChild(propertyDesc);
}

function addInfoContainer(propertyContainer, property) {
const infoContainer = document.createElement("ul");
propertyContainer.appendChild(infoContainer);

if (property.oneOf) {
property.oneOf.forEach(function(oneOf) {
const oneOfContainer = document.createElement("li");
infoContainer.appendChild(oneOfContainer);
const prefix = document.createElement("strong");
prefix.textContent = valueToText(oneOf.const);
oneOfContainer.appendChild(prefix);
if (oneOf.description != null && oneOf.description.length > 0) {
oneOfContainer.append(" - " + oneOf.description);
}
if (oneOf.const === property.default) {
oneOfContainer.append(" (Default)");
}
});
} else {
// type
const typeContainer = document.createElement("li");
infoContainer.appendChild(typeContainer);
const typePrefix = document.createElement("strong");
typePrefix.textContent = "Type: ";
typeContainer.appendChild(typePrefix);
typeContainer.append(property.type);

// default
const defaultContainer = document.createElement("li");
infoContainer.appendChild(defaultContainer);
const defaultPrefix = document.createElement("strong");
defaultPrefix.textContent = "Default: ";
defaultContainer.appendChild(defaultPrefix);
defaultContainer.append(valueToText(property.default));
}
}

function valueToText(value) {
if (typeof value === "string") {
return "\"" + value + "\"";
Expand Down Expand Up @@ -122,18 +141,26 @@ function getDprintPluginConfig(configSchemaUrl) {
const property = json.properties[propertyName];

if (property["$ref"]) {
const definition = json.definitions[propertyName];
if (definition != null) {
setDefinitionForPropertyName(propertyName, definition);
const derivedPropName = property["$ref"].replace("#/definitions/", "");

const lastSegment = propertyName.split(".").pop();
let parentProperty;
if (derivedPropName !== propertyName && derivedPropName in json.properties) {
parentProperty = derivedPropName;
} else if (lastSegment !== propertyName && lastSegment in json.properties) {
parentProperty = lastSegment;
}

const definition = json.definitions[derivedPropName];
if (parentProperty) {
ensurePropertyName(parentProperty);
const isSameDefinition = property["$ref"] === json.properties[parentProperty]["$ref"];
properties[parentProperty].astSpecificProperties.push({
propertyName,
definition: isSameDefinition ? null : definition,
});
} else {
const derivedPropName = property["$ref"].replace("#/definitions/", "");
if (json.properties[derivedPropName] == null) {
// occurs when the definition doesn't have a corresponding property
setDefinitionForPropertyName(propertyName, json.definitions[derivedPropName]);
} else {
ensurePropertyName(derivedPropName);
properties[derivedPropName].astSpecificProperties.push(propertyName);
}
setDefinitionForPropertyName(propertyName, definition);
}
} else {
ensurePropertyName(propertyName);
Expand Down

0 comments on commit 9924edf

Please sign in to comment.