CDS Lint
To catch issues in CDS models and the CDS environment early, CAP provides an ESLint plugin for CDS @sap/eslint-plugin-cds
with a set of recommended rules that are considered best practice and universal for every CAP project. This plugin, together with the cds lint
CLI of @sap/cds-dk
, comprises what we call CDS Lint.
By nature of its design, the plugin can also be run with the ESLint CLI. However, we recommended using the CDS Lint CLI instead as it comes with all preconfigured settings.
Setup
The following command automatically installs ESLint, the CDS ESLint plugin, and adds the ESLint configuration. VS Code settings are added to your project as well, to also be able to lint CDS alongside Javascript or any other ESLint plugins you may have:
cds add lint
Adding feature 'lint'... Successfully added features to your project. Almost done -you are missing 2 npm dependencies : (1) ESLint v>=8.0.0 (2) ESLint plugin for CDSInstall dependencies now ? [y/n] y Successfully added features to your project.
If a package.json, pom.xml, or .cdsrc.json file is found, you have the choice to install the ESLint dependencies directly. Once confirmed, this will install ESLint and the CDS plugin, as well as add the corresponding configuration for the recommended rules into your project.
CDS Lint CLI
In your project's root folder, execute:
cds lint
It follows standard ESLint behaviour. If there are no lint errors, there is no output. If there are, a standard ESLint error report will be printed.
CDS Lint in VS Code
TIP
Make sure you have ESLint and our ESLint plugin installed via cds add lint
.
To turn on Lint checking in your VS Code Editor simply download the ESLint extension for Visual Studio Code. CDS Lint seamlessly integrates with it. For SAP Business Application Studio this comes preinstalled.
Now you can see lint reports also in your editor. You can see all rules marked as Editor default here. Any other (project-based) rules are not turned on by default but can be turned on via the show
rule option. For example, if we want to show the valid-csv-header
rule reports in the Editor, we would add the following to our ESLint rules
configuration:
{
"rules": {
"@sap/cds/valid-csv-header": ["warn", "show"]
}
}
CDS Lint Rules
The CDS Lint rules are a set of generic rules based on CAP best practices.
See our Rules Reference page to find out more
ESLint CLI (optional)
To have more control over the linting process, you can also access the CDS ESLint plugin natively via the ESLint CLI. To determine the proper command line options, it can help to refer to output of the equivalent call using the CDS Lint CLI with DEBUG="lint"
, which shows all of the options and flags applied:
DEBUG=lint cds lint
Linting:
[lint] - eslint --ext ".cds,.csn,.csv" ...