logoESLint React
Rules

function-definition

Validates and transforms React Client/Server Function definitions.

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-plugin-react-rsc

react-rsc/function-definition

Full Name in @eslint-react/eslint-plugin

@eslint-react/rsc-function-definition

Features

๐Ÿ”ง ๐Ÿงช

Presets

rsc recommended recommended-typescript recommended-type-checked strict strict-typescript strict-type-checked

Rule Details

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.

Common Violations

Invalid

'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>;
}

Valid

'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>;
}

Resources

Further Reading

On this page