# Why Cursor Generates Invalid JavaScript

- Tool: Cursor
- Difficulty: Beginner
- Time required: 10-15 min
- Compatibility: Cursor Free+, JavaScript/TypeScript projects
- Last updated: March 2026

## TL;DR

Cursor can generate JavaScript that uses patterns banned in strict mode, such as undeclared variables, duplicate parameters, with statements, and octal literals. Since most modern projects enforce strict mode through ES modules or explicit 'use strict' directives, this code fails at runtime. Adding .cursor/rules/ with strict mode patterns and combining with ESLint's strict-mode rules ensures Cursor generates valid, strict-mode-compliant JavaScript.

## Why Cursor generates invalid JavaScript and how to fix it

JavaScript strict mode prohibits error-prone patterns like implicit global variables, duplicate function parameters, and the with statement. ES modules automatically enable strict mode, so any non-strict code Cursor generates fails silently or throws errors. This tutorial configures Cursor to always generate strict-mode-compliant code.

## Before you start

- Cursor installed with a JavaScript or TypeScript project
- Basic understanding of JavaScript strict mode
- ESLint configured in the project
- Familiarity with Cursor project rules

## Step-by-step guide

### 1. Create a strict mode compliance rule

Add a project rule that lists specific strict mode violations to avoid. TypeScript projects get most of these for free, but Cursor can still generate patterns that cause runtime issues in JavaScript files.

```
---
description: Enforce JavaScript strict mode compliance
globs: "*.js,*.jsx,*.mjs"
alwaysApply: true
---

# Strict Mode Compliance
- NEVER use undeclared variables (always use const, let, or var)
- NEVER use the 'with' statement
- NEVER use duplicate parameter names in functions
- NEVER use octal numeric literals (0644) — use 0o644 instead
- NEVER assign to read-only properties or undeletable properties
- NEVER use 'arguments.callee' or 'arguments.caller'
- NEVER use eval() to create variables in surrounding scope
- ALWAYS declare variables before use
- ALWAYS use 'use strict' at the top of .js files (or use ES modules)

## TypeScript files are strict by default if tsconfig has strict: true
```

**Expected result:** Cursor generates JavaScript that complies with strict mode restrictions.

### 2. Add ESLint strict mode rules

Configure ESLint rules that catch strict mode violations. These rules create a feedback loop where Cursor reads lint errors via @Lint Errors and adjusts its output automatically.

```
// eslint.config.js
import js from '@eslint/js';

export default [
  js.configs.recommended,
  {
    rules: {
      'strict': ['error', 'safe'],
      'no-undef': 'error',
      'no-var': 'error',
      'no-with': 'error',
      'no-octal': 'error',
      'no-eval': 'error',
      'no-caller': 'error',
      'no-implicit-globals': 'error',
      'no-shadow-restricted-names': 'error',
    },
  },
];
```

**Expected result:** ESLint catches strict mode violations in Cursor-generated code and feeds errors back to Cursor.

### 3. Fix existing violations with Cmd+K

Select code with strict mode violations and use Cmd+K to fix them. Common fixes include replacing undeclared variables with const/let, removing with statements, and converting octal literals to the 0o prefix syntax.

```
Fix all strict mode violations in this code:
- Add const/let declarations for any undeclared variables
- Replace 'with' statements with explicit property access
- Replace octal literals (0644) with ES6 octal syntax (0o644)
- Remove duplicate function parameter names
- Replace arguments.callee with named function references
- Ensure 'use strict' is present at file top if not using ES modules
```

**Expected result:** All strict mode violations are fixed while maintaining the original code behavior.

### 4. Verify with TypeScript strict configuration

If your project uses TypeScript, enable strict mode in tsconfig.json. TypeScript strict mode catches most JavaScript strict mode violations at compile time, providing an additional safety layer beyond Cursor rules.

```
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  }
}
```

**Expected result:** TypeScript compiler catches strict mode violations at build time, complementing Cursor rules and ESLint.

### 5. Test generated code for strict mode compliance

Use Cursor Chat to audit your codebase for remaining strict mode issues. This catches violations in both AI-generated and manually written code.

```
@strict-mode.mdc @codebase

Search the codebase for JavaScript strict mode violations:
1. Variables used without declaration
2. 'with' statement usage
3. Duplicate function parameter names
4. Octal numeric literals without 0o prefix
5. eval() calls that create variables
6. arguments.callee or arguments.caller usage
7. Files missing 'use strict' that are not ES modules

For each violation, show the file, line, and the fix.
```

**Expected result:** Cursor identifies remaining strict mode violations with specific fixes for each.

## Complete code example

File: `.cursor/rules/strict-mode.mdc`

```markdown
---
description: Enforce JavaScript strict mode compliance
globs: "*.js,*.jsx,*.mjs"
alwaysApply: true
---

# Strict Mode Compliance Rules

## NEVER generate:
- Undeclared variables (always use const or let)
- The 'with' statement
- Duplicate parameter names
- Octal literals without 0o prefix
- arguments.callee or arguments.caller
- eval() to create variables in enclosing scope
- Assignment to read-only globals (undefined, NaN, Infinity)
- delete on plain variable names

## ALWAYS:
- Use 'use strict' at file top for .js files
- Use const for values that never change
- Use let only when reassignment is needed
- Use named functions instead of arguments.callee
- Use 0o prefix for octal numbers (0o755 not 0755)
- Use ES module syntax (import/export) which implies strict mode

## Correct:
```javascript
'use strict';
const fileMode = 0o755;
const items = [1, 2, 3];
const sum = items.reduce((acc, val) => acc + val, 0);
```

## Wrong:
```javascript
fileMode = 0755;        // undeclared var + old octal
with (obj) { x = 1; }  // with statement
function f(a, a) {}     // duplicate params
```
```

## Common mistakes

- **Assuming TypeScript files are immune to strict mode issues** — TypeScript with strict: true catches most issues but can still allow some patterns like eval and arguments.callee if not explicitly configured. Fix: Enable alwaysStrict in tsconfig.json and add ESLint rules for eval and arguments usage.
- **Not adding 'use strict' to CommonJS files** — CommonJS files (.js with require/module.exports) do not automatically enable strict mode. Cursor may generate code that works in development but fails in strict environments. Fix: Add 'use strict' as the first line of all CommonJS files, or migrate to ES modules which enable strict mode automatically.
- **Using eval for dynamic code execution** — Cursor sometimes uses eval for tasks like dynamic JSON parsing or computed property access. In strict mode, eval cannot create variables in the surrounding scope, causing unexpected behavior. Fix: Add NEVER use eval to your rules. Use JSON.parse for JSON, computed property access for dynamic properties, and new Function() only as a last resort.

## Best practices

- Enable TypeScript strict mode in tsconfig.json for compile-time checking
- Add ESLint strict mode rules for runtime JavaScript files
- Use ES modules (import/export) which enable strict mode automatically
- Add 'use strict' to any remaining CommonJS files
- Audit generated code with @codebase for strict mode violations
- Keep Cursor rules and ESLint rules aligned for consistent enforcement
- Test generated code in strict mode environments before deploying

## Frequently asked questions

### Are ES modules always in strict mode?

Yes. Any file using import/export syntax is automatically in strict mode, regardless of whether 'use strict' is present. This is the simplest way to ensure strict mode compliance.

### Does strict mode affect performance?

Strict mode can actually improve performance because the engine can make certain optimizations when it knows variables are properly declared and eval does not create new variables.

### What about Node.js strict mode?

Node.js follows the same rules. ES modules (.mjs or type: module in package.json) are strict by default. CommonJS files need explicit 'use strict' at the top.

### Will Cursor Tab completion violate strict mode?

Tab completion does not read project rules, so it may suggest non-strict patterns. ESLint catches these immediately, and you can reject them with Esc.

### How do I migrate a legacy codebase to strict mode?

Use Cursor with @folder to process files one at a time. Prompt: Fix all strict mode violations in this file. Test after each file to ensure behavior is preserved.

### Can RapidDev help modernize our JavaScript codebase?

Yes. RapidDev handles JavaScript modernization including strict mode migration, ES module conversion, and TypeScript adoption, with Cursor rules configured for ongoing compliance.

---

Source: https://www.rapidevelopers.com/cursor-tutorial/how-to-keep-cursor-ai-from-suggesting-code-that-violates-strict-mode-in-ecmascript
© RapidDev — https://www.rapidevelopers.com/cursor-tutorial/how-to-keep-cursor-ai-from-suggesting-code-that-violates-strict-mode-in-ecmascript
