logoESLint React
Rules

function-definition

This rule is experimental and may change in the future or be removed. It is not recommended for use in production code at this time.

Full Name in @eslint-react/eslint-plugin

@eslint-react/rsc/function-definition

Full Name in eslint-plugin-react-rsc

react-rsc/function-definition

Features

๐Ÿ”ง ๐Ÿงช

Description

Validate and transform React Client/Server Function definitions.

It includes the following checks:

  1. If a file has a top-level 'use server' directive, exported functions must be async. Functions with their own 'use server' directive must also be async.

Examples

Failing

'use server';

export function serverFunction1() {
  // ...
}

export const serverFunction2 = function() {
  // ...
};

export const serverFunction3 = () => {
  // ...
};
export function Component() {
  function serverFunction1() {
    'use server';
    // ...
  }

  const serverFunction2 = function() {
    'use server';
    // ...
  };

  const serverFunction3 = () => {
    'use server';
    // ...
  };

  return <div>...</div>;
}

Passing

'use server';

export async function serverFunction1() {
  // ...
}

export const serverFunction2 = async function() {
  // ...
};

export const serverFunction3 = async () => {
  // ...
};
export function Component() {
  async function serverFunction1() {
    'use server';
    // ...
  }

  const serverFunction2 = async function() {
    'use server';
    // ...
  };

  const serverFunction3 = async () => {
    'use server';
    // ...
  };

  return <div>...</div>;
}

Implementation

Further Reading

On this page