PACT, the way to code

Hello Developers! Many of us use various design patterns with React Development to make React Apps more clean, clearly understandable, and more structured. But still we are facing issues with coding standards, we changed at one place and it breaks at a…


This content originally appeared on DEV Community and was authored by Diego Vallejo

Hello Developers! Many of us use various design patterns with React Development to make React Apps more clean, clearly understandable, and more structured. But still we are facing issues with coding standards, we changed at one place and it breaks at another place. Is there any resolution for this? Yes, there is. PACT is a tool that allows you to create contract between various components of your app that tell you which components are compatible with each other. It can help you create a consistent structure in your React Apps.

What is PACT?

PACT stands for Prettier Acceptance Criteria Templates. It is a tool for creating pre-commit tests to enforce a consistent code style. PACT takes your current code style and compares it to the desired style. If your code matches, the test passes and your commit is accepted. If it doesn’t match, the test fails and your commit is rejected.

How does it work?

PACT is a tool that can be used to enforce a consistent code style. It takes your current code style and compares it to the desired style. If your code matches, the test passes and your commit is accepted. If it doesn’t match, the test fails and your commit is rejected.

For example, in a pre-commit hook, you could specify a config file with an array of rules that your project should follow and a command for checking if your code is compliant with those rules:

var config = [{
    rule: {
        "error": true,
        "message": "Use double quotes instead of single",
        "matcher": "source.js",
        "lineRange": [1, 3]
    },
    input: "var x = 'foo"
    ' , }, { 
    rule: {
        "error": false,
        "message": "Use one space after function/array bracket",
        "matcher": "source.js",
        "lineRange": [4, 4]
    },
    input: "var x = function() {}, foo[",
}, {
    rule: {
        "error": true,
        "message": "Use two spaces after function/array bracket",
        "matcher": "source.js",
        "lineRange": [6, 6]
    },
    input: "foo[] = 'bar'",
}]

var check = require ( 'preact-pact' ).commands.check ; 
var pact = require ( 'preact-pact' ); 
check(pact( config )).run();

The whole thing is wrapped in a function that takes a configuration file and returns the promise that resolves to an array of errors that would be returned by the check command.

The input and matcher properties of each rule tell Weasel what range of code to check for compliance. Since Weasel is using source-map’s precise mappings, it’s possible to check specific lines or ranges of lines.

The check command looks for each rule in the config file and runs the command specified in the command property.

PACT can be used with any editor, but we will use VSCode as our example.

Install PACT via npm.

npm install pact-cli --global

Create a pact file for the folder where you want to enforce the code style. For this example, let’s create a pact file for the app root.

pact create <app-name>

This will create a pact file with the name .js .

Add some rules to the pact file.

This pact file will have a single rule for the whole app.

module.exports = {
        style: {
            rule: 'indent',
            params: {
                spaces: 2, // 2 spaces per indent level 
                },
                message: 'Indentation should be 2 spaces per indent level.'
            }
        }

To run the pact, you need to npm install the project and then run the pact test.

npm install
npm run pact

The pact test will run and return a green check if it passes. In our case, it will return a red “x” because we haven’t added our rules into our app yet.

We can also add a pact test to enforce the code style for a file.

Add the following at the top of our index.js file.

module.exports = {
        style: {
            rule: 'indent',
            params: {
                spaces: 2, // 2 spaces per indent level }, message: 'Indentation should be 2 spaces per indent level.' 
            },
        }

Now, when we run the pact test, it will return a green check because the file already matches the code style.

If you want to enforce the code style for an individual file, you can add it to the pact file and run the test.

module.exports = {
        style: {
            rule: 'indent',
            params: {
                spaces: 2, // 2 spaces per indent level }, message: 'Indentation should be 2 spaces per indent level.',
                file: 'index.js' 
            }
        }

Now, when you run the test, it will return a red “x” again.

Why we should use PACT?

PACT is a good tool that offers many advantages. But the main advantage is that it allows you to build a consistent React App. It is a very good tool for enforcing a consistent project structure, code style, and workflow. This is why PACT should be used in modern React App Development.

Conclusion

PACT is a tool for enforcing a consistent code style across a project. It can help you create a consistent structure in your React Apps.It is a very good tool for enforcing a consistent project structure, code style, and workflow. This is why PACT should be used in modern React App Development.

Hope you like it. Share your feedback in the comment section below.


This content originally appeared on DEV Community and was authored by Diego Vallejo


Print Share Comment Cite Upload Translate Updates
APA

Diego Vallejo | Sciencx (2021-05-24T01:50:16+00:00) PACT, the way to code. Retrieved from https://www.scien.cx/2021/05/24/pact-the-way-to-code/

MLA
" » PACT, the way to code." Diego Vallejo | Sciencx - Monday May 24, 2021, https://www.scien.cx/2021/05/24/pact-the-way-to-code/
HARVARD
Diego Vallejo | Sciencx Monday May 24, 2021 » PACT, the way to code., viewed ,<https://www.scien.cx/2021/05/24/pact-the-way-to-code/>
VANCOUVER
Diego Vallejo | Sciencx - » PACT, the way to code. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/24/pact-the-way-to-code/
CHICAGO
" » PACT, the way to code." Diego Vallejo | Sciencx - Accessed . https://www.scien.cx/2021/05/24/pact-the-way-to-code/
IEEE
" » PACT, the way to code." Diego Vallejo | Sciencx [Online]. Available: https://www.scien.cx/2021/05/24/pact-the-way-to-code/. [Accessed: ]
rf:citation
» PACT, the way to code | Diego Vallejo | Sciencx | https://www.scien.cx/2021/05/24/pact-the-way-to-code/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.