Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump skuba from 6.2.0 to 7.3.1 #84

Merged
merged 3 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
# managed by skuba
.idea/*
.vscode/*

.cdk.staging/
.serverless/
cdk.out/
node_modules*/

/coverage*/
/dist*/
/lib*/
/tmp*/

# Gantry resource files support non-standard template syntax
/.gantry/**/*.yaml
/.gantry/**/*.yml
gantry*.yaml
gantry*.yml
pnpm-lock.yaml
# end managed by skuba

src/rules/**/tests/*-disable-prettier*.ts
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"devDependencies": {
"@types/node": "20.11.3",
"skuba": "6.2.0",
"skuba": "7.3.1",
"zod": "3.22.4",
"zod-openapi": "2.9.1"
},
Expand Down
157 changes: 82 additions & 75 deletions src/rules/prefer-openapi-last/rule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ESLintUtils, type TSESLint } from '@typescript-eslint/utils';
import {
AST_NODE_TYPES,
ESLintUtils,
type TSESLint,
} from '@typescript-eslint/utils';

import { findOpenApiCallExpression, getIdentifier } from '../../util/traverse';
import { getType } from '../../util/type';
Expand All @@ -8,90 +12,93 @@ const createRule = ESLintUtils.RuleCreator(
(name) => `https://example.com/rule/${name}`,
);

export const rule: TSESLint.RuleModule<any, any> = createRule({
create(context) {
return {
VariableDeclaration(node) {
const declarator = node?.declarations[0];
if (!declarator) {
return;
}
export const rule: TSESLint.RuleModule<'requires', readonly unknown[]> =
createRule({
create(context) {
return {
VariableDeclaration(node) {
const declarator = node?.declarations[0];
if (!declarator) {
return;
}

const type = getType(declarator, context);
if (!type?.isZodType) {
return;
}
const type = getType(declarator, context);
if (!type?.isZodType) {
return;
}

const openApiCallExpression = findOpenApiCallExpression(declarator);
const openApiCallExpression = findOpenApiCallExpression(declarator);

if (!openApiCallExpression) {
return;
}
if (!openApiCallExpression) {
return;
}

const lastNode = getIdentifier(declarator);
const lastNode = getIdentifier(declarator);

if (!lastNode || lastNode?.name === 'openapi') {
return;
}
if (!lastNode || lastNode?.name === 'openapi') {
return;
}

if (
lastNode.parent?.type === 'MemberExpression' &&
lastNode.parent.parent?.type === 'CallExpression'
) {
return context.report({
messageId: 'requires',
node:
openApiCallExpression.callee.type === 'MemberExpression'
? openApiCallExpression.callee.property
: openApiCallExpression,
});
}
},
Property(node) {
const type = getType(node, context);
if (!type?.isZodType) {
return;
}
if (
lastNode.parent?.type === AST_NODE_TYPES.MemberExpression &&
lastNode.parent.parent?.type === AST_NODE_TYPES.CallExpression
) {
return context.report({
messageId: 'requires',
node:
openApiCallExpression.callee.type ===
AST_NODE_TYPES.MemberExpression
? openApiCallExpression.callee.property
: openApiCallExpression,
});
}
},
Property(node) {
const type = getType(node, context);
if (!type?.isZodType) {
return;
}

const openApiCallExpression = findOpenApiCallExpression(node);
const openApiCallExpression = findOpenApiCallExpression(node);

if (!openApiCallExpression) {
return;
}
if (!openApiCallExpression) {
return;
}

const lastNode = getIdentifier(node.value);
const lastNode = getIdentifier(node.value);

if (!lastNode || lastNode?.name === 'openapi') {
return;
}
if (!lastNode || lastNode?.name === 'openapi') {
return;
}

if (
lastNode.parent?.type === 'MemberExpression' &&
lastNode.parent.parent?.type === 'CallExpression'
) {
return context.report({
messageId: 'requires',
node:
openApiCallExpression.callee.type === 'MemberExpression'
? openApiCallExpression.callee.property
: openApiCallExpression,
});
}
},
};
},
name: 'require-openapi-last',
meta: {
type: 'suggestion',
messages: {
requires: '.openapi() should be declared at the end of a zod chain',
if (
lastNode.parent?.type === AST_NODE_TYPES.MemberExpression &&
lastNode.parent.parent?.type === AST_NODE_TYPES.CallExpression
) {
return context.report({
messageId: 'requires',
node:
openApiCallExpression.callee.type ===
AST_NODE_TYPES.MemberExpression
? openApiCallExpression.callee.property
: openApiCallExpression,
});
}
},
};
},
schema: [],
docs: {
description:
'Requires that .openapi() should be declared at the end of a zod chain',
recommended: 'error',
name: 'require-openapi-last',
meta: {
type: 'suggestion',
messages: {
requires: '.openapi() should be declared at the end of a zod chain',
},
schema: [],
docs: {
description:
'Requires that .openapi() should be declared at the end of a zod chain',
recommended: 'error',
},
},
},
defaultOptions: [],
});
defaultOptions: [],
});
9 changes: 5 additions & 4 deletions src/rules/prefer-zod-default/rule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AST_NODE_TYPES,
ESLintUtils,
type TSESLint,
type TSESTree,
Expand All @@ -12,8 +13,8 @@ const getDefault = (
): TSESTree.Property | undefined => {
for (const property of properties) {
if (
property.type === 'Property' &&
property.key.type === 'Identifier' &&
property.type === AST_NODE_TYPES.Property &&
property.key.type === AST_NODE_TYPES.Identifier &&
property.key.name === 'default'
) {
return property;
Expand All @@ -24,11 +25,11 @@ const getDefault = (

const testDefault = (
_node: TSESTree.Node,
context: Readonly<TSESLint.RuleContext<any, any>>,
context: Readonly<TSESLint.RuleContext<'prefer', readonly unknown[]>>,
openApiCallExpression: TSESTree.CallExpression,
) => {
const argument = openApiCallExpression?.arguments[0];
if (!argument || argument.type !== 'ObjectExpression') {
if (!argument || argument.type !== AST_NODE_TYPES.ObjectExpression) {
return;
}

Expand Down
47 changes: 27 additions & 20 deletions src/rules/require-comment/rule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
ESLintUtils,
type TSESLint,
type TSESTree,
Expand All @@ -24,7 +26,7 @@ const getCommentValue = (comment: string): string | undefined => {
};

export const getCommentNode = (node: TSESTree.Node): TSESTree.Node => {
if (node.parent?.type === 'ExportNamedDeclaration') {
if (node.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration) {
return node.parent;
}

Expand All @@ -33,10 +35,12 @@ export const getCommentNode = (node: TSESTree.Node): TSESTree.Node => {

export const getComment = (
node: TSESTree.Node,
context: Readonly<TSESLint.RuleContext<any, any>>,
context: Readonly<
TSESLint.RuleContext<'required' | 'comment', readonly unknown[]>
>,
): TSESTree.BlockComment | undefined => {
const comment = context.getSourceCode().getCommentsBefore(node).at(-1);
return comment?.type === 'Block' ? comment : undefined;
return comment?.type === AST_TOKEN_TYPES.Block ? comment : undefined;
};

const createCommentValue = (
Expand Down Expand Up @@ -68,7 +72,7 @@ ${indent}`;

export const isNewLineRequired = (node: TSESTree.Property) => {
const objectExpression = node.parent;
if (objectExpression?.type !== 'ObjectExpression') {
if (objectExpression?.type !== AST_NODE_TYPES.ObjectExpression) {
return false;
}

Expand All @@ -83,7 +87,7 @@ export const isNewLineRequired = (node: TSESTree.Property) => {
if (
objectExpression.properties.some(
(property) =>
property.type === 'Property' &&
property.type === AST_NODE_TYPES.Property &&
node.range[0] !== property.range[0] &&
node.range[1] !== property.range[1] &&
node.loc.start.line === property.loc.start.line,
Expand All @@ -104,8 +108,8 @@ const getPropertyNode = (
| undefined => {
for (const property of properties) {
if (
property.type === 'Property' &&
property.key.type === 'Identifier' &&
property.type === AST_NODE_TYPES.Property &&
property.key.type === AST_NODE_TYPES.Identifier &&
property.key.name === key
) {
return property;
Expand All @@ -121,14 +125,14 @@ const getExampleValue = (
const example = getPropertyNode(properties, 'example');

if (examples) {
if (examples.value.type !== 'ArrayExpression') {
if (examples.value.type !== AST_NODE_TYPES.ArrayExpression) {
// This should always be an array if not ts compiler will complain
return;
}

const element = examples.value.elements?.[0];

if (!element || element.type !== 'Literal') {
if (!element || element.type !== AST_NODE_TYPES.Literal) {
return;
}

Expand All @@ -137,7 +141,7 @@ const getExampleValue = (
}

if (example) {
if (example.value.type !== 'Literal') {
if (example.value.type !== AST_NODE_TYPES.Literal) {
return;
}

Expand All @@ -157,23 +161,23 @@ const getLiteralValue = (
return undefined;
}

if (property.value.type === 'Literal') {
if (property.value.type === AST_NODE_TYPES.Literal) {
return property.value.value;
}
return undefined;
};

const getExpectedCommentValue = (
node: TSESTree.VariableDeclarator | TSESTree.Property,
context: Readonly<TSESLint.RuleContext<any, any>>,
context: Readonly<TSESLint.RuleContext<string, readonly unknown[]>>,
) => {
const openApiCallExpression = findOpenApiCallExpression(node);
if (!openApiCallExpression) {
return getInferredComment(node, context);
}

const argument = openApiCallExpression?.arguments[0];
if (!argument || argument.type !== 'ObjectExpression') {
if (!argument || argument.type !== AST_NODE_TYPES.ObjectExpression) {
return;
}

Expand Down Expand Up @@ -333,33 +337,36 @@ export const rule = createRule({
TSTypeReference(node) {
// only check z.infer, z.input, z.output
if (
node.typeName.type !== 'TSQualifiedName' ||
node.typeName.left.type !== 'Identifier' ||
node.typeName.type !== AST_NODE_TYPES.TSQualifiedName ||
node.typeName.left.type !== AST_NODE_TYPES.Identifier ||
node.typeName.left.name !== 'z' ||
node.typeName.right.type !== 'Identifier' ||
node.typeName.right.type !== AST_NODE_TYPES.Identifier ||
(node.typeName.right.name !== 'infer' &&
node.typeName.right.name !== 'input' &&
node.typeName.right.name !== 'output')
) {
return;
}

if (node.typeParameters?.type !== 'TSTypeParameterInstantiation') {
if (
node.typeParameters?.type !==
AST_NODE_TYPES.TSTypeParameterInstantiation
) {
return;
}

const param = node.typeParameters.params[0];

if (
param?.type !== 'TSTypeQuery' ||
param.exprName.type !== 'Identifier'
param?.type !== AST_NODE_TYPES.TSTypeQuery ||
param.exprName.type !== AST_NODE_TYPES.Identifier
) {
return;
}

const declaration = node.parent;

if (declaration?.type !== 'TSTypeAliasDeclaration') {
if (declaration?.type !== AST_NODE_TYPES.TSTypeAliasDeclaration) {
return;
}

Expand Down
Loading