Skip to content

Commit

Permalink
feat(packages/eslint-plugin-sui): create few rules
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvillu committed Feb 6, 2024
1 parent 74fc576 commit 7e41dc7
Show file tree
Hide file tree
Showing 9 changed files with 426 additions and 0 deletions.
58 changes: 58 additions & 0 deletions packages/eslint-plugin-sui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# eslint-plugin-sui

Set of sui lint rules

## Installation

You'll first need to install [ESLint](https://eslint.org/):

```sh
npm i eslint --save-dev
```

Next, install `eslint-plugin-sui`:

```sh
npm install eslint-plugin-sui --save-dev
```

## Usage

Add `sui` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:

```json
{
"plugins": [
"sui"
]
}
```


Then configure the rules you want to use under the rules section.

```json
{
"rules": {
"sui/rule-name": 2
}
}
```



## Configurations

<!-- begin auto-generated configs list -->
TODO: Run eslint-doc-generator to generate the configs list (or delete this section if no configs are offered).
<!-- end auto-generated configs list -->



## Rules

<!-- begin auto-generated rules list -->
TODO: Run eslint-doc-generator to generate the rules list.
<!-- end auto-generated rules list -->


35 changes: 35 additions & 0 deletions packages/eslint-plugin-sui/docs/rules/factory-pattern.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Ensure that our classes are using the convetion of has a static create method as factory. (`factory-pattern`)

Please describe the origin of the rule here.

## Rule Details

This rule aims to...

Examples of **incorrect** code for this rule:

```js

// fill me in

```

Examples of **correct** code for this rule:

```js

// fill me in

```

### Options

If there are any options, describe them here. Otherwise, delete this section.

## When Not To Use It

Give a short description of when it would be appropriate to turn off this rule.

## Further Reading

If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.
39 changes: 39 additions & 0 deletions packages/eslint-plugin-sui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "eslint-plugin-sui",
"version": "0.0.0",
"description": "Set of sui lint rules",
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin"
],
"author": "Sui",
"main": "./src/index.js",
"exports": "./src/index.js",
"scripts": {
"lint": "npm-run-all \"lint:*\"",
"lint:eslint-docs": "npm-run-all \"update:eslint-docs -- --check\"",
"lint:js": "eslint .",
"test": "mocha tests --recursive",
"update:eslint-docs": "eslint-doc-generator"
},
"dependencies": {
"requireindex": "^1.2.0",
"string-dedent": "^3.0.1"
},
"devDependencies": {
"eslint": "^8.19.0",
"eslint-doc-generator": "^1.0.0",
"eslint-plugin-eslint-plugin": "^5.0.0",
"eslint-plugin-node": "^11.1.0",
"mocha": "^10.0.0",
"npm-run-all": "^4.1.5"
},
"engines": {
"node": "^14.17.0 || ^16.0.0 || >= 18.0.0"
},
"peerDependencies": {
"eslint": ">=7"
},
"license": "ISC"
}
14 changes: 14 additions & 0 deletions packages/eslint-plugin-sui/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const FactoryPattern = require('./rules/factory-pattern.js')
const SerializeDeserialize = require('./rules/serialize-deserialize.js')

// ------------------------------------------------------------------------------
// Plugin Definition
// ------------------------------------------------------------------------------

// import all rules in lib/rules
module.exports = {
rules: {
'factory-pattern': FactoryPattern,
'serialize-deserialize': SerializeDeserialize
}
}
65 changes: 65 additions & 0 deletions packages/eslint-plugin-sui/src/rules/factory-pattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @fileoverview Ensure that our classes are using the convetion of has a static create method as factory.
* @author factory pattern
*/
'use strict'

const dedent = require('string-dedent')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'ensure to define at least one factory function',
recommended: true,
url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements/02-project-structure.md'
},
fixable: null,
schema: [],
messages: {
notFoundFactoryFunction: dedent`
You have to define at least one static function that return an instance of your class.
Avoid to use the 'new' keyword directly in your code.
Use always a factory function
`
}
},
create: function (context) {
// variables should be defined here

// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------

// any helper functions should go here or else delete this section

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return {
ClassDeclaration(node) {
const hasStaticFactoryMethod = Boolean(
node.body?.body?.find(methodDefinition => {
return (
methodDefinition.static &&
methodDefinition.value?.body?.body?.find(body => body.type === 'ReturnStatement')?.argument?.callee.name === node.id.name // eslint-disable-line
)
})
)

if (!hasStaticFactoryMethod) {
return context.report({
node,
messageId: 'notFoundFactoryFunction'
})
}
}
}
}
}
46 changes: 46 additions & 0 deletions packages/eslint-plugin-sui/src/rules/forbidden-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @fileoverview Ensure that our coda doesnt have require (CJS) styles
* @author factory pattern
*/
'use strict'

const dedent = require('string-dedent')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'ensure to use only ESM (import) style',
recommended: true,
url: null
},
fixable: null,
schema: [],
messages: {
badFileName: dedent``,
badClassName: dedent``
}
},
create: function (context) {
// variables should be defined here

// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------

// any helper functions should go here or else delete this section

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return {
ClassDeclaration(node) {}
}
}
}
46 changes: 46 additions & 0 deletions packages/eslint-plugin-sui/src/rules/naming-convention.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @fileoverview Ensure that our classes are using the naming convention for UseCases, Services and Repositories
* @author factory pattern
*/
'use strict'

const dedent = require('string-dedent')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'ensure to use a proper naming convention',
recommended: true,
url: null
},
fixable: null,
schema: [],
messages: {
badFileName: dedent``,
badClassName: dedent``
}
},
create: function (context) {
// variables should be defined here

// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------

// any helper functions should go here or else delete this section

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return {
ClassDeclaration(node) {}
}
}
}
Loading

0 comments on commit 7e41dc7

Please sign in to comment.