eslint-plugin-zod-to-openapi
Advanced tools
Comparing version 0.0.22 to 0.0.23
@@ -7,4 +7,13 @@ "use strict"; | ||
const type_1 = require("../../util/type"); | ||
const commentRegex = /\*\n\s+\* (.*)\n/; | ||
const deprecatedTag = '@deprecated'; | ||
const commentRegex = /[\*\n\s]+(.*)[\*\s]+(.*)/; | ||
const DEPRECATED_TAG = '@deprecated'; | ||
const EXAMPLE_TAG = '@example'; | ||
const getCommentValue = (comment) => { | ||
const match = commentRegex.exec(comment); | ||
const commentValue = match?.[1]; | ||
const exampleValue = match?.[2]; | ||
return commentValue | ||
? `${commentValue}${exampleValue ? `\n${exampleValue}` : ''}` | ||
: undefined; | ||
}; | ||
const getCommentNode = (node) => { | ||
@@ -22,7 +31,12 @@ if (node.parent?.type === 'ExportNamedDeclaration') { | ||
exports.getComment = getComment; | ||
const createCommentValue = (contents, deprecated) => `${deprecated ? `${deprecatedTag} ` : ''}${contents}`; | ||
const createCommentValue = (contents, deprecated, example) => { | ||
const deprecatedValue = deprecated ? `${DEPRECATED_TAG} ` : ''; | ||
const exampleValue = example ? `\n${EXAMPLE_TAG} ${example}` : ''; | ||
return `${deprecatedValue}${contents}${exampleValue}`; | ||
}; | ||
const createFormattedComment = (contents, loc, newline) => { | ||
const lines = contents.split('\n'); | ||
const indent = ' '.repeat(loc.start.column); | ||
return `${newline ? `\n${indent}` : ''}/** | ||
${indent} * ${contents} | ||
${lines.map((line) => `${indent} * ${line}`).join('\n')} | ||
${indent} */ | ||
@@ -56,8 +70,4 @@ ${indent}`; | ||
property.key.type === 'Identifier' && | ||
property.key.name === key && | ||
property.value.type === 'Literal') { | ||
return { | ||
property, | ||
value: property.value.value, | ||
}; | ||
property.key.name === key) { | ||
return property; | ||
} | ||
@@ -67,2 +77,30 @@ } | ||
}; | ||
const getExampleValue = (properties, context) => { | ||
const examples = getPropertyNode(properties, 'examples'); | ||
const example = getPropertyNode(properties, 'example'); | ||
if (examples) { | ||
if (examples.value.type !== 'ArrayExpression') { | ||
// This should always be an array if not ts compiler will complain | ||
return; | ||
} | ||
// Because grabbing a value is difficult if it is not a literal | ||
const arrayText = context.getSourceCode().getText(examples.value); | ||
// Remove square brackets | ||
return arrayText.slice(1, -1); | ||
} | ||
if (example) { | ||
const text = context.getSourceCode().getText(example.value); | ||
return text; | ||
} | ||
return undefined; | ||
}; | ||
const getLiteralValue = (property) => { | ||
if (!property) { | ||
return undefined; | ||
} | ||
if (property.value.type === 'Literal') { | ||
return property.value.value; | ||
} | ||
return undefined; | ||
}; | ||
const getExpectedCommentValue = (node, context) => { | ||
@@ -77,4 +115,4 @@ const openApiCallExpression = (0, traverse_1.findOpenApiCallExpression)(node); | ||
} | ||
const description = getPropertyNode(argument.properties, 'description'); | ||
if (!description) { | ||
const descriptionProperty = getPropertyNode(argument.properties, 'description'); | ||
if (!descriptionProperty) { | ||
return context.report({ | ||
@@ -85,3 +123,3 @@ messageId: 'required', | ||
} | ||
const descriptionValue = description.value; | ||
const descriptionValue = getLiteralValue(descriptionProperty); | ||
if (typeof descriptionValue !== 'string') { | ||
@@ -94,8 +132,9 @@ // would be handled by ts | ||
messageId: 'required', | ||
node: description.property, | ||
node: descriptionProperty, | ||
}); | ||
} | ||
const deprecated = getPropertyNode(argument.properties, 'deprecated'); | ||
const deprecatedValue = Boolean(deprecated?.value); | ||
return createCommentValue(descriptionValue, deprecatedValue); | ||
const deprecatedNode = getPropertyNode(argument.properties, 'deprecated'); | ||
const deprecatedValue = Boolean(getLiteralValue(deprecatedNode)); | ||
const exampleValue = getExampleValue(argument.properties, context); | ||
return createCommentValue(descriptionValue, deprecatedValue, exampleValue); | ||
}; | ||
@@ -129,3 +168,3 @@ // eslint-disable-next-line new-cap | ||
} | ||
const commentValue = commentRegex.exec(comment.value)?.[1]; | ||
const commentValue = getCommentValue(comment.value); | ||
if (!commentValue || expectedCommentValue !== commentValue) { | ||
@@ -214,3 +253,3 @@ return context.report({ | ||
} | ||
const commentValue = commentRegex.exec(comment.value)?.[1]; | ||
const commentValue = getCommentValue(comment.value); | ||
if (!commentValue || expectedCommentValue !== commentValue) { | ||
@@ -217,0 +256,0 @@ return context.report({ |
{ | ||
"name": "eslint-plugin-zod-to-openapi", | ||
"version": "0.0.22", | ||
"version": "0.0.23", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "Eslint rules for zod-to-openapi", |
Sorry, the diff of this file is not supported yet
69227
837