Skip to content

Commit

Permalink
Handle Shorthand Property (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored Apr 24, 2023
1 parent c5ee426 commit e59af64
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/rules/require-comment/rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,9 @@ ruleTester.run(ruleName, rule, {
...test('string-example-wrong', true),
errors: [{ messageId: 'comment' }],
},
{
...test('object-shorthand-property', true),
errors: [{ messageId: 'comment' }],
},
],
});
21 changes: 21 additions & 0 deletions src/rules/require-comment/tests/object-shorthand-property.fix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { z } from 'zod';
import { extendZodWithOpenApi } from 'zod-openapi';

extendZodWithOpenApi(z);

/**
* some description
*/
const a = z.string().openapi({ description: 'some description' });

/**
* object description
*/
export const ZodObject = z
.object({
/**
* some description
*/
a,
})
.openapi({ description: 'object description' });
18 changes: 18 additions & 0 deletions src/rules/require-comment/tests/object-shorthand-property.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { z } from 'zod';
import { extendZodWithOpenApi } from 'zod-openapi';

extendZodWithOpenApi(z);

/**
* some description
*/
const a = z.string().openapi({ description: 'some description' });

/**
* object description
*/
export const ZodObject = z
.object({
a,
})
.openapi({ description: 'object description' });
31 changes: 30 additions & 1 deletion src/util/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ const getType = <T extends TSESTree.Node>(
};
};

const getFlowNode = (
node: ts.Node,
baseIdentifier: TSESTree.Node,
): ts.Node & { name?: ts.Node } => {
if (
baseIdentifier.parent?.type !== 'Property' ||
!('flowNode' in node) ||
!node.flowNode
) {
return node;
}

const flowNode = node.flowNode;
if (typeof flowNode !== 'object' || !('node' in flowNode) || !flowNode.node) {
return node;
}

return flowNode.node as ts.Node & { name: ts.Node };
};

const getInferredComment = <T extends TSESTree.Node>(
node: T,
context: Readonly<TSESLint.RuleContext<any, any>>,
Expand All @@ -89,13 +109,22 @@ const getInferredComment = <T extends TSESTree.Node>(
return;
}

if (
baseIdentifier.parent?.type === 'Property' &&
baseIdentifier.parent.parent?.type === 'ObjectExpression' &&
baseIdentifier.parent.parent.parent?.type !== 'CallExpression'
) {
return;
}

// 1. Grab the TypeScript program from parser services
const parserServices = ESLintUtils.getParserServices(context);
const checker = parserServices.program.getTypeChecker();

// 2. Find the backing TS node for the ES node, then that TS type
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(baseIdentifier);
const symbol = checker.getSymbolAtLocation(originalNode);
const flowNode = getFlowNode(originalNode, baseIdentifier);
const symbol = checker.getSymbolAtLocation(flowNode?.name ?? flowNode);

if (!symbol) {
return;
Expand Down

0 comments on commit e59af64

Please sign in to comment.