html-validate-vue-webpack-plugin
Version:
extract html-validate rules from Vue single file components
113 lines (112 loc) • 4.64 kB
JavaScript
;
/* eslint-disable security/detect-object-injection -- technically could be a problem and should be mitigated */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHtmlValidateSlotRules = exports.getHtmlValidateRootRules = exports.getHtmlValidateSlotNames = exports.getHtmlValidateContent = exports.getComponentName = exports.getFileName = exports.toKebabCase = exports.prettify = void 0;
/**
* Wrapper for `JSON.stringify(value, null, 2);`.
* @param value - the value to prettify
*/
function prettify(value) {
return JSON.stringify(value, null, 2);
}
exports.prettify = prettify;
/**
* Takes a value and converts it to kebab-case format.
* @param value - the value to convert
*/
function toKebabCase(value) {
return value
.replace(/([A-Z])([A-Z])/g, "$1-$2")
.replace(/([a-z])([A-Z])/g, "$1-$2")
.replace(/[\s_]+/g, "-")
.toLowerCase();
}
exports.toKebabCase = toKebabCase;
/**
* Takes a file path and returns the file name from the end of the file path.
* @param filePath - the file path, i.e. example/file.vue or file.vue
*/
function getFileName(filePath) {
const lastIndexOfSlash = filePath.lastIndexOf("/");
const lastIndexOfDot = filePath.lastIndexOf(".");
return filePath.slice(lastIndexOfSlash + 1, lastIndexOfDot === -1 ? undefined : lastIndexOfDot);
}
exports.getFileName = getFileName;
/**
* Takes a source and returns the value of the name property.
* If no name property is found it will return an empty string.
* @param source - the content with a Vue SFC, i.e.
* ```vue
* <script>
* export default { name: 'Example' };
* </script>
* ```
*/
function getComponentName(source) {
var _a;
const regexp = new RegExp("name:\\s*[\"']?(\\w+)[\"']?");
const result = (_a = regexp.exec(source)) !== null && _a !== void 0 ? _a : [];
const [, componentName = ""] = result;
return componentName;
}
exports.getComponentName = getComponentName;
/**
* Takes a source and returns the content of the `<htmlvalidate>{}</htmlvalidate>` block.
* If no content is found it will return an empty string object `"{}"`.
* @param source - the content with a htmlvalidate block, i.e.
* ```vue
* <htmlvalidate>
* {}
* </htmlvalidate>
* ```
*/
function getHtmlValidateContent(source) {
var _a;
const regexp = new RegExp("<htmlvalidate>((?:[\\n\\t\\r]|.)+)</htmlvalidate>");
const result = (_a = regexp.exec(source)) !== null && _a !== void 0 ? _a : [];
const [, block = "{}"] = result;
return block;
}
exports.getHtmlValidateContent = getHtmlValidateContent;
/**
* Takes html validate content and returns all slot names.
* Slot names are prefixed with either `#` or `:`.
* If no slot names are found it will return an empty array.
* @param htmlValidateContent - @see getHtmlValidateContent
*/
function getHtmlValidateSlotNames(htmlValidateContent) {
var _a;
const regexp = new RegExp("(#|:)([\\w-]+)", "g");
return (_a = htmlValidateContent.match(regexp)) !== null && _a !== void 0 ? _a : [];
}
exports.getHtmlValidateSlotNames = getHtmlValidateSlotNames;
/**
* Takes html validate content and returns all the rules that should be in the root, i.e. no slot rules.
* @param htmlValidateContent - @see getHtmlValidateContent
* @param componentName - the name of the component
*/
function getHtmlValidateRootRules(htmlValidateContent, componentName) {
const htmlValidateContentParsed = JSON.parse(htmlValidateContent);
const slotNames = getHtmlValidateSlotNames(htmlValidateContent);
slotNames === null || slotNames === void 0 ? void 0 : slotNames.forEach((slotName) => delete htmlValidateContentParsed[slotName]);
const rootRules = {};
rootRules[componentName] = htmlValidateContentParsed;
return rootRules;
}
exports.getHtmlValidateRootRules = getHtmlValidateRootRules;
/**
* Takes html validate content and returns all the rules that are considered as slot rules.
* @param htmlValidateContent - @see getHtmlValidateContent
* @param componentName - the name of the component
*/
function getHtmlValidateSlotRules(htmlValidateContent, componentName) {
const htmlValidateContentParsed = JSON.parse(htmlValidateContent);
const slotNames = getHtmlValidateSlotNames(htmlValidateContent);
const slotRules = {};
slotNames === null || slotNames === void 0 ? void 0 : slotNames.forEach((slotName) => {
var _a;
slotRules[`${componentName}${slotName}`] = (_a = htmlValidateContentParsed[slotName]) !== null && _a !== void 0 ? _a : {};
});
return slotRules;
}
exports.getHtmlValidateSlotRules = getHtmlValidateSlotRules;