close

no-document-cookie

Added in v0.9.1

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/no-document-cookie': 'error',
    },
  },
]);

Rule Details

Disallow assigning to document.cookie directly. Constructing cookie strings by hand is error-prone; prefer the Cookie Store API or a cookie library.

Examples of incorrect code for this rule:

document.cookie = 'name=value; Path=/; Secure';
document.cookie += '; SameSite=Lax';

Examples of correct code for this rule:

await cookieStore.set({
  name: 'name',
  value: 'value',
  path: '/',
});

const cookies = document.cookie.split('; ');

The rule follows aliases of the global document object and its standard global-object forms, so these assignments are also reported:

const doc = globalThis.document;
doc.cookie = 'name=value';

window.document.cookie = 'name=value';

Original Documentation