custom-d/lint-hook

A lint hook

1.6.0 2021-03-23 22:44 UTC

This package is not auto-updated.

Last update: 2024-11-09 19:22:36 UTC


README

GitHub Workflow Status styleci

Packagist Packagist Packagist

Package description: CHANGE ME

Installation

Install via composer by adding the following to your composer.json file

 "repositories": [
        {
            "type": "vcs",
            "url": "https://git.customd.com/custom-d/linters.git"
        }
    ]

and

"scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi",
            "[ $COMPOSER_DEV_MODE -eq 0 ] || composer ide-helper",
            "[ $COMPOSER_DEV_MODE -eq 0 ] || cghooks add --force"
        ],
composer require custom-d/lint-hook

Setup JS Linter

run ./vendor/bin/eslint-cd

It will walk you step-by-step though the options. Each one will display the Custom D recommendation.

This script is a modified version of paulolramos/eslint-prettier-airbnb-react with our custom rules. This script was inspired by Jeffrey Zhen's tutorial

Setup PHP Linter:

Laravel

cp vendor/customd-d/lint-hook/phpcs.xml .

update your composer.json file secion as follows by adding hooks to the extra section

"extra": {
        "laravel": {
            "dont-discover": []
        },
        "hooks": {
            "post-merge": "fin composer install",
            "commit-msg": "vendor/bin/cd-git-laravel $1"
        }
    },

In your unit test if you have some that are slow or that you only want to run during the Pipelines add a comment above the method with @group CI_ONLY

eg:

/**
     * A basic test example.
     *
     * @group CI_ONLY
     * @group Unit
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }

Zon

cp vendor/customd-d/lint-hook/zon/phpcs.xml .

Add to composer.json file in your project:

"standards:check" : [
    "phpcs --standard=./phpcs.xml -p"
],
"standards:format" : [
    "phpcbf --standard=./phpcs.xml -p"
]

Code editor (VSCode) setup

Code Editor Plugins

Open your code editor extension manager, search for the Eslint, Prettier, and PHP Sniffer & Beautifier plugins and install

ESLint

Prettier

PHP Sniffer & Beautifier

Code Editor Settings

  • Open your code editor settings. You can edit the raw settings.json file by clicking the file icon (top right).

  • Add the following lines to override VSCode's default settings. This will allow VSCode to auto-format your file every time you save it.


"prettier.printWidth": 140,
"editor.wordWrapColumn": 140,

"editor.defaultFormatter": "esbenp.prettier-vscode",

"[php]": {
    "editor.defaultFormatter": "valeryanm.vscode-phpsab"
},

// Set Prettier as the default formatter
"[javascriptreact]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
},

// Set default terraform formatter
"[terraform]": {
    "editor.defaultFormatter": "hashicorp.terraform"
},

// Set default shell script formatters
"[dotenv]": {
    "editor.defaultFormatter": "foxundermoon.shell-format"
},
"[dockerfile]": {
    "editor.defaultFormatter": "foxundermoon.shell-format"
},
"[ignore]": {
    "editor.defaultFormatter": "foxundermoon.shell-format"
},
"[shellscript]": {
    "editor.defaultFormatter": "foxundermoon.shell-format"
},
"[properties]": {
    "editor.defaultFormatter": "foxundermoon.shell-format"
},

// format on paste and save
"editor.formatOnPaste": true,
"editor.formatOnType": true,
"editor.formatOnSave": true,
// run ESlint on save
"eslint.run": "onSave",
"eslint.alwaysShowStatus": true,
"editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
},
// strip whitespace
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
// shell format config
"shellformat.useEditorConfig": true,

PHP Sniffer & Beautifier settings

PHP Sniffer & Beautifier will probably require you to specify the exact full path to the phpcs and phpcbf binaries.

To do that:

  • In the settings.json file (see how to open above)
  • Look for the executable path inputs, and fill them out with the right paths, which you can find with e.g. which phpcs in a terminal. They will be something like this:
/Users/thisWillBeYou/.composer/vendor/bin/phpcbf
/Users/thisWillBeYou/.composer/vendor/bin/phpcs

Add these paths above as follows:

  "phpsab.executablePathCBF": "/Users/thisWillBeYou/.composer/vendor/bin/phpcbf",
  "phpsab.executablePathCS": "/Users/thisWillBeYou/.composer/vendor/bin/phpcs",

Important: The "PHP Sniffer and Beautifier" plugin will find and use the ruleset from the phpcs.xml file in your current workspace.

If you open a file from project A in a project B workspace, Project B rules will be applied when formatting/saving that file.