Default linting configurations for the Studyportals repositories
npm install @studyportals/code-styleThe purpose of this repository is to provide standardized configuration files for the most common linters we use in Studyportals repositories. The main usage is in Super-Linter, which uses these configuration files to run the most important linters on most of our repositories.
Please send pull requests to @stefanklokgieters if you think you want to suggest changes. Never publish changes on NPM without a approval...
- Install
- Linters
- ESLint
- Stylelint
- PHP_CodeSniffer
- PHP Mess Detector
- PHPStan
- Running Super-Linter Locally
- Best Practices
You can add CodeStyle as a dependency to your project by running the following command:
``bash`
npm install @studyportals/code-style --save-dev
ESLint is a linter for JavaScript, as well as TypeScript code. It can help find and fix potential problems in your code.
When you install the CodeStyle package you can simply _extend_ from the ESLint configuration file inside it.
`json`
// package.json
"eslintConfig": {
"extends": "./node_modules/@studyportals/code-style/.eslintrc.js",
"parserOptions": {
"sourceType": "module"
}
}
#### Global variables
When global variables are used in different files than they we're defined in, ESLint will see it as an undefined variable. To solve this problem, you can define the global variables at the top of your file like so:
`javascript`
/ global var1, var2, var3 /
To avoid this you can define your project's global variables in a custom configuration.
#### Local Linting
##### Visual Studio Code
To enable linting locally, you can follw the steps below.
1. Install the ESLint plugin from the Microsoft Extensions Marketplace in VSCode
2. Open your user settings file by hitting Ctrl + Shift + P and search for Preferences: Open Settings (JSON)settings.json
3. Add the following JSON snippet to the
`json`
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
},
"eslint.validate": [
"javascript",
"typescript",
]
4. Restart the TypeScript Language Server by hitting Ctrl + Shift + P and running TypeScript: Restart TS Server
With these changes in place, whenever you save any changes, some errors are automatically resolved. For those that aren't you will receive errors or warnings that you can act upon.
##### PhpStorm
Follow the official documentation on how to configure ESLint with PhpStorm.
#### Examples
##### Overriding Rules
The example below demonstrates how you can override an existing rule in your .eslintrc.js.
`javascript`
module.exports = {
extends: "./node_modules/@studyportals/code-style/.eslintrc.js",
rules: {
"@typescript-eslint/array-type": [
"error",
{
default: "generic",
},
],
},
};
You can read more about overriding rules here.
##### Ignoring Files
The example below demonstrates how you can ignore files in your .eslintrc.js.
`javascript`
module.exports = {
extends: "./node_modules/@studyportals/code-style/.eslintrc.js",
ignorePatterns: ["**/tests/"],
};
Otherwise, you can ignore these files by appending them to your .eslintignore. You can read more about ignoring files here.
StyleLint is our main linter for all styling related files. It can read all types of style related syntax, like SCSS, Sass, Less and SugarSS. When you install the CodeStyle package you can simply _extend_ from the StyleLint configuration file inside it.
`json`
// .stylelintrc.json
{
"extends": "./node_modules/@studyportals/code-style/.stylelintrc.json"
}
#### Local Linting
##### Visual Studio Code
To enable linting locally, you can follow the steps below.
1. Install the Stylelint plugin from the Microsoft Extensions Marketplace in VSCode
2. Open your user settings file by hitting Ctrl + Shift + P and search for Preferences: Open Settings (JSON)settings.json
3. Add the following JSON snippet to the
`json`
"editor.codeActionsOnSave": {
"source.fixAll.stylelint": true,
}
With these changes in place, whenever you save any changes, some errors are automatically resolved. For those that aren't you will receive errors or warnings that you can act upon.
##### PhpStorm
Follow the official documentation on how to configure Stylelint with PhpStorm.
#### Examples
##### Overriding Rules
The example below demonstrates how you can override an existing rule in your .stylelintrc.json.
`json`
{
"extends": "./node_modules/@studyportals/code-style/.stylelintrc.json",
"rule-empty-line-before": [
"always",
{
"except": "first-nested"
}
]
}
##### Ignoring Files
The example below demonstrates how you can ignore files in your .stylelintrc.json. However, StyleLint recommends you do so with a .stylelintignore, instead. You can read more about this here.
`json`
{
"extends": "./node_modules/@studyportals/code-style/.stylelintrc.json",
"ignoreFiles": ["node_modules/", "*/.js"]
}
PHP_CodeSniffer is a linter for PHP files. It can both detect and fix coding standard violations. PHP_CodeSniffer can be run from the command line, where the configuration can be referenced. You can extend the PHP_CodeSniffer configuration from the CodeStyle package.
`bash`
phpcs-run --standard=./node_modules/@studyportals/code-style/phpcs.xml ./
#### Local Linting
##### Visual Studio Code
To enable linting locally, you can follow the steps below.
1. Install the phpcs plugin from the Microsoft Extensions Marketplace in VSCode.
2. Open your user settings file by hitting Ctrl + Shift + P and search for Preferences: Open Settings (JSON)settings.json
3. Add the following JSON snippet to the
`json`
"phpcs.executablePath": "./vendor/bin/phpcs" // or "./vendor/bin/phpcs.bat"
"phpcs.ignorePatterns": [ "/vendor/" ]
##### PhpStorm
Follow the official documentation on how to configure PHP_CodeSniffer with PhpStorm.
#### Examples
##### Overriding Rules
The example below demonstrates how you can override an existing rule in your phpcs.xml to exclude it.
`xml`
##### Ignoring Files
The example below demonstrates how you can ignore files in your phpcs.xml
`xml`
PHP Mess Detector (PHPMD) is a linter for PHP files. It takes a given PHP source code base and look for several potential problems within that source. When you install the CodeStyle package you can reference the PHPMD configuration file inside it.
`xml`
#### Local Linting
##### Visual Studio Code
To enable linting locally, you can follow the steps below.
1. Install the PHP Mess Detector plugin from the Microsoft Extensions Marketplace in VSCode.
2. Open your user settings file by hitting Ctrl + Shift + P and search for Preferences: Open Settings (JSON)settings.json
3. Add the following JSON snippet to the
`json`
"phpmd.rules": "${workspaceFolder}/phpmd.xml",
##### PhpStorm
Follow the official documentation on how to configure PHPMD with PhpStorm.
#### Examples
##### Overriding Rules
The example below demonstrates how you can override an existing rule in your phpmd.xml to exclude it.
`xml`
##### Ignoring Files
The example below demonstrates how you can ignore files in your phpmd.xml
`xml`
PHP Static Analysis Tool is a linter for PHP files. PHPStan can be run from the command line, where the configuration can be referenced. The phpstan.neon configuration from CodeStyle can be included as follows.
`yaml`
includes:
- ./node_modules/@studyportals/code-style/phpstan.neon
To run PHPStan
`bash`
phpstan analyse -c phpstan.neon
#### Local Linting
##### Visual Studio Code
To enable linting locally, you can follow the steps below.
1. Install the phpstan plugin from the Microsoft Extensions Marketplace in VSCode.
##### PhpStorm
Follow the official documentation on how to configure PHPStan with PhpStorm.
#### Examples
##### Overriding Rules
The example below demonstrates how you can override an existing rule in your phpstan.neon.
`yaml`
includes:
- ./node_modules/@studyportals/code-style/phpstan.neon
- phpstan-baseline.neon
parameters:
level: 8
##### Ignoring Files
The example below demonstrates how you can ignore files in your phpstan.xml
`yaml`
includes:
- ./node_modules/@studyportals/code-style/phpstan.neon
- phpstan-baseline.neon
parameters:
excludePaths:
- Modules/RankingXDiscipline/ChartBase/rankings_disciplines.php
- TestSuites\PHPUnit\Integration\Router\Handlers\Error\DebugErrorHandlerTest.php
- TestSuites\PHPUnit\Integration\Router\Handlers\Error\DebugXhrErrorHandlerTest.php
- TestSuites\PHPUnit\Integration\Router\IndexTest.php
It is possible to run the Super-Linter GitHub workflow using Docker containers. In some cases, this can be useful. To do so, follow the instructions below.
1. Follow the act installation instructions and choose the option that best suits your environmentrepo
2. Create a GitHub access token with the scope.secrets
3. Create a new file (or equivalent) and add it to your .gitignoreSUPERLINTER=
4. Append to the .secrets file by pasting the value of the access token created in step 2package.json
5. Add a new script to your called superlinter (or equivalent), which executes the command act workflow_dispatch -W [path-to-linter-workflow] --secret-file [path-to-secret-file] --rmdocker login
6. Ensure that you are logged into the Docker registry by running npm run superlinter
7. Run
> [!IMPORTANT]
> If you are using Windows (not WSL), you might need to add the argument -P when running act. You can identify the platform based on the runs-on property of the job in the linter.yml. For the docker-image, you can select the relevant medium-sized image from the documentation.
It is recommended to read the act` documentation.
1. Whitelisting source folders is the recommended way of specifying which files should be linted in a project. If a certain file within the source folders needs to be skipped, that should be done on file level.