npm package: @cbs-consulting/cds-eslint
Description: ESLint plugin for CDS (Core Data Services) files with custom linting rules
Latest version: 1.0.0

# @cbs-consulting/cds-eslint

ESLint plugin for CDS (Core Data Services) files with custom linting rules for better code quality and consistency.

## Installation

```bash
npm install --save-dev @cbs-consulting/cds-eslint eslint
```

## Usage

### Configuration (ESLint 9+ Flat Config)

Create an `eslint.config.js` in your project root:

```javascript
const cdsLint = require("@cbs-consulting/cds-eslint");

module.exports = [
  // Simply use the recommended config - it includes parser and all settings
  cdsLint.configs.recommended,
  
  // ...your other configs
];
```

That's it! The plugin handles the CDS parser internally.

### Custom Configuration

If you want to customize the rules:

```javascript
const cdsLint = require("@cbs-consulting/cds-eslint");

module.exports = [
  {
    files: ["**/*.cds"],
    languageOptions: {
      parser: cdsLint.parsers.cds,
    },
    plugins: {
      "@cbs-consulting/cds-eslint": cdsLint,
    },
    rules: {
      "@cbs-consulting/cds-eslint/group-by-matches-keys": "warn", // or "error"
    },
  },
];
```

### VS Code Integration

For the best development experience with inline error highlighting in VS Code:

1. **Install the ESLint extension**:
   - Open VS Code
   - Go to Extensions (Ctrl+Shift+X / Cmd+Shift+X)
   - Search for "ESLint" by Microsoft
   - Click Install

2. **Configure VS Code settings**:
   
   Add to your `.vscode/settings.json`:
   
   ```json
   {
     "eslint.validate": [
       "javascript",
       "javascriptreact",
       "typescript",
       "typescriptreact",
       "cds"
     ],
     "eslint.probe": [
       "javascript",
       "javascriptreact",
       "typescript",
       "typescriptreact",
       "cds"
     ],
     "eslint.options": {
       "extensions": [".js", ".jsx", ".ts", ".tsx", ".cds"]
     }
   }
   ```

3. **Restart VS Code** to ensure the settings take effect.

Now, CDS files will show inline errors and warnings directly in the editor!

### Command Line Usage

You can also run the linter from the command line:

```bash
npx eslint "**/*.cds"
```

Or add to your `package.json` scripts:

```json
{
  "scripts": {
    "lint": "eslint \"**/*.cds\"",
    "lint:fix": "eslint \"**/*.cds\" --fix"
  }
}
```

## Rules

### `group-by-matches-keys`

Ensures that GROUP BY clauses in CDS views match key columns exactly.

**Rule Details:**

This rule enforces that GROUP BY clauses properly align with the key columns defined in CDS entity projections and views.

**Examples of incorrect code:**

```cds
// ❌ Missing key in GROUP BY
entity BadExample1 as
  select from Orders as o {
    key o.ID,
    key o.OrderNo,  // ❌ OrderNo is a key but not in GROUP BY
        sum(o.price) as totalPrice
  }
  group by o.ID;

// ❌ Non-key column in GROUP BY
entity BadExample2 as
  select from Orders as o {
    key o.ID,
        o.quantity,  // ❌ quantity is not a key but is in GROUP BY
        sum(o.price) as totalPrice
  }
  group by
    o.ID,
    o.quantity;

// ❌ Undefined column in GROUP BY
entity BadExample3 as
  select from Orders as o {
    key o.productID,
        count(*) as orderCount
  }
  group by
    o.productID,
    o.customerID;  // ❌ customerID is not selected
```

**Examples of correct code:**

```cds
// ✅ All keys in GROUP BY, no non-keys
entity GoodExample1 as
  select from Orders as o {
    key o.ID,
    key o.OrderNo,
        sum(o.price) as totalPrice
  }
  group by
    o.ID,
    o.OrderNo;

// ✅ Single key with aggregation
entity GoodExample2 as
  select from Orders as o {
    key o.productID,
        count(*) as orderCount,
        sum(o.price) as totalRevenue
  }
  group by o.productID;
```