From 7e41dc7323cbe9dc782d3eb78a90fef651b72e19 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Tue, 6 Feb 2024 11:40:53 +0100 Subject: [PATCH 001/103] feat(packages/eslint-plugin-sui): create few rules --- packages/eslint-plugin-sui/README.md | 58 ++++++++++++ .../docs/rules/factory-pattern.md | 35 +++++++ packages/eslint-plugin-sui/package.json | 39 ++++++++ packages/eslint-plugin-sui/src/index.js | 14 +++ .../src/rules/factory-pattern.js | 65 +++++++++++++ .../src/rules/forbidden-require.js | 46 +++++++++ .../src/rules/naming-convention.js | 46 +++++++++ .../src/rules/serialize-deserialize.js | 93 +++++++++++++++++++ .../tests/src/rules/factory-pattern.js | 30 ++++++ 9 files changed, 426 insertions(+) create mode 100644 packages/eslint-plugin-sui/README.md create mode 100644 packages/eslint-plugin-sui/docs/rules/factory-pattern.md create mode 100644 packages/eslint-plugin-sui/package.json create mode 100644 packages/eslint-plugin-sui/src/index.js create mode 100644 packages/eslint-plugin-sui/src/rules/factory-pattern.js create mode 100644 packages/eslint-plugin-sui/src/rules/forbidden-require.js create mode 100644 packages/eslint-plugin-sui/src/rules/naming-convention.js create mode 100644 packages/eslint-plugin-sui/src/rules/serialize-deserialize.js create mode 100644 packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js diff --git a/packages/eslint-plugin-sui/README.md b/packages/eslint-plugin-sui/README.md new file mode 100644 index 000000000..07481eb4d --- /dev/null +++ b/packages/eslint-plugin-sui/README.md @@ -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 + + +TODO: Run eslint-doc-generator to generate the configs list (or delete this section if no configs are offered). + + + + +## Rules + + +TODO: Run eslint-doc-generator to generate the rules list. + + + diff --git a/packages/eslint-plugin-sui/docs/rules/factory-pattern.md b/packages/eslint-plugin-sui/docs/rules/factory-pattern.md new file mode 100644 index 000000000..c6699910d --- /dev/null +++ b/packages/eslint-plugin-sui/docs/rules/factory-pattern.md @@ -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. diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json new file mode 100644 index 000000000..99d36c4a1 --- /dev/null +++ b/packages/eslint-plugin-sui/package.json @@ -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" +} diff --git a/packages/eslint-plugin-sui/src/index.js b/packages/eslint-plugin-sui/src/index.js new file mode 100644 index 000000000..84d25af60 --- /dev/null +++ b/packages/eslint-plugin-sui/src/index.js @@ -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 + } +} diff --git a/packages/eslint-plugin-sui/src/rules/factory-pattern.js b/packages/eslint-plugin-sui/src/rules/factory-pattern.js new file mode 100644 index 000000000..779da9890 --- /dev/null +++ b/packages/eslint-plugin-sui/src/rules/factory-pattern.js @@ -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' + }) + } + } + } + } +} diff --git a/packages/eslint-plugin-sui/src/rules/forbidden-require.js b/packages/eslint-plugin-sui/src/rules/forbidden-require.js new file mode 100644 index 000000000..ec3dbe74e --- /dev/null +++ b/packages/eslint-plugin-sui/src/rules/forbidden-require.js @@ -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) {} + } + } +} diff --git a/packages/eslint-plugin-sui/src/rules/naming-convention.js b/packages/eslint-plugin-sui/src/rules/naming-convention.js new file mode 100644 index 000000000..68477031f --- /dev/null +++ b/packages/eslint-plugin-sui/src/rules/naming-convention.js @@ -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) {} + } + } +} diff --git a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js new file mode 100644 index 000000000..fc3b6c7f5 --- /dev/null +++ b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js @@ -0,0 +1,93 @@ +/** + * @fileoverview ensure entity create - toJSON + * @creator david.lacedonia@adevinta.com + */ +'use strict' +const dedent = require('string-dedent') + +// ------------------------------------------------------------------------------ +// Rule Definition +// ------------------------------------------------------------------------------ + +/** @type {import('eslint').Rule.RuleModule} */ +module.exports = { + meta: { + type: 'suggestion', + docs: { + description: 'ensure entity create - toJSON', + recommended: false, + url: null + }, + fixable: null, + schema: [], + messages: { + toJSONProperties: 'Missing toJSON properties ({{props}})', + invalidTOJSONProperties: 'toJSON should return an object', + missingToJSONMethod: dedent` + If your class has a 'static create' method. You have to define a 'toJSON' method too. + The output of the 'toJSON' should be the same as the input of your 'static create' method + `, + missingCreateMethod: dedent` + If your class has a 'toJSON' method. You have to define a 'static create' method too. + The output of the 'toJSON' should be the same as the input of your 'static create' method + ` + } + }, + + create(context) { + return { + ClassDeclaration(node) { + const create = node.body.body.find(i => i.key.name === 'create') + const toJSON = node.body.body.find(i => i.key.name === 'toJSON') + const className = node.id.name + + if (['UseCase', 'Service', 'Repository'].some(allowWord => className.includes(allowWord))) return // eslint-disable-line + + if (!create && !toJSON) return + + if (create && !toJSON) + return context.report({ + node: create, + messageId: 'missingToJSONMethod' + }) + + if (toJSON && !create) + return context.report({ + node: toJSON, + messageId: 'missingCreateMethod' + }) + + let createParams = create.value.params[0] || {properties: []} + if (createParams.left) { + createParams = createParams.left + } + + const createProperties = createParams.properties + const toJSONProperties = toJSON.value.body.body[0].argument.properties + + if (!toJSONProperties) { + return context.report({ + node: toJSON, + messageId: 'invalidTOJSONProperties' + }) + } + + const createProps = createProperties.map(i => i.key.name) + const toJSONProps = toJSONProperties.map(i => i.key.name) + + const missingToJSONProps = createProps.filter( + p => !toJSONProps.find(e => e === p) + ) + if (missingToJSONProps.length) { + context.report({ + node: toJSON, + messageId: 'toJSONProperties', + data: { + props: missingToJSONProps.join(', ') + } + }) + } + } + } + } +} diff --git a/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js b/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js new file mode 100644 index 000000000..e04d54faa --- /dev/null +++ b/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js @@ -0,0 +1,30 @@ +/** + * @fileoverview Ensure that our classes are using the convetion of has a static create method as factory. + * @author factory pattern + */ +'use strict' + +// ------------------------------------------------------------------------------ +// Requirements +// ------------------------------------------------------------------------------ +import {RuleTester} from 'eslint' + +import rule from '../../../src/rules/factory-pattern' + +// ------------------------------------------------------------------------------ +// Tests +// ------------------------------------------------------------------------------ + +const ruleTester = new RuleTester() +ruleTester.run('factory-pattern', rule, { + valid: [ + // give me some code that won't trigger a warning + ], + + invalid: [ + { + code: "class Model { constructor() { this.name = 'John Doe' } }", + errors: [{message: 'Fill me in.', type: 'Me too'}] + } + ] +}) From c6ffa98cea6b262be5d203eaa91de5b82444aa46 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 7 Feb 2024 10:44:07 +0100 Subject: [PATCH 002/103] feat(packages/eslint-plugin-sui): make warning --- packages/eslint-plugin-sui/src/rules/factory-pattern.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-sui/src/rules/factory-pattern.js b/packages/eslint-plugin-sui/src/rules/factory-pattern.js index 779da9890..97cf15290 100644 --- a/packages/eslint-plugin-sui/src/rules/factory-pattern.js +++ b/packages/eslint-plugin-sui/src/rules/factory-pattern.js @@ -13,7 +13,7 @@ const dedent = require('string-dedent') /** @type {import('eslint').Rule.RuleModule} */ module.exports = { meta: { - type: 'problem', + type: 'warning', docs: { description: 'ensure to define at least one factory function', recommended: true, @@ -54,7 +54,7 @@ module.exports = { ) if (!hasStaticFactoryMethod) { - return context.report({ + context.report({ node, messageId: 'notFoundFactoryFunction' }) From 0a2563ee0809c2e348684fdf72134cdc085e6060 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 7 Feb 2024 10:46:01 +0100 Subject: [PATCH 003/103] feat(packages/sui-lint): Create reporter flag --- packages/sui-lint/bin/sui-lint-js.js | 13 +++++++++-- packages/sui-lint/eslintrc.js | 20 ++++++++++++----- packages/sui-lint/package.json | 3 ++- packages/sui-lint/src/reporter.js | 32 ++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 packages/sui-lint/src/reporter.js diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index 4e2f90642..bf7db12fc 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -7,20 +7,23 @@ const {checkFilesToLint, getFilesToLint, getGitIgnoredFiles, stageFilesIfRequire const {ESLint} = require('eslint') const config = require('../eslintrc.js') +const {Reporter} = require('../src/reporter.js') program .option('--add-fixes') .option('--staged') .option('--fix', 'fix automatically problems with js files') .option('--ignore-patterns ', 'Path patterns to ignore for linting') + .option('--report', 'Send results to DD using sui-logger') + .option('--pattern ', 'Send results to DD using sui-logger') .parse(process.argv) -const {addFixes, fix, ignorePatterns = [], staged} = program.opts() +const {addFixes, fix, ignorePatterns = [], staged, pattern, report} = program.opts() const {CI} = process.env const EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] const IGNORE_PATTERNS = ['lib', 'dist', 'public', 'node_modules'] -const DEFAULT_PATTERN = './' +const DEFAULT_PATTERN = pattern ?? './' const LINT_FORMATTER = 'stylish' const baseConfig = { ...config, @@ -51,6 +54,12 @@ const baseConfig = { const results = await eslint.lintFiles(files) + if (report) { + debugger + console.log('[sui-lint] Generating report and send it to DD') + Reporter.create().map(results) + } + if (fix) { await ESLint.outputFixes(results) stageFilesIfRequired({extensions: EXTENSIONS, staged, addFixes}) diff --git a/packages/sui-lint/eslintrc.js b/packages/sui-lint/eslintrc.js index c247117c7..b4352b11c 100644 --- a/packages/sui-lint/eslintrc.js +++ b/packages/sui-lint/eslintrc.js @@ -175,7 +175,8 @@ module.exports = { 'react-hooks', 'simple-import-sort', 'jest', - 'jest-dom' + 'jest-dom', + 'sui' ], rules: { ...REACT_RULES, @@ -198,19 +199,28 @@ module.exports = { 'prefer-regex-literals': RULES.WARNING, 'prettier/prettier': [RULES.ERROR, prettierOptions], 'simple-import-sort/imports': [RULES.WARNING, {groups: IMPORT_SORT_GROUPS}], - 'react/jsx-no-bind': RULES.OFF + 'react/jsx-no-bind': RULES.OFF, + 'sui/factory-pattern': RULES.ERROR, + 'sui/serialize-deserialize': RULES.ERROR }, overrides: [ { files: ['**/*.+(ts|tsx)'], - extends: ['standard-with-typescript'], + extends: ['standard-with-typescript', 'standard-react', 'prettier'], + parser: '@typescript-eslint/parser', parserOptions: { project: './tsconfig.json' }, rules: { + 'import/extensions': RULES.OFF, 'no-return-await': RULES.OFF, - 'prettier/prettier': RULES.OFF, - 'react/react-in-jsx-scope': RULES.OFF + 'prettier/prettier': [RULES.ERROR, prettierOptions], + 'react/react-in-jsx-scope': RULES.OFF, + 'react/no-unused-prop-types': RULES.OFF, + '@typescript-eslint/explicit-function-return-type': [RULES.OFF, {allowTypedFunctionExpressions: false}], + 'chai-friendly/no-unused-expressions': RULES.ERROR, + '@typescript-eslint/no-unused-expressions': RULES.OFF, + '@typescript-eslint/return-await': RULES.OFF } }, { diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index dfb8672e4..aa29378b1 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -19,8 +19,9 @@ "@babel/eslint-plugin": "7.18.10", "@s-ui/helpers": "1", "@typescript-eslint/eslint-plugin": "5.62.0", + "@adv-ui/logger": "2", "commander": "8.3.0", - "eslint": "8.20.0", + "eslint": "8.56.0", "eslint-config-prettier": "8.5.0", "eslint-config-standard": "17.0.0", "eslint-config-standard-with-typescript": "22.0.0", diff --git a/packages/sui-lint/src/reporter.js b/packages/sui-lint/src/reporter.js new file mode 100644 index 000000000..841fd31ff --- /dev/null +++ b/packages/sui-lint/src/reporter.js @@ -0,0 +1,32 @@ +class Reporter { + #data + + static create() { + return new Reporter() + } + + map(results) { + const stats = results.reduce((rulesIDs, entry) => { + const repitedRulesFailed = entry.messages.map(message => message.ruleId) + const rulesFailed = new Set([...repitedRulesFailed]) + + rulesFailed.forEach(rule => { + rulesIDs[rule] = rulesIDs[rule] ?? [] + rulesIDs[rule].push(entry.filePath) + }) + return rulesIDs + }, {}) + + this.#data = { + totalFiles: results.length, + ...stats + } + debugger + } + + toJSON() { + return {} + } +} + +module.exports.Reporter = Reporter From e2cef862c94d6c0e8d8ea3f67df5e33a1dbaa6ae Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 7 Feb 2024 10:47:22 +0100 Subject: [PATCH 004/103] feat(packages/eslint-plugin-sui): add empty rule --- .../src/rules/function-named-parameters.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 packages/eslint-plugin-sui/src/rules/function-named-parameters.js diff --git a/packages/eslint-plugin-sui/src/rules/function-named-parameters.js b/packages/eslint-plugin-sui/src/rules/function-named-parameters.js new file mode 100644 index 000000000..99c991176 --- /dev/null +++ b/packages/eslint-plugin-sui/src/rules/function-named-parameters.js @@ -0,0 +1,47 @@ +/** + * @fileoverview Ensure that our function are using always named parameters + * @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 named parameters', + 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) {} + } + } +} + From 1ce2585af0a11d9dd11a21fc9cd9e7366a3122c3 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Tue, 13 Feb 2024 14:48:21 +0100 Subject: [PATCH 005/103] feat(packages/sui-lint): Add send method to the reporter --- packages/sui-lint/bin/sui-lint-js.js | 2 +- packages/sui-lint/package.json | 2 +- packages/sui-lint/src/reporter.js | 8 +++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index bf7db12fc..87148995e 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -57,7 +57,7 @@ const baseConfig = { if (report) { debugger console.log('[sui-lint] Generating report and send it to DD') - Reporter.create().map(results) + await Reporter.create().map(results).send() } if (fix) { diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index aa29378b1..3df0fb71b 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -14,12 +14,12 @@ "author": "", "license": "MIT", "dependencies": { + "@adv-ui/logger": "^2.14.0", "@babel/core": "7.18.10", "@babel/eslint-parser": "7.18.9", "@babel/eslint-plugin": "7.18.10", "@s-ui/helpers": "1", "@typescript-eslint/eslint-plugin": "5.62.0", - "@adv-ui/logger": "2", "commander": "8.3.0", "eslint": "8.56.0", "eslint-config-prettier": "8.5.0", diff --git a/packages/sui-lint/src/reporter.js b/packages/sui-lint/src/reporter.js index 841fd31ff..6ee4cde63 100644 --- a/packages/sui-lint/src/reporter.js +++ b/packages/sui-lint/src/reporter.js @@ -21,7 +21,13 @@ class Reporter { totalFiles: results.length, ...stats } - debugger + } + + send() { + if(this.#data === undefined){ + throw new Error('[sui-lint] No data to send. Maybe you must call to map before') + } + } toJSON() { From 7d34092f0c083bdb8daefb3c254603196839b7b2 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 14 Feb 2024 12:17:13 +0100 Subject: [PATCH 006/103] feat(packages/sui-lint): Add send method to the reporter --- packages/sui-lint/bin/sui-lint-js.js | 1 - packages/sui-lint/src/reporter.js | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index 87148995e..970e0070f 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -55,7 +55,6 @@ const baseConfig = { const results = await eslint.lintFiles(files) if (report) { - debugger console.log('[sui-lint] Generating report and send it to DD') await Reporter.create().map(results).send() } diff --git a/packages/sui-lint/src/reporter.js b/packages/sui-lint/src/reporter.js index 6ee4cde63..3304667fc 100644 --- a/packages/sui-lint/src/reporter.js +++ b/packages/sui-lint/src/reporter.js @@ -1,6 +1,8 @@ class Reporter { #data + static RULES_TO_SEND = ['sui/'] + static create() { return new Reporter() } @@ -21,12 +23,25 @@ class Reporter { totalFiles: results.length, ...stats } + + return this } send() { if(this.#data === undefined){ throw new Error('[sui-lint] No data to send. Maybe you must call to map before') } + debugger + const {totalFiles, ...rest} = this.#data + const statsEntries = Object.entries(rest).map(entry => { + const [rule, failedFiles] = entry + if(!Reporter.RULES_TO_SEND.some(whiteRule => rule.startsWith(whiteRule))) return false + + return [rule, (failedFiles.length * 100) / totalFiles] + }).filter(Boolean) + + debugger + const stats = Object.fromEntries(statsEntries) } From 2fc202a47f9eefe58bd4e8c68fa9f2101c01dad5 Mon Sep 17 00:00:00 2001 From: Kiko Ruiz Lloret Date: Mon, 26 Feb 2024 10:35:28 +0100 Subject: [PATCH 007/103] feat(packages/sui-test-e2e): make e2e folder optional --- packages/sui-test-e2e/bin/sui-test-e2e.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/sui-test-e2e/bin/sui-test-e2e.js b/packages/sui-test-e2e/bin/sui-test-e2e.js index 32cd8aa1b..70e413fb9 100755 --- a/packages/sui-test-e2e/bin/sui-test-e2e.js +++ b/packages/sui-test-e2e/bin/sui-test-e2e.js @@ -12,7 +12,7 @@ const require = createRequire(import.meta.url) const {version} = require('../package.json') const CWD = process.cwd() -const TESTS_FOLDER = `${CWD}/test-e2e` +const TESTS_FOLDER = `${CWD}/${process.env.E2E_FOLDER ?? 'test-e2e'}` const SCREENSHOTS_FOLDER = `${CWD}/.tmp/test-e2e/screenshots` const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 Chrome/65.0.3325.146 Safari/537.36' @@ -26,14 +26,14 @@ const DEFAULT_CYPRESS_CONFIG = { } const DEFAULT_CYPRESS_CONFIG_FILE_CONTENT = `module.exports = { - e2e: {} + e2e: {} };` const CYPRESS_CONFIG_FILE_WITH_LEGACY_PLUGINS = `const plugins = require('./plugins/index.js') module.exports = { e2e: { setupNodeEvents: plugins - } + } };` const HELP_MESSAGE = ` From 1034a9756903171ea7599895a0cacc363bf97b37 Mon Sep 17 00:00:00 2001 From: Kiko Ruiz Lloret Date: Mon, 26 Feb 2024 11:06:11 +0100 Subject: [PATCH 008/103] docs(packages/sui-test-e2e): add docs --- packages/sui-test-e2e/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/sui-test-e2e/README.md b/packages/sui-test-e2e/README.md index 7b09dfa07..88bab9950 100644 --- a/packages/sui-test-e2e/README.md +++ b/packages/sui-test-e2e/README.md @@ -4,8 +4,7 @@ ## Folder Structure -Your e2e tests must be in a `test-e2e` folder in your project root. -Each test file should follow the pattern: `*Spec.js`. +All your e2e tests must be in a folder in your project, expecting by default to be in the root as `./test-e2e` but it can be renamed with the process env variable `E2E_FOLDER`. Each test file should follow the pattern: `*Spec.js`. ``` . @@ -16,7 +15,7 @@ Each test file should follow the pattern: `*Spec.js`. └── test-e2e ├── fixtures │ └── literals.js - ├-─ + ├-─ └── user └── userSpec.js ``` @@ -32,6 +31,7 @@ npx @s-ui/test-e2e ``` Options: + ``` Usage: sui-test-e2e [options] @@ -62,6 +62,7 @@ Options: Examples: $ sui-test-e2e --baseUrl=http://www.github.com $ sui-test-e2e --baseUrl=http://www.github.com --gui + $ E2E_FOLDER="my-custom-folder" sui-test-e2e --baseUrl=http://www.github.com ``` ### Support files @@ -101,7 +102,6 @@ cy.findByText() [More info here](https://testing-library.com/docs/cypress-testing-library/intro/) - ### Plugin files If you need to have plugins, then create a `./test-e2e/plugins` directory, it will be detected and added to the `cypress.json` configuration. @@ -146,4 +146,4 @@ Cypress can be detected as a robot if your server has that kind of protection or #### `sui-test-e2e --screenshotsOnError` -If defined, any error on your tests will create a screenshot of that moment in the `./.tmp/test-e2e/screenshots` folder of your project. \ No newline at end of file +If defined, any error on your tests will create a screenshot of that moment in the `./.tmp/test-e2e/screenshots` folder of your project. From f3f91f360e5f8bbf0133e74cdc08eda37bf9a020 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Mon, 26 Feb 2024 10:29:01 +0000 Subject: [PATCH 009/103] release(packages/sui-test-e2e): v1.15.0 [skip ci] --- packages/sui-test-e2e/CHANGELOG.md | 14 ++++++++++---- packages/sui-test-e2e/package.json | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/sui-test-e2e/CHANGELOG.md b/packages/sui-test-e2e/CHANGELOG.md index b37866bfb..0c8ae7fd9 100644 --- a/packages/sui-test-e2e/CHANGELOG.md +++ b/packages/sui-test-e2e/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 1.15.0 (2024-02-26) + + +### Features + +* make e2e folder optional ([2fc202a](https://github.com/SUI-Components/sui/commit/2fc202a47f9eefe58bd4e8c68fa9f2101c01dad5)) + + + # 1.14.0 (2022-08-09) @@ -127,7 +136,4 @@ * **packages/sui-test-e2e:** Transform string to number ([f88b937](https://github.com/SUI-Components/sui/commit/f88b93724050a87179b76d9ea3760bab2deaa8fd)) * **packages/sui-test-e2e:** Upgrade beta version ([47b38e3](https://github.com/SUI-Components/sui/commit/47b38e3b36cae7261bd392fc832c8a67189172d6)) * **packages/sui-test-e2e:** Upgrade Cypress version ([d1cb2fc](https://github.com/SUI-Components/sui/commit/d1cb2fcb9d1cfbfda73b83db6e447b9558f2da73)) -* **packages/sui-test-e2e:** Use new config ([7e3a037](https://github.com/SUI-Components/sui/commit/7e3a0379dd767cfdccb8ed898ba317db68e5e06c)) - - - +* **packages/sui-test-e2e:** Use new config ([7e3a037](https://github.com/SUI-Components/sui/commit/7e3a0379dd767cfdccb8ed898ba317db68e5e06c)) \ No newline at end of file diff --git a/packages/sui-test-e2e/package.json b/packages/sui-test-e2e/package.json index 956cdc767..3395436ce 100644 --- a/packages/sui-test-e2e/package.json +++ b/packages/sui-test-e2e/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/test-e2e", - "version": "1.14.0", + "version": "1.15.0", "description": "", "main": "lib/index.js", "type": "module", From ed4035f8b503c9c7eea41e762115fe4e6339ee51 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 28 Feb 2024 15:06:51 +0100 Subject: [PATCH 010/103] feat(packages/sui-lint): Create RepositoryLinter and desactivate eslint-preset-sui rules --- packages/sui-lint/bin/sui-lint-js.js | 15 ++-- packages/sui-lint/bin/sui-lint-repository.js | 36 ++++++++++ packages/sui-lint/eslintrc.js | 23 +++---- packages/sui-lint/package.json | 4 +- packages/sui-lint/repository.config.js | 15 ++++ .../sui-lint/src/RepositoryLinter/Config.js | 23 +++++++ .../sui-lint/src/RepositoryLinter/Context.js | 68 +++++++++++++++++++ .../sui-lint/src/RepositoryLinter/Match.js | 36 ++++++++++ .../sui-lint/src/RepositoryLinter/Results.js | 60 ++++++++++++++++ .../sui-lint/src/RepositoryLinter/Runner.js | 13 ++++ .../sui-lint/src/RepositoryLinter/index.js | 24 +++++++ packages/sui-lint/src/reporter.js | 53 --------------- 12 files changed, 294 insertions(+), 76 deletions(-) create mode 100755 packages/sui-lint/bin/sui-lint-repository.js create mode 100644 packages/sui-lint/repository.config.js create mode 100644 packages/sui-lint/src/RepositoryLinter/Config.js create mode 100644 packages/sui-lint/src/RepositoryLinter/Context.js create mode 100644 packages/sui-lint/src/RepositoryLinter/Match.js create mode 100644 packages/sui-lint/src/RepositoryLinter/Results.js create mode 100644 packages/sui-lint/src/RepositoryLinter/Runner.js create mode 100644 packages/sui-lint/src/RepositoryLinter/index.js delete mode 100644 packages/sui-lint/src/reporter.js diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index 970e0070f..7a566d15a 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -7,18 +7,17 @@ const {checkFilesToLint, getFilesToLint, getGitIgnoredFiles, stageFilesIfRequire const {ESLint} = require('eslint') const config = require('../eslintrc.js') -const {Reporter} = require('../src/reporter.js') program .option('--add-fixes') .option('--staged') .option('--fix', 'fix automatically problems with js files') .option('--ignore-patterns ', 'Path patterns to ignore for linting') - .option('--report', 'Send results to DD using sui-logger') - .option('--pattern ', 'Send results to DD using sui-logger') + .option('--reporter ', 'Send results to DD using sui-logger') + .option('--pattern ', 'Patter of files to lint') .parse(process.argv) -const {addFixes, fix, ignorePatterns = [], staged, pattern, report} = program.opts() +const {addFixes, fix, ignorePatterns = [], staged, pattern, reporter} = program.opts() const {CI} = process.env const EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] @@ -54,9 +53,11 @@ const baseConfig = { const results = await eslint.lintFiles(files) - if (report) { - console.log('[sui-lint] Generating report and send it to DD') - await Reporter.create().map(results).send() + if (reporter) { + console.log('[sui-lint] Sending stats using the reporter ', reporter) + const {Reporter} = await import(reporter) + const reportered = await Reporter.create() + await reportered.map(results).send() } if (fix) { diff --git a/packages/sui-lint/bin/sui-lint-repository.js b/packages/sui-lint/bin/sui-lint-repository.js new file mode 100755 index 000000000..e8d845225 --- /dev/null +++ b/packages/sui-lint/bin/sui-lint-repository.js @@ -0,0 +1,36 @@ +#!/usr/bin/env node +/* eslint-disable no-console */ + +const program = require('commander') +const {RepositoryLinter} = require('../src/RepositoryLinter') + +program + .option('--reporter ', 'Send results to DD using sui-logger') + .option('--output-json ', 'Print messages errors as JSON. Default is a table') + .parse(process.argv) + +const {reporter, outputJson} = program.opts() + +;(async function main() { + const linter = RepositoryLinter.create() + const results = await linter.lint() + + if (outputJson) console.log('\n\n') + if (outputJson) { + results.logJSON() + } else { + results.logTable() + } + if (outputJson) console.log('\n\n') + + if (reporter) { + console.log('\n[sui-lint] Sending stats using the reporter\n\n', reporter) + const {RepositoryReporter} = await import(reporter) + const reportered = RepositoryReporter.create() + await reportered.map(results).send() + results.logMonitorings() + } +})().catch(error => { + process.exitCode = 1 + console.error('[sui-lint]', error) +}) diff --git a/packages/sui-lint/eslintrc.js b/packages/sui-lint/eslintrc.js index b4352b11c..7f089f0e2 100644 --- a/packages/sui-lint/eslintrc.js +++ b/packages/sui-lint/eslintrc.js @@ -175,8 +175,8 @@ module.exports = { 'react-hooks', 'simple-import-sort', 'jest', - 'jest-dom', - 'sui' + 'jest-dom' + // 'sui' ], rules: { ...REACT_RULES, @@ -199,28 +199,21 @@ module.exports = { 'prefer-regex-literals': RULES.WARNING, 'prettier/prettier': [RULES.ERROR, prettierOptions], 'simple-import-sort/imports': [RULES.WARNING, {groups: IMPORT_SORT_GROUPS}], - 'react/jsx-no-bind': RULES.OFF, - 'sui/factory-pattern': RULES.ERROR, - 'sui/serialize-deserialize': RULES.ERROR + 'react/jsx-no-bind': RULES.OFF + // 'sui/factory-pattern': RULES.ERROR, + // 'sui/serialize-deserialize': RULES.ERROR }, overrides: [ { files: ['**/*.+(ts|tsx)'], - extends: ['standard-with-typescript', 'standard-react', 'prettier'], - parser: '@typescript-eslint/parser', + extends: ['standard-with-typescript'], parserOptions: { project: './tsconfig.json' }, rules: { - 'import/extensions': RULES.OFF, 'no-return-await': RULES.OFF, - 'prettier/prettier': [RULES.ERROR, prettierOptions], - 'react/react-in-jsx-scope': RULES.OFF, - 'react/no-unused-prop-types': RULES.OFF, - '@typescript-eslint/explicit-function-return-type': [RULES.OFF, {allowTypedFunctionExpressions: false}], - 'chai-friendly/no-unused-expressions': RULES.ERROR, - '@typescript-eslint/no-unused-expressions': RULES.OFF, - '@typescript-eslint/return-await': RULES.OFF + 'prettier/prettier': RULES.OFF, + 'react/react-in-jsx-scope': RULES.OFF } }, { diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index 3df0fb71b..619497be1 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -14,13 +14,15 @@ "author": "", "license": "MIT", "dependencies": { - "@adv-ui/logger": "^2.14.0", "@babel/core": "7.18.10", "@babel/eslint-parser": "7.18.9", "@babel/eslint-plugin": "7.18.10", "@s-ui/helpers": "1", "@typescript-eslint/eslint-plugin": "5.62.0", "commander": "8.3.0", + "console-table-printer": "2.12.0", + "js-yaml": "4.1.0", + "fast-glob": "3.3.2", "eslint": "8.56.0", "eslint-config-prettier": "8.5.0", "eslint-config-standard": "17.0.0", diff --git a/packages/sui-lint/repository.config.js b/packages/sui-lint/repository.config.js new file mode 100644 index 000000000..a4f0bf935 --- /dev/null +++ b/packages/sui-lint/repository.config.js @@ -0,0 +1,15 @@ +const RULES = { + OFF: 0, + WARNING: 1, + ERROR: 2 +} + +module.exports = { + plugins: ['sui'], + rules: { + 'sui/node-version': RULES.WARNING, + 'sui/react-version': RULES.WARNING, + 'sui/package-lock': RULES.WARNING, + 'sui/github-action': RULES.WARNING + } +} diff --git a/packages/sui-lint/src/RepositoryLinter/Config.js b/packages/sui-lint/src/RepositoryLinter/Config.js new file mode 100644 index 000000000..6def2e744 --- /dev/null +++ b/packages/sui-lint/src/RepositoryLinter/Config.js @@ -0,0 +1,23 @@ +module.exports.Config = class Config { + static create() { + return new Config() + } + + async load() { + const repositoryConfig = require('../../repository.config.js') + + const rules = repositoryConfig.plugins.reduce((acc, pkg) => { + const pkgConfig = require(`lint-repository-${pkg}`) + const rulesEntries = Object.entries(pkgConfig.rules) + .map(([rule, handler]) => { + const key = `${pkg}/${rule}` + const level = repositoryConfig.rules[key] + return level ? [key, {handler, level}] : [] + }) + .filter(([key, value]) => key && value) + return {...acc, ...Object.fromEntries(rulesEntries)} + }, {}) + + return rules + } +} diff --git a/packages/sui-lint/src/RepositoryLinter/Context.js b/packages/sui-lint/src/RepositoryLinter/Context.js new file mode 100644 index 000000000..72ceebb04 --- /dev/null +++ b/packages/sui-lint/src/RepositoryLinter/Context.js @@ -0,0 +1,68 @@ +const {Runner} = require('./Runner') + +const EMPTY = 0 + +module.exports.Context = class Context { + #messages = [] + #monitorings = [] + #handler + #runner + + static create(level, handler, rule) { + return new Context(level, handler, rule, Runner.create()) + } + + constructor(level, handler, rule, runner) { + this.#handler = handler + this.#runner = runner + this.rule = rule + this.level = level + } + + run() { + const assertions = this.#handler.create(this) + const {missmatch = () => {}, ...restAssertions} = assertions + Object.entries(restAssertions).forEach(([key, fn]) => { + const matches = this.#runner.assertion(key) + if (matches.length === EMPTY) { + this._assertion = 'missmatch' + return missmatch(key) + } + this._assertion = key // We cant execute assertions in parallel + fn(matches) + }) + return this + } + + get messages() { + return this.#messages.map(opts => { + let message = this.#handler?.meta?.messages[opts.messageId] ?? opts.messageId + message = Object.entries(opts.data ?? {}).reduce((acc, [key, value]) => { + return acc.replaceAll(`{{${key}}}`, value) + }, message) + return {...opts, message, rule: this.rule, level: this.level} + }) + } + + get signal() { + const _signal = {rule: this.rule, level: this.level} + if (this.#monitorings.length === 0) return _signal + if (this.#monitorings.length === 1) return {..._signal, value: this.#monitorings[0].value} + + if (this.#handler.reduceMonitoring === undefined) + throw new Error(` + [RepositoryLinter Context#signal] If your has call to 'context.monitoring' more than one time in your rule. + You have to create a function 'reduceMonitoring' to be able reduce all of them to 1 value. + `) + + return {rule: this.rule, level: this.level, value: this.#handler?.reduceMonitoring(this.#monitorings)} + } + + report(opts) { + this.#messages.push(opts) + } + + monitoring(value, assertion) { + this.#monitorings.push({assertion: assertion ?? this._assertion, rule: this.rule, value, level: this.level}) + } +} diff --git a/packages/sui-lint/src/RepositoryLinter/Match.js b/packages/sui-lint/src/RepositoryLinter/Match.js new file mode 100644 index 000000000..4dbd19ff5 --- /dev/null +++ b/packages/sui-lint/src/RepositoryLinter/Match.js @@ -0,0 +1,36 @@ +const {extname} = require('path') +const yaml = require('js-yaml') +const fs = require('fs') + +module.exports.Match = class Match { + static create(path) { + const ext = extname(path) + if (!ext && fs.statSync(process.cwd() + '/' + path).isDirectory()) { + return new Match(path, undefined, undefined, true) + } + + let parsed + let raw + switch (ext) { + case '.json': + parsed = require(process.cwd() + '/' + path) + break + case '.yml': + case '.yaml': + parsed = yaml.load(fs.readFileSync(process.cwd() + '/' + path, 'utf8')) + break + default: + raw = fs.readFileSync(process.cwd() + '/' + path, 'utf8') + } + + return new Match(path, parsed, raw, false) + } + + constructor(path, parsed, raw, isDir) { + this.parsed = parsed + this.raw = raw + this.path = path + this.isDir = isDir + this.fullPath = process.cwd() + '/' + path + } +} diff --git a/packages/sui-lint/src/RepositoryLinter/Results.js b/packages/sui-lint/src/RepositoryLinter/Results.js new file mode 100644 index 000000000..079ff3ba6 --- /dev/null +++ b/packages/sui-lint/src/RepositoryLinter/Results.js @@ -0,0 +1,60 @@ +const dedent = require('string-dedent') +const {Table} = require('console-table-printer') + +const COLORS_BY_LEVEL = ['green', 'yellow', 'red'] + +module.exports.Results = class Results { + #executions + #messages + #monitorings + + static create(executions) { + return new Results(executions) + } + + constructor(executions) { + this.#executions = executions + this.#messages = executions.reduce((acc, ctxt) => [...acc, ...ctxt.messages], []) + this.#monitorings = executions.map(ctxt => ctxt.signal) + } + + get monitorings() { + return this.#monitorings + } + + logTable() { + if (this.#messages.length === 0) + return console.log(dedent` + 🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳 + 🥳 Your repository follow all our internal conventions 🥳 + 🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳 + `) + + const p = new Table({ + title: dedent` + Lint Respository messages (green=OFF, yellow=WARNING, red=ERROR) + ` + }) + this.#messages.forEach(msg => + p.addRow({rule: msg.rule, message: msg.message.replaceAll('\n', ' ')}, {color: COLORS_BY_LEVEL[msg.level]}) + ) + p.printTable() + } + + logJSON() { + return console.log(JSON.stringify(this.#messages, null, 2)) + } + + logMonitorings() { + if (this.#monitorings.length === 0) + return console.log(dedent` + + There is not signal to be send to DD. Use 'context.monitoring' to add signals at your execution + + `) + + const p = new Table({title: 'List of Signals that will be send to DD'}) + this.#monitorings.forEach(monitor => p.addRow(monitor)) + p.printTable() + } +} diff --git a/packages/sui-lint/src/RepositoryLinter/Runner.js b/packages/sui-lint/src/RepositoryLinter/Runner.js new file mode 100644 index 000000000..056d3204f --- /dev/null +++ b/packages/sui-lint/src/RepositoryLinter/Runner.js @@ -0,0 +1,13 @@ +const {Match} = require('./Match') +const fg = require('fast-glob') + +module.exports.Runner = class Runner { + static create() { + return new Runner() + } + + assertion(key) { + const files = fg.sync(key, {ignore: ['node_modules'], onlyFiles: false}) ?? [] + return files.map(Match.create) + } +} diff --git a/packages/sui-lint/src/RepositoryLinter/index.js b/packages/sui-lint/src/RepositoryLinter/index.js new file mode 100644 index 000000000..0399abc92 --- /dev/null +++ b/packages/sui-lint/src/RepositoryLinter/index.js @@ -0,0 +1,24 @@ +const {Config} = require('./Config') +const {Context} = require('./Context') +const {Results} = require('./Results') + +module.exports.RepositoryLinter = class RepositoryLinter { + #cofig + + static create() { + return new RepositoryLinter(Config.create()) + } + + constructor(config) { + this.#cofig = config + } + + async lint() { + const rules = await this.#cofig.load() + const executions = Object.entries(rules).map(([rule, {handler, level}]) => + Context.create(level, handler, rule).run() + ) + + return Results.create(executions) + } +} diff --git a/packages/sui-lint/src/reporter.js b/packages/sui-lint/src/reporter.js deleted file mode 100644 index 3304667fc..000000000 --- a/packages/sui-lint/src/reporter.js +++ /dev/null @@ -1,53 +0,0 @@ -class Reporter { - #data - - static RULES_TO_SEND = ['sui/'] - - static create() { - return new Reporter() - } - - map(results) { - const stats = results.reduce((rulesIDs, entry) => { - const repitedRulesFailed = entry.messages.map(message => message.ruleId) - const rulesFailed = new Set([...repitedRulesFailed]) - - rulesFailed.forEach(rule => { - rulesIDs[rule] = rulesIDs[rule] ?? [] - rulesIDs[rule].push(entry.filePath) - }) - return rulesIDs - }, {}) - - this.#data = { - totalFiles: results.length, - ...stats - } - - return this - } - - send() { - if(this.#data === undefined){ - throw new Error('[sui-lint] No data to send. Maybe you must call to map before') - } - debugger - const {totalFiles, ...rest} = this.#data - const statsEntries = Object.entries(rest).map(entry => { - const [rule, failedFiles] = entry - if(!Reporter.RULES_TO_SEND.some(whiteRule => rule.startsWith(whiteRule))) return false - - return [rule, (failedFiles.length * 100) / totalFiles] - }).filter(Boolean) - - debugger - const stats = Object.fromEntries(statsEntries) - - } - - toJSON() { - return {} - } -} - -module.exports.Reporter = Reporter From 75a3684fed86de9d2f3ddaa06c94d6d641b26740 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 28 Feb 2024 15:07:36 +0100 Subject: [PATCH 011/103] refactor(packages/eslint-plugin-sui): eslint format --- .../eslint-plugin-sui/src/rules/function-named-parameters.js | 1 - packages/eslint-plugin-sui/src/rules/serialize-deserialize.js | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/eslint-plugin-sui/src/rules/function-named-parameters.js b/packages/eslint-plugin-sui/src/rules/function-named-parameters.js index 99c991176..d7fcd975a 100644 --- a/packages/eslint-plugin-sui/src/rules/function-named-parameters.js +++ b/packages/eslint-plugin-sui/src/rules/function-named-parameters.js @@ -44,4 +44,3 @@ module.exports = { } } } - diff --git a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js index fc3b6c7f5..82232e19a 100644 --- a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js +++ b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js @@ -75,9 +75,7 @@ module.exports = { const createProps = createProperties.map(i => i.key.name) const toJSONProps = toJSONProperties.map(i => i.key.name) - const missingToJSONProps = createProps.filter( - p => !toJSONProps.find(e => e === p) - ) + const missingToJSONProps = createProps.filter(p => !toJSONProps.find(e => e === p)) if (missingToJSONProps.length) { context.report({ node: toJSON, From 836098d30de4fe2f5420530bd10902d2d2d278cb Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 28 Feb 2024 15:08:50 +0100 Subject: [PATCH 012/103] feat(packages/lint-repository-sui): Create first version of the Golden Path Rules --- packages/lint-repository-sui/README.md | 0 packages/lint-repository-sui/package.json | 17 +++++ packages/lint-repository-sui/src/index.js | 18 +++++ .../src/rules/github-action.js | 71 +++++++++++++++++++ .../src/rules/node-version.js | 60 ++++++++++++++++ .../src/rules/package-lock.js | 34 +++++++++ .../src/rules/react-version.js | 62 ++++++++++++++++ 7 files changed, 262 insertions(+) create mode 100644 packages/lint-repository-sui/README.md create mode 100644 packages/lint-repository-sui/package.json create mode 100644 packages/lint-repository-sui/src/index.js create mode 100644 packages/lint-repository-sui/src/rules/github-action.js create mode 100644 packages/lint-repository-sui/src/rules/node-version.js create mode 100644 packages/lint-repository-sui/src/rules/package-lock.js create mode 100644 packages/lint-repository-sui/src/rules/react-version.js diff --git a/packages/lint-repository-sui/README.md b/packages/lint-repository-sui/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/packages/lint-repository-sui/package.json b/packages/lint-repository-sui/package.json new file mode 100644 index 000000000..97c8dbcb5 --- /dev/null +++ b/packages/lint-repository-sui/package.json @@ -0,0 +1,17 @@ +{ + "name": "lint-repository-sui", + "version": "0.0.0", + "description": "Set of sui lint rules to lint a repository", + "keywords": [ + ], + "author": "Sui", + "main": "./src/index.js", + "exports": "./src/index.js", + "scripts": { + }, + "dependencies": { + }, + "devDependencies": { + } +} + diff --git a/packages/lint-repository-sui/src/index.js b/packages/lint-repository-sui/src/index.js new file mode 100644 index 000000000..3ec72031b --- /dev/null +++ b/packages/lint-repository-sui/src/index.js @@ -0,0 +1,18 @@ +const NodeVersion = require('./rules/node-version.js') +const ReactVersion = require('./rules/react-version.js') +const PackageLock = require('./rules/package-lock.js') +const GithubAction = require('./rules/github-action.js') + +// ------------------------------------------------------------------------------ +// Plugin Definition +// ------------------------------------------------------------------------------ + +// import all rules in lib/rules +module.exports = { + rules: { + 'node-version': NodeVersion, + 'react-version': ReactVersion, + 'package-lock': PackageLock, + 'github-action': GithubAction + } +} diff --git a/packages/lint-repository-sui/src/rules/github-action.js b/packages/lint-repository-sui/src/rules/github-action.js new file mode 100644 index 000000000..402f83a5a --- /dev/null +++ b/packages/lint-repository-sui/src/rules/github-action.js @@ -0,0 +1,71 @@ +const dedent = require('string-dedent') + +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'Check that your repository have properly setup the GHA to CI/CD', + recommended: true, + url: null + }, + fixable: null, + schema: [], + messages: { + missingGithubFolder: dedent` + Every project needs to have a .github/worflows folder to be able to run CI/CD in GHA. + If you are not sure about how do it, please contact with Platform Web. + `, + missingMainWorkflow: dedent` + Every project needs to have a workflow to run on master. + If you are not sure about how do it, please contact with Platform Web. + `, + missingPRWorkflow: dedent` + Every project needs to have a workflow to run on every PR. + If you are not sure about how do it, please contact with Platform Web. + ` + } + }, + reduceMonitoring: function (monitorings) { + return monitorings.reduce((acc, signal) => { + return acc && signal.value + }, true) + }, + create: function (context) { + return { + '.github/_workflows': matches => { + context.monitoring(true) + }, + + '.github/**/main.yml': matches => { + context.monitoring(true) + }, + + '.github/**/pullrequest.yml': matches => { + context.monitoring(true) + }, + + missmatch: key => { + if (key === '.github/_workflows') { + context.report({ + messageId: 'missingGithubFolder' + }) + context.monitoring(false, '.github/workflows') + } + + if (key === '.github/**/main.yml') { + context.report({ + messageId: 'missingMainWorkflow' + }) + context.monitoring(false, '.github/**/main.yml') + } + + if (key === '.github/**/pullrequest.yml') { + context.report({ + messageId: 'missingPRWorkflow' + }) + context.monitoring(false, '.github/**/pullrequest.yml') + } + } + } + } +} diff --git a/packages/lint-repository-sui/src/rules/node-version.js b/packages/lint-repository-sui/src/rules/node-version.js new file mode 100644 index 000000000..0e53b0f68 --- /dev/null +++ b/packages/lint-repository-sui/src/rules/node-version.js @@ -0,0 +1,60 @@ +const dedent = require('string-dedent') + +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'Check that your repository use the latest Node version', + recommended: true, + url: null + }, + fixable: null, + schema: [], + messages: { + moreThanOneNVMRC: dedent` + Your project has more than one .nvmrc file. That can be dangerous. + Please, use onle ONE in the root of your project. + If you are not sure about how do it, please contact with Platform Web. + `, + badNodeVersion: dedent` + Your current Node version is {{version}}. + Please be sure that your repository use the latest Node Version 20. + If you are not sure about how do it, please contact with Platform Web. + `, + noNMVRCFile: dedent` + Every project have to have a .npmrc file to define the node versión. + If you are not sure about how do it, please contact with Platform Web. + ` + } + }, + create: function (context) { + return { + '.nvmrc': matches => { + if (matches.length > 1) { + context.report({ + messageId: 'moreThanOneNVMRC' + }) + return context.monitoring(0) + } + + const [nvmrcMatch] = matches + const version = nvmrcMatch.raw.trim() + if (version !== '20') { + context.report({ + messageId: 'badNodeVersion', + data: {version} + }) + } + + context.monitoring(version) + }, + + missmatch: key => { + context.report({ + messageId: 'noNMVRCFile' + }) + context.monitoring(0) + } + } + } +} diff --git a/packages/lint-repository-sui/src/rules/package-lock.js b/packages/lint-repository-sui/src/rules/package-lock.js new file mode 100644 index 000000000..657fdaca0 --- /dev/null +++ b/packages/lint-repository-sui/src/rules/package-lock.js @@ -0,0 +1,34 @@ +const dedent = require('string-dedent') + +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'Check that your repository have created a package-lock file', + recommended: true, + url: null + }, + fixable: null, + schema: [], + messages: { + missingPackageLock: dedent` + Every project needs to have a package-lock.json file to be used in CI/CD. + If you are not sure about how do it, please contact with Platform Web. + ` + } + }, + create: function (context) { + return { + 'package-lock.json': matches => { + context.monitoring(true) + }, + + missmatch: key => { + context.report({ + messageId: 'missingPackageLock' + }) + context.monitoring(false) + } + } + } +} diff --git a/packages/lint-repository-sui/src/rules/react-version.js b/packages/lint-repository-sui/src/rules/react-version.js new file mode 100644 index 000000000..38411eb7f --- /dev/null +++ b/packages/lint-repository-sui/src/rules/react-version.js @@ -0,0 +1,62 @@ +const dedent = require('string-dedent') + +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'Check that your repository use the latest React version', + recommended: true, + url: null + }, + fixable: null, + schema: [], + messages: { + badReactVersion: dedent` + Please be sure that your repository use the latest React Version 18. + Your current version is {{version}}. + If you are not sure about how do it, please contact with Platform Web. + `, + missingReactDependencie: dedent` + Your project doesnt have installed React. + Please install at least the version 18. + If you are not sure about how do it, please contact with Platform Web. + `, + missingPackageLock: dedent` + To calculate the react version first we need to have a package-lock.json in the root + If you are not sure about how do it, please contact with Platform Web. + ` + } + }, + create: function (context) { + return { + 'package-lock.json': matches => { + const [packageLock] = matches + let version = packageLock?.parsed?.packages?.['node_modules/react']?.version + + if (!version) { + context.report({ + messageId: 'missingReactDependencie' + }) + return context.monitoring(0) + } + + version = version.split('.')[0] + + if (version !== '18') { + context.report({ + messageId: 'badReactVersion', + data: {version} + }) + } + return context.monitoring(version) + }, + + missmatch: key => { + context.report({ + messageId: 'missingPackageLock' + }) + context.monitoring(0) + } + } + } +} From 07293da8db0823911b3a6f7518d5de4fc1cbf24d Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 28 Feb 2024 15:47:50 +0100 Subject: [PATCH 013/103] fix(packages/sui-lint): Fix JSReporter import and plugins version --- packages/sui-lint/bin/sui-lint-js.js | 4 ++-- packages/sui-lint/bin/sui-lint.js | 2 +- packages/sui-lint/package.json | 2 ++ 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index 7a566d15a..91a826d04 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -55,8 +55,8 @@ const baseConfig = { if (reporter) { console.log('[sui-lint] Sending stats using the reporter ', reporter) - const {Reporter} = await import(reporter) - const reportered = await Reporter.create() + const {JSReporter} = await import(reporter) + const reportered = await JSReporter.create() await reportered.map(results).send() } diff --git a/packages/sui-lint/bin/sui-lint.js b/packages/sui-lint/bin/sui-lint.js index 78c90c0aa..61623f84c 100755 --- a/packages/sui-lint/bin/sui-lint.js +++ b/packages/sui-lint/bin/sui-lint.js @@ -4,6 +4,6 @@ const {version} = require('../package.json') program.version(version, ' --version') -program.command('js', 'lint javascript files').command('sass', 'lint sass files') +program.command('js', 'lint javascript files').command('sass', 'lint sass files').command('repository', 'lint repository structure') program.parse(process.argv) diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index 619497be1..1839a1dbe 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -39,6 +39,8 @@ "eslint-plugin-react": "7.30.1", "eslint-plugin-react-hooks": "4.6.0", "eslint-plugin-simple-import-sort": "7.0.0", + "eslint-plugin-sui": "1", + "lint-repository-sui": "1", "postcss-scss": "4.0.4", "prettier": "2.7.1", "stylelint": "14.11.0", From 36a3830f0d0056d0dbc01dbd526ce8261fdb723d Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 28 Feb 2024 16:01:07 +0100 Subject: [PATCH 014/103] fix(packages/lint-repository-sui): Fix typo in rule --- packages/lint-repository-sui/src/rules/github-action.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/lint-repository-sui/src/rules/github-action.js b/packages/lint-repository-sui/src/rules/github-action.js index 402f83a5a..5ace0db38 100644 --- a/packages/lint-repository-sui/src/rules/github-action.js +++ b/packages/lint-repository-sui/src/rules/github-action.js @@ -32,7 +32,7 @@ module.exports = { }, create: function (context) { return { - '.github/_workflows': matches => { + '.github/workflows': matches => { context.monitoring(true) }, @@ -45,7 +45,7 @@ module.exports = { }, missmatch: key => { - if (key === '.github/_workflows') { + if (key === '.github/workflows') { context.report({ messageId: 'missingGithubFolder' }) From 02cd409e73ac28dc21bda52c1229adcde74fc28d Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 28 Feb 2024 16:04:21 +0100 Subject: [PATCH 015/103] fix(packages/sui-lint): Fix version --- packages/sui-lint/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index 1839a1dbe..9f4181b1e 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -39,8 +39,8 @@ "eslint-plugin-react": "7.30.1", "eslint-plugin-react-hooks": "4.6.0", "eslint-plugin-simple-import-sort": "7.0.0", - "eslint-plugin-sui": "1", - "lint-repository-sui": "1", + "eslint-plugin-sui": "beta", + "lint-repository-sui": "beta", "postcss-scss": "4.0.4", "prettier": "2.7.1", "stylelint": "14.11.0", From e0fd01378a1f3c381d99802d1feb827d55f0eae3 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 28 Feb 2024 16:15:01 +0100 Subject: [PATCH 016/103] fix(packages/sui-lint): Fix lint --- packages/sui-lint/bin/sui-lint.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/sui-lint/bin/sui-lint.js b/packages/sui-lint/bin/sui-lint.js index 61623f84c..1f2816297 100755 --- a/packages/sui-lint/bin/sui-lint.js +++ b/packages/sui-lint/bin/sui-lint.js @@ -4,6 +4,8 @@ const {version} = require('../package.json') program.version(version, ' --version') -program.command('js', 'lint javascript files').command('sass', 'lint sass files').command('repository', 'lint repository structure') +program.command('js', 'lint javascript files') + .command('sass', 'lint sass files') + .command('repository', 'lint repository structure') program.parse(process.argv) From 0836810202a370e600fd1cb25250a18595aa7e81 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 28 Feb 2024 16:31:01 +0100 Subject: [PATCH 017/103] fix(packages/sui-lint): Fix lint --- packages/sui-lint/bin/sui-lint.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/sui-lint/bin/sui-lint.js b/packages/sui-lint/bin/sui-lint.js index 1f2816297..12bd4bbb9 100755 --- a/packages/sui-lint/bin/sui-lint.js +++ b/packages/sui-lint/bin/sui-lint.js @@ -4,7 +4,8 @@ const {version} = require('../package.json') program.version(version, ' --version') -program.command('js', 'lint javascript files') +program + .command('js', 'lint javascript files') .command('sass', 'lint sass files') .command('repository', 'lint repository structure') From 74751db22552649b6cfb94410c7e4be3194200ed Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Fri, 1 Mar 2024 12:09:57 +0100 Subject: [PATCH 018/103] test(packages/eslint-plugin-sui): Add more tests --- packages/eslint-plugin-sui/package.json | 3 +- .../tests/src/rules/factory-pattern.js | 37 ++++--- .../tests/src/rules/serialize-deserialize.js | 96 +++++++++++++++++++ 3 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 packages/eslint-plugin-sui/tests/src/rules/serialize-deserialize.js diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index 99d36c4a1..bb67458ac 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -14,7 +14,7 @@ "lint": "npm-run-all \"lint:*\"", "lint:eslint-docs": "npm-run-all \"update:eslint-docs -- --check\"", "lint:js": "eslint .", - "test": "mocha tests --recursive", + "test": "npx sui-test server -P ./tests/**/*.js", "update:eslint-docs": "eslint-doc-generator" }, "dependencies": { @@ -22,6 +22,7 @@ "string-dedent": "^3.0.1" }, "devDependencies": { + "@s-ui/test": "^8.33.0", "eslint": "^8.19.0", "eslint-doc-generator": "^1.0.0", "eslint-plugin-eslint-plugin": "^5.0.0", diff --git a/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js b/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js index e04d54faa..9f961d232 100644 --- a/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js +++ b/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js @@ -1,30 +1,41 @@ -/** - * @fileoverview Ensure that our classes are using the convetion of has a static create method as factory. - * @author factory pattern - */ -'use strict' - -// ------------------------------------------------------------------------------ -// Requirements -// ------------------------------------------------------------------------------ +import dedent from 'dedent' import {RuleTester} from 'eslint' import rule from '../../../src/rules/factory-pattern' // ------------------------------------------------------------------------------ // Tests +// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester // ------------------------------------------------------------------------------ -const ruleTester = new RuleTester() +const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) ruleTester.run('factory-pattern', rule, { valid: [ - // give me some code that won't trigger a warning + { + code: dedent` + class User { + static create() { return new User() } + } + ` + } ], invalid: [ { - code: "class Model { constructor() { this.name = 'John Doe' } }", - errors: [{message: 'Fill me in.', type: 'Me too'}] + code: dedent` + class Model { + constructor() { this.name = 'John Doe' } + }`, + errors: [ + { + message: 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 + `, + type: 'ClassDeclaration' + } + ] } ] }) diff --git a/packages/eslint-plugin-sui/tests/src/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/tests/src/rules/serialize-deserialize.js new file mode 100644 index 000000000..b9d952b68 --- /dev/null +++ b/packages/eslint-plugin-sui/tests/src/rules/serialize-deserialize.js @@ -0,0 +1,96 @@ +import dedent from 'dedent' +import {RuleTester} from 'eslint' + +import rule from '../../../src/rules/serialize-deserialize' + +// ------------------------------------------------------------------------------ +// Tests +// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester +// ------------------------------------------------------------------------------ + +const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) +ruleTester.run('serialize-deserialize', rule, { + valid: [ + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + id: this.id, + name: this.name + } + } + } + ` + } + ], + + invalid: [ + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return this.id + } + } + `, + errors: [ + { + message: dedent`toJSON should return an object` + } + ] + }, + { + code: dedent` + class User { + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + Noid: this.id, + Noname: this.name + } + } + } + `, + errors: [ + { + message: dedent` + If your class has a 'toJSON' method. You have to define a 'static create' method too. + The output of the 'toJSON' should be the same as the input of your 'static create' method + ` + } + ] + }, + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + Noid: this.id, + Noname: this.name + } + } + } + `, + errors: [{message: 'Missing toJSON properties (id, name)'}] + } + ] +}) From d057f605e68a921a8018c790e7b76d3e94afbdde Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Fri, 1 Mar 2024 12:11:53 +0100 Subject: [PATCH 019/103] test(packages/lint-repository-sui): Add firsts tests Create a custom test runner following the ESLint API --- packages/lint-repository-sui/package.json | 8 +-- .../lint-repository-sui/test/TestHelpers.js | 64 +++++++++++++++++++ .../test/rules/github-actionSpec.js | 53 +++++++++++++++ .../test/rules/node-versionSpec.js | 44 +++++++++++++ .../test/rules/package-lockSpec.js | 25 ++++++++ .../test/rules/react-versionSpec.js | 44 +++++++++++++ 6 files changed, 233 insertions(+), 5 deletions(-) create mode 100644 packages/lint-repository-sui/test/TestHelpers.js create mode 100644 packages/lint-repository-sui/test/rules/github-actionSpec.js create mode 100644 packages/lint-repository-sui/test/rules/node-versionSpec.js create mode 100644 packages/lint-repository-sui/test/rules/package-lockSpec.js create mode 100644 packages/lint-repository-sui/test/rules/react-versionSpec.js diff --git a/packages/lint-repository-sui/package.json b/packages/lint-repository-sui/package.json index 97c8dbcb5..46455cfa6 100644 --- a/packages/lint-repository-sui/package.json +++ b/packages/lint-repository-sui/package.json @@ -2,16 +2,14 @@ "name": "lint-repository-sui", "version": "0.0.0", "description": "Set of sui lint rules to lint a repository", - "keywords": [ - ], + "keywords": [], "author": "Sui", "main": "./src/index.js", "exports": "./src/index.js", "scripts": { - }, - "dependencies": { + "test": "npx sui-test server" }, "devDependencies": { + "@s-ui/test": "^8.33.0" } } - diff --git a/packages/lint-repository-sui/test/TestHelpers.js b/packages/lint-repository-sui/test/TestHelpers.js new file mode 100644 index 000000000..ce34922d1 --- /dev/null +++ b/packages/lint-repository-sui/test/TestHelpers.js @@ -0,0 +1,64 @@ +import {expect} from 'chai' +import {stub} from 'sinon' +export class RuleTester { + id + handler + + static create(id, handler) { + return new RuleTester(id, handler) + } + + constructor(id, handler) { + this.handler = handler + this.id = id + } + + run(assertions) { + const instance = this + + Object.entries(assertions).forEach(([kind, tests]) => { + describe(`[${kind.toUpperCase()}] ${this.id}`, function () { + beforeEach(function () { + this.ctxt = { + report: stub(), + monitoring: stub() + } + }) + afterEach(function () { + this.ctxt.report.reset() + this.ctxt.monitoring.reset() + }) + + tests.forEach(assertion => { + const {monitoring, report, name, ...rest} = assertion + Object.entries(rest).forEach(([FSPattern, matches]) => { + it(name ?? FSPattern, function () { + instance.handler.create(this.ctxt)[FSPattern](matches) + monitoring && expect(this.ctxt.monitoring.calledWith(monitoring)).to.be.eql(true) + report && expect(instance._formatMessages(this.ctxt.report)).to.be.eqls(report) + expect(true).to.be.eql(true) + }) + }) + }) + }) + }) + } + + _formatMessages(stub) { + const report = stub.firstCall.firstArg + return Object.entries(report.data ?? {}).reduce((acc, [key, value]) => { + return acc.replaceAll(`{{${key}}}`, value) + }, this.handler.meta.messages[report.messageId]) + } +} + +export class MatchStub { + static create({parsed, raw}) { + return new MatchStub(parsed, raw) + } + + constructor(parsed, raw) { + this.parsed = parsed + this.raw = raw + } +} diff --git a/packages/lint-repository-sui/test/rules/github-actionSpec.js b/packages/lint-repository-sui/test/rules/github-actionSpec.js new file mode 100644 index 000000000..17f4ecb5e --- /dev/null +++ b/packages/lint-repository-sui/test/rules/github-actionSpec.js @@ -0,0 +1,53 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/github-action.js' +import {RuleTester} from '../TestHelpers' + +RuleTester.create('github-action', handler).run({ + valid: [ + { + '.github/workflows': [], + name: 'The porject has define a worflows folder', + monitoring: true + }, + { + '.github/**/main.yml': [], + name: 'The porject has define a worflow for the main branch', + monitoring: true + }, + { + '.github/**/pullrequest.yml': [], + name: 'The porject has define a worflow for PRs', + monitoring: true + } + ], + invalid: [ + { + missmatch: '.github/workflows', + name: 'The porject has NOT define a worflows folder', + report: dedent` + Every project needs to have a .github/worflows folder to be able to run CI/CD in GHA. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + }, + { + missmatch: '.github/**/main.yml', + name: 'The porject has NOT define a worflow for the main branch', + report: dedent` + Every project needs to have a workflow to run on master. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + }, + { + missmatch: '.github/**/pullrequest.yml', + name: 'The porject has NOT define a worflow for PRs', + report: dedent` + Every project needs to have a workflow to run on every PR. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + } + ] +}) diff --git a/packages/lint-repository-sui/test/rules/node-versionSpec.js b/packages/lint-repository-sui/test/rules/node-versionSpec.js new file mode 100644 index 000000000..380bfcbd2 --- /dev/null +++ b/packages/lint-repository-sui/test/rules/node-versionSpec.js @@ -0,0 +1,44 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/node-version.js' +import {MatchStub, RuleTester} from '../TestHelpers' + +RuleTester.create('node-version', handler).run({ + valid: [ + { + '.nvmrc': [MatchStub.create({raw: '20'})], + name: 'nvmrc Exists and has setup the version 20', + monitoring: '20' + } + ], + invalid: [ + { + '.nvmrc': [MatchStub.create({raw: '20'}), MatchStub.create({raw: 17})], + name: 'Exits more than one nvmrc file', + report: dedent` + Your project has more than one .nvmrc file. That can be dangerous. + Please, use onle ONE in the root of your project. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + }, + { + '.nvmrc': [MatchStub.create({raw: '16'})], + name: 'Exits more than one nvmrc file', + report: dedent` + Your current Node version is 16. + Please be sure that your repository use the latest Node Version 20. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + }, + { + missmatch: '', + report: dedent` + Every project have to have a .npmrc file to define the node versión. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + } + ] +}) diff --git a/packages/lint-repository-sui/test/rules/package-lockSpec.js b/packages/lint-repository-sui/test/rules/package-lockSpec.js new file mode 100644 index 000000000..dd3cc21a0 --- /dev/null +++ b/packages/lint-repository-sui/test/rules/package-lockSpec.js @@ -0,0 +1,25 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/package-lock.js' +import {RuleTester} from '../TestHelpers' + +RuleTester.create('package-lock', handler).run({ + valid: [ + { + 'package-lock.json': [], + name: 'Project has package-lock.json in the root folder', + monitoring: true + } + ], + invalid: [ + { + missmatch: '', + name: 'Project doesnt has package-lock in the root folder', + report: dedent` + Every project needs to have a package-lock.json file to be used in CI/CD. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + } + ] +}) diff --git a/packages/lint-repository-sui/test/rules/react-versionSpec.js b/packages/lint-repository-sui/test/rules/react-versionSpec.js new file mode 100644 index 000000000..a7a50ca49 --- /dev/null +++ b/packages/lint-repository-sui/test/rules/react-versionSpec.js @@ -0,0 +1,44 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/react-version.js' +import {MatchStub, RuleTester} from '../TestHelpers' + +RuleTester.create('react-version', handler).run({ + valid: [ + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/react': {version: '18.0.0'}}}})], + name: 'React 18 installed', + monitoring: '18' + } + ], + invalid: [ + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {}}})], + name: 'React not installed', + report: dedent` + Your project doesnt have installed React. + Please install at least the version 18. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + }, + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/react': {version: '17.0.0'}}}})], + name: 'React wrong version', + report: dedent` + Please be sure that your repository use the latest React Version 18. + Your current version is 17. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: '17' + }, + { + missmatch: '', + report: dedent` + To calculate the react version first we need to have a package-lock.json in the root + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + } + ] +}) From 3a8a6d58955d7f3d4fce97db4cd03223d079ac81 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Fri, 1 Mar 2024 14:43:16 +0100 Subject: [PATCH 020/103] test(packages/sui-lint): Add firsts tests to the RepositoryLinter --- .../sui-lint/src/RepositoryLinter/Config.js | 12 ++- .../sui-lint/src/RepositoryLinter/Match.js | 22 ++++- .../sui-lint/src/RepositoryLinter/Results.js | 19 ++-- .../sui-lint/src/RepositoryLinter/Runner.js | 12 ++- .../sui-lint/src/RepositoryLinter/index.js | 4 +- .../test/RepositoryLinter/ConfigSpec.js | 38 +++++++ .../test/RepositoryLinter/MatchSpec.js | 99 +++++++++++++++++++ .../test/RepositoryLinter/ResultsSpec.js | 44 +++++++++ .../test/RepositoryLinter/indexSpec.js | 0 9 files changed, 229 insertions(+), 21 deletions(-) create mode 100644 packages/sui-lint/test/RepositoryLinter/ConfigSpec.js create mode 100644 packages/sui-lint/test/RepositoryLinter/MatchSpec.js create mode 100644 packages/sui-lint/test/RepositoryLinter/ResultsSpec.js create mode 100644 packages/sui-lint/test/RepositoryLinter/indexSpec.js diff --git a/packages/sui-lint/src/RepositoryLinter/Config.js b/packages/sui-lint/src/RepositoryLinter/Config.js index 6def2e744..57d09eaf5 100644 --- a/packages/sui-lint/src/RepositoryLinter/Config.js +++ b/packages/sui-lint/src/RepositoryLinter/Config.js @@ -4,10 +4,10 @@ module.exports.Config = class Config { } async load() { - const repositoryConfig = require('../../repository.config.js') + const repositoryConfig = this.requireConfig() const rules = repositoryConfig.plugins.reduce((acc, pkg) => { - const pkgConfig = require(`lint-repository-${pkg}`) + const pkgConfig = this.requirePkg(pkg) const rulesEntries = Object.entries(pkgConfig.rules) .map(([rule, handler]) => { const key = `${pkg}/${rule}` @@ -20,4 +20,12 @@ module.exports.Config = class Config { return rules } + + requireConfig() { + return require('../../repository.config.js') + } + + requirePkg(pkg) { + return require(`lint-repository-${pkg}`) + } } diff --git a/packages/sui-lint/src/RepositoryLinter/Match.js b/packages/sui-lint/src/RepositoryLinter/Match.js index 4dbd19ff5..73c8b6e4a 100644 --- a/packages/sui-lint/src/RepositoryLinter/Match.js +++ b/packages/sui-lint/src/RepositoryLinter/Match.js @@ -2,10 +2,19 @@ const {extname} = require('path') const yaml = require('js-yaml') const fs = require('fs') -module.exports.Match = class Match { +class CustomFileReader { + static create() { return new CustomFileReader() } // eslint-disable-line + + isDirectory(path) { return fs.statSync(process.cwd() + '/' + path).isDirectory() } // eslint-disable-line + parseYML(path) { return yaml.load(fs.readFileSync(process.cwd() + '/' + path, 'utf8')) } // eslint-disable-line + parseJSON(path) { return require(process.cwd() + '/' + path) } // eslint-disable-line + raw(path) { return fs.readFileSync(process.cwd() + '/' + path, 'utf8') } // eslint-disable-line +} + +class Match { static create(path) { const ext = extname(path) - if (!ext && fs.statSync(process.cwd() + '/' + path).isDirectory()) { + if (!ext && CustomFileReader.create().isDirectory(path)) { return new Match(path, undefined, undefined, true) } @@ -13,14 +22,14 @@ module.exports.Match = class Match { let raw switch (ext) { case '.json': - parsed = require(process.cwd() + '/' + path) + parsed = CustomFileReader.create().parseJSON(path) break case '.yml': case '.yaml': - parsed = yaml.load(fs.readFileSync(process.cwd() + '/' + path, 'utf8')) + parsed = CustomFileReader.create().parseYML(path) break default: - raw = fs.readFileSync(process.cwd() + '/' + path, 'utf8') + raw = CustomFileReader.create().raw(path) } return new Match(path, parsed, raw, false) @@ -34,3 +43,6 @@ module.exports.Match = class Match { this.fullPath = process.cwd() + '/' + path } } + +module.exports.CustomFileReader = CustomFileReader +module.exports.Match = Match diff --git a/packages/sui-lint/src/RepositoryLinter/Results.js b/packages/sui-lint/src/RepositoryLinter/Results.js index 079ff3ba6..6b837ca75 100644 --- a/packages/sui-lint/src/RepositoryLinter/Results.js +++ b/packages/sui-lint/src/RepositoryLinter/Results.js @@ -8,6 +8,12 @@ module.exports.Results = class Results { #messages #monitorings + static HAPPY_MESSAGE = dedent` + 🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳 + 🥳 Your repository follow all our internal conventions 🥳 + 🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳 + ` + static create(executions) { return new Results(executions) } @@ -22,13 +28,10 @@ module.exports.Results = class Results { return this.#monitorings } + log(msg) { console.log(msg) } // eslint-disable-line + logTable() { - if (this.#messages.length === 0) - return console.log(dedent` - 🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳 - 🥳 Your repository follow all our internal conventions 🥳 - 🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳 - `) + if (this.#messages.length === 0) return this.log(Results.HAPPY_MESSAGE) const p = new Table({ title: dedent` @@ -42,12 +45,12 @@ module.exports.Results = class Results { } logJSON() { - return console.log(JSON.stringify(this.#messages, null, 2)) + return this.log(JSON.stringify(this.#messages, null, 2)) } logMonitorings() { if (this.#monitorings.length === 0) - return console.log(dedent` + return this.log(dedent` There is not signal to be send to DD. Use 'context.monitoring' to add signals at your execution diff --git a/packages/sui-lint/src/RepositoryLinter/Runner.js b/packages/sui-lint/src/RepositoryLinter/Runner.js index 056d3204f..8c3ffc81f 100644 --- a/packages/sui-lint/src/RepositoryLinter/Runner.js +++ b/packages/sui-lint/src/RepositoryLinter/Runner.js @@ -1,13 +1,17 @@ const {Match} = require('./Match') -const fg = require('fast-glob') +const fastGlob = require('fast-glob') module.exports.Runner = class Runner { - static create() { - return new Runner() + static create(fg) { + return new Runner(fg ?? fastGlob) + } + + constructor(fg) { + this.fg = fg } assertion(key) { - const files = fg.sync(key, {ignore: ['node_modules'], onlyFiles: false}) ?? [] + const files = this.fg.sync(key, {ignore: ['node_modules'], onlyFiles: false}) ?? [] return files.map(Match.create) } } diff --git a/packages/sui-lint/src/RepositoryLinter/index.js b/packages/sui-lint/src/RepositoryLinter/index.js index 0399abc92..0178a72d3 100644 --- a/packages/sui-lint/src/RepositoryLinter/index.js +++ b/packages/sui-lint/src/RepositoryLinter/index.js @@ -5,8 +5,8 @@ const {Results} = require('./Results') module.exports.RepositoryLinter = class RepositoryLinter { #cofig - static create() { - return new RepositoryLinter(Config.create()) + static create(config) { + return new RepositoryLinter(config ?? Config.create()) } constructor(config) { diff --git a/packages/sui-lint/test/RepositoryLinter/ConfigSpec.js b/packages/sui-lint/test/RepositoryLinter/ConfigSpec.js new file mode 100644 index 000000000..2960d8d0a --- /dev/null +++ b/packages/sui-lint/test/RepositoryLinter/ConfigSpec.js @@ -0,0 +1,38 @@ +import {expect} from 'chai' +import {stub} from 'sinon' + +import {Config} from '../../src/RepositoryLinter/Config.js' + +describe('Config', function () { + beforeEach(function () { + this.requireConfigStub = stub(Config.prototype, 'requireConfig') + this.requirePkgStub = stub(Config.prototype, 'requirePkg') + }) + + afterEach(function () { + this.requireConfigStub.reset() + this.requirePkgStub.reset() + }) + + it('Should return a rules object', async function () { + // Given + const handler = { + meta: {messages: {badVersion: 'Your react version is not 20'}}, + create: function () {} + } + this.requireConfigStub.returns({ + plugins: ['tester'], + rules: { + 'tester/react-version': 1 + } + }) + this.requirePkgStub.returns({rules: {'react-version': handler}}) + + // When + const rulesLoaded = await Config.create().load() + + // Then + expect(this.requirePkgStub.calledWith('tester')).to.be.eql(true) + expect(rulesLoaded).to.be.eqls({'tester/react-version': {handler, level: 1}}) + }) +}) diff --git a/packages/sui-lint/test/RepositoryLinter/MatchSpec.js b/packages/sui-lint/test/RepositoryLinter/MatchSpec.js new file mode 100644 index 000000000..5db0c60c7 --- /dev/null +++ b/packages/sui-lint/test/RepositoryLinter/MatchSpec.js @@ -0,0 +1,99 @@ +import {expect} from 'chai' +import {stub} from 'sinon' + +import {CustomFileReader, Match} from '../../src/RepositoryLinter/Match.js' + +describe('Match', function () { + beforeEach(function () { + this.isDirStub = stub(CustomFileReader.prototype, 'isDirectory') + + this.parseYMLStub = stub(CustomFileReader.prototype, 'parseYML') + this.parseJSONStub = stub(CustomFileReader.prototype, 'parseJSON') + this.rawStub = stub(CustomFileReader.prototype, 'raw') + }) + + afterEach(function () { + this.isDirStub.restore() + + this.parseYMLStub.restore() + this.parseJSONStub.restore() + this.rawStub.restore() + }) + + it('Should detect directories', function () { + // Given + this.isDirStub.returns(true) + + // When + const match = Match.create('/dir/path') + + // Then + expect(match.isDir).to.be.eqls(true) + }) + + it('Should detect files w/out extensions', function () { + // Given + this.isDirStub.returns(false) + this.rawStub.returns('20') + + // When + const match = Match.create('/dir/path') + + // Then + expect(match.isDir).to.be.eqls(false) + expect(this.rawStub.calledWith('/dir/path')).to.be.eql(true) + }) + + it('Should parse JSON files', function () { + // Given + this.isDirStub.returns(false) + this.parseJSONStub.returns({a: 1}) + + // When + const match = Match.create('/dir/file.json') + + // Then + expect(match.parsed).to.be.eqls({a: 1}) + expect(this.parseJSONStub.calledWith('/dir/file.json')).to.be.eql(true) + }) + + it('Should parse yml files', function () { + // Given + this.isDirStub.returns(false) + this.parseYMLStub.returns({a: 1}) + + // When + const match = Match.create('/dir/file.yml') + + // Then + expect(match.parsed).to.be.eqls({a: 1}) + expect(this.parseYMLStub.calledWith('/dir/file.yml')).to.be.eql(true) + }) + + it('Should parse yaml files', function () { + // Given + this.isDirStub.returns(false) + this.parseYMLStub.returns({a: 1}) + + // When + const match = Match.create('/dir/file.yaml') + + // Then + expect(match.parsed).to.be.eqls({a: 1}) + expect(this.parseYMLStub.calledWith('/dir/file.yaml')).to.be.eql(true) + }) + + it('Should read unkown files', function () { + // Given + this.isDirStub.returns(false) + this.rawStub.returns('Hello') + + // When + const match = Match.create('/dir/file.txt') + + // Then + expect(match.raw).to.be.eqls('Hello') + expect(match.parsed).to.be.eqls(undefined) + expect(this.rawStub.calledWith('/dir/file.txt')).to.be.eql(true) + }) +}) diff --git a/packages/sui-lint/test/RepositoryLinter/ResultsSpec.js b/packages/sui-lint/test/RepositoryLinter/ResultsSpec.js new file mode 100644 index 000000000..311911f23 --- /dev/null +++ b/packages/sui-lint/test/RepositoryLinter/ResultsSpec.js @@ -0,0 +1,44 @@ +import {expect} from 'chai' +import {Table} from 'console-table-printer' +import {stub} from 'sinon' + +import {Results} from '../../src/RepositoryLinter/Results.js' + +describe('Results', function () { + beforeEach(function () { + this.addRowStub = stub(Table.prototype, 'addRow') + this.printTableStub = stub(Table.prototype, 'printTable') + + this.logStub = stub(Results.prototype, 'log') + }) + + afterEach(function () { + this.addRowStub.restore() + this.printTableStub.restore() + + this.logStub.restore() + }) + + it('Should print Happy Message it there is not messages', function () { + const executions = [ + { + messages: [], + signal: false + } + ] + Results.create(executions).logTable() + + expect(this.logStub.calledWith(Results.HAPPY_MESSAGE)).to.be.eq(true) + }) + + it('Should print a table with all the messages', function () { + const executions = [ + {messages: [{rule: 'tester/node-version', message: 'Node version fail', level: 1}], signal: 12}, + {messages: [{rule: 'tester/react-version', message: 'React version fail', level: 2}], signal: 17}, + {messages: [], signal: true} + ] + Results.create(executions).logTable() + + // expect(this.logStub.calledWith(Results.HAPPY_MESSAGE)).to.be.eq(true) + }) +}) diff --git a/packages/sui-lint/test/RepositoryLinter/indexSpec.js b/packages/sui-lint/test/RepositoryLinter/indexSpec.js new file mode 100644 index 000000000..e69de29bb From 0727399c1a348b3e2d16a0973a82953701683403 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 14:45:04 +0100 Subject: [PATCH 021/103] test(packages/sui-lint): Add more unit test for the RepositoryLinter package --- .../sui-lint/src/RepositoryLinter/Context.js | 15 +- .../sui-lint/src/RepositoryLinter/Match.js | 4 + .../test/RepositoryLinter/ContextSpec.js | 206 ++++++++++++++++++ .../test/RepositoryLinter/ResultsSpec.js | 41 +++- .../test/RepositoryLinter/RunnerSpec.js | 26 +++ .../test/RepositoryLinter/indexSpec.js | 0 6 files changed, 284 insertions(+), 8 deletions(-) create mode 100644 packages/sui-lint/test/RepositoryLinter/ContextSpec.js create mode 100644 packages/sui-lint/test/RepositoryLinter/RunnerSpec.js delete mode 100644 packages/sui-lint/test/RepositoryLinter/indexSpec.js diff --git a/packages/sui-lint/src/RepositoryLinter/Context.js b/packages/sui-lint/src/RepositoryLinter/Context.js index 72ceebb04..e04f769f6 100644 --- a/packages/sui-lint/src/RepositoryLinter/Context.js +++ b/packages/sui-lint/src/RepositoryLinter/Context.js @@ -8,8 +8,13 @@ module.exports.Context = class Context { #handler #runner - static create(level, handler, rule) { - return new Context(level, handler, rule, Runner.create()) + static MISSING_REDUCER_MONITORING_MSG = ` + [RepositoryLinter Context#signal] If your has call to 'context.monitoring' more than one time in your rule. + You have to create a function 'reduceMonitoring' to be able reduce all of them to 1 value. + ` + + static create(level, handler, rule, runner) { + return new Context(level, handler, rule, runner ?? Runner.create()) } constructor(level, handler, rule, runner) { @@ -49,11 +54,7 @@ module.exports.Context = class Context { if (this.#monitorings.length === 0) return _signal if (this.#monitorings.length === 1) return {..._signal, value: this.#monitorings[0].value} - if (this.#handler.reduceMonitoring === undefined) - throw new Error(` - [RepositoryLinter Context#signal] If your has call to 'context.monitoring' more than one time in your rule. - You have to create a function 'reduceMonitoring' to be able reduce all of them to 1 value. - `) + if (this.#handler.reduceMonitoring === undefined) throw new Error(Context.MISSING_REDUCER_MONITORING_MSG) return {rule: this.rule, level: this.level, value: this.#handler?.reduceMonitoring(this.#monitorings)} } diff --git a/packages/sui-lint/src/RepositoryLinter/Match.js b/packages/sui-lint/src/RepositoryLinter/Match.js index 73c8b6e4a..ffb826b85 100644 --- a/packages/sui-lint/src/RepositoryLinter/Match.js +++ b/packages/sui-lint/src/RepositoryLinter/Match.js @@ -12,6 +12,10 @@ class CustomFileReader { } class Match { + static empty() { + return new Match(undefined, undefined, undefined, false) + } + static create(path) { const ext = extname(path) if (!ext && CustomFileReader.create().isDirectory(path)) { diff --git a/packages/sui-lint/test/RepositoryLinter/ContextSpec.js b/packages/sui-lint/test/RepositoryLinter/ContextSpec.js new file mode 100644 index 000000000..fd640bcd4 --- /dev/null +++ b/packages/sui-lint/test/RepositoryLinter/ContextSpec.js @@ -0,0 +1,206 @@ +import {expect} from 'chai' +import {spy, stub} from 'sinon' + +import {Context} from '../../src/RepositoryLinter/Context.js' +import {Match} from '../../src/RepositoryLinter/Match.js' + +const LEVELS = { + OFF: 0, + WARNING: 1, + ERROR: 2 +} + +describe('Context', function () { + beforeEach(function () { + this.reportSpy = spy(Context.prototype, 'report') + this.monitoringSpy = spy(Context.prototype, 'monitoring') + this.runnerStub = {assertion: stub()} + this.handlerStub = { + meta: {messages: {badVersion: 'Message for your bad version'}}, + __assertionStub: stub(), + __missmatchStub: stub(), + create() { + return { + 'package.json': this.__assertionStub, + missmatch: this.__missmatchStub + } + } + } + + this.handlerInnerStub = { + meta: {messages: {badVersion: 'Message for your bad version', badKey: 'Message for bad {{key}}'}}, + create(context) { + return { + 'package.json': matches => { + context.report({messageId: 'badVersion'}) + context.monitoring(true) + }, + missmatch: key => { + context.report({messageId: 'badKey', data: {key}}) + context.monitoring(false) + } + } + } + } + + this.handlerReducerMonitoringStub = { + meta: {messages: {badVersion: 'Message for your bad version', badKey: 'Message for bad {{key}}'}}, + reduceMonitoring: stub(), + create(context) { + return { + 'package.json': matches => { + context.monitoring(true) + context.monitoring(false) + } + } + } + } + }) + + afterEach(function () { + this.handlerStub.__assertionStub.reset() + this.handlerStub.__missmatchStub.reset() + + this.monitoringSpy.restore() + this.reportSpy.restore() + + this.runnerStub.assertion.reset() + + this.handlerReducerMonitoringStub.reduceMonitoring?.reset() + }) + + it('Should call to the assertions in the handler when there is Match', function () { + // Given + const emptyMatch = Match.empty() + this.runnerStub.assertion.returns([emptyMatch]) + + // When + Context.create(LEVELS.WARNING, this.handlerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(this.handlerStub.__assertionStub.firstCall.firstArg).to.be.instanceof(Array) + expect(this.handlerStub.__assertionStub.firstCall.firstArg[0]).to.be.eql(emptyMatch) + }) + + it('Should call to the missmatch in the handler when there is not Match with the "failing" key', function () { + // Given + this.runnerStub.assertion.returns([]) + + // When + Context.create(LEVELS.WARNING, this.handlerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(this.handlerStub.__assertionStub.firstCall).to.be.eql(null) + expect(this.handlerStub.__missmatchStub.firstCall.firstArg).to.be.eql('package.json') + }) + + it('Should create new monitorings and messages from assertion function', function () { + // Given + this.runnerStub.assertion.returns([Match.empty()]) + + // When + Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(this.reportSpy.firstCall.firstArg).to.be.eql({messageId: 'badVersion'}) + expect(this.monitoringSpy.firstCall.firstArg).to.be.eql(true) + }) + + it('Should create new monitorings and messages from missmatch function', function () { + // Given + this.runnerStub.assertion.returns([]) + + // When + Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(this.reportSpy.firstCall.firstArg).to.be.eql({messageId: 'badKey', data: {key: 'package.json'}}) + expect(this.monitoringSpy.firstCall.firstArg).to.be.eql(false) + }) + + it('Should properly format the messages', function () { + // Given + this.runnerStub.assertion.returns([]) + + // When + const context = Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(context.messages).to.be.eql([ + { + rule: 'tests/node-version', + message: 'Message for bad package.json', + level: 1, + messageId: 'badKey', + data: {key: 'package.json'} + } + ]) + }) + + it('Should properly format the monitoring', function () { + // Given + this.runnerStub.assertion.returns([Match.empty()]) + + // When + const context = Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(context.signal).to.be.eql({ + rule: 'tests/node-version', + value: true, + level: 1 + }) + }) + + it('Should require a reduceMonitoring function when there are more than one monitor', function () { + // Given + this.runnerStub.assertion.returns([Match.empty()]) + this.handlerReducerMonitoringStub.reduceMonitoring.returns(false) + + // When + const context = Context.create( + LEVELS.WARNING, + this.handlerReducerMonitoringStub, + 'tests/node-version', + this.runnerStub + ).run() + + // Then + expect(context.signal).to.be.eql({ + rule: 'tests/node-version', + value: false, + level: 1 + }) + expect(this.handlerReducerMonitoringStub.reduceMonitoring.firstCall.firstArg).to.be.eql([ + { + assertion: 'package.json', + rule: 'tests/node-version', + value: true, + level: 1 + }, + { + assertion: 'package.json', + rule: 'tests/node-version', + value: false, + level: 1 + } + ]) + }) + + it('Should throw an exception if the reduceMonitoring function is undefined', function () { + // Given + this.runnerStub.assertion.returns([Match.empty()]) + delete this.handlerReducerMonitoringStub.reduceMonitoring + + // When + const context = Context.create( + LEVELS.WARNING, + this.handlerReducerMonitoringStub, + 'tests/node-version', + this.runnerStub + ).run() + + // Then + expect(() => context.signal).to.be.throw(Context.MISSING_REDUCER_MONITORING_MSG) + }) +}) diff --git a/packages/sui-lint/test/RepositoryLinter/ResultsSpec.js b/packages/sui-lint/test/RepositoryLinter/ResultsSpec.js index 311911f23..ad9cc62c5 100644 --- a/packages/sui-lint/test/RepositoryLinter/ResultsSpec.js +++ b/packages/sui-lint/test/RepositoryLinter/ResultsSpec.js @@ -39,6 +39,45 @@ describe('Results', function () { ] Results.create(executions).logTable() - // expect(this.logStub.calledWith(Results.HAPPY_MESSAGE)).to.be.eq(true) + expect(this.addRowStub.firstCall.firstArg).to.be.eql({rule: 'tester/node-version', message: 'Node version fail'}) + expect(this.addRowStub.firstCall.lastArg).to.be.eql({color: 'yellow'}) + + expect(this.addRowStub.secondCall.firstArg).to.be.eql({rule: 'tester/react-version', message: 'React version fail'}) + expect(this.addRowStub.secondCall.lastArg).to.be.eql({color: 'red'}) + }) + + it('Should print a table with all the monitorings', function () { + const executions = [ + {messages: [{rule: 'tester/node-version', message: 'Node version fail', level: 1}], signal: 12}, + {messages: [{rule: 'tester/react-version', message: 'React version fail', level: 2}], signal: 17}, + {messages: [], signal: true} + ] + Results.create(executions).logMonitorings() + + expect(this.addRowStub.firstCall.firstArg).to.be.eql(12) + expect(this.addRowStub.secondCall.firstArg).to.be.eql(17) + expect(this.addRowStub.thirdCall.firstArg).to.be.eql(true) + }) + + it('Should print a JSON output', function () { + const executions = [ + {messages: [{rule: 'tester/node-version', message: 'Node version fail', level: 1}], signal: 12}, + {messages: [{rule: 'tester/react-version', message: 'React version fail', level: 2}], signal: 17}, + {messages: [], signal: true} + ] + Results.create(executions).logJSON() + + expect( + this.logStub.calledWith( + JSON.stringify( + [ + {rule: 'tester/node-version', message: 'Node version fail', level: 1}, + {rule: 'tester/react-version', message: 'React version fail', level: 2} + ], + null, + 2 + ) + ) + ).to.be.eq(true) }) }) diff --git a/packages/sui-lint/test/RepositoryLinter/RunnerSpec.js b/packages/sui-lint/test/RepositoryLinter/RunnerSpec.js new file mode 100644 index 000000000..5d69d1e58 --- /dev/null +++ b/packages/sui-lint/test/RepositoryLinter/RunnerSpec.js @@ -0,0 +1,26 @@ +import {expect} from 'chai' +import {stub} from 'sinon' + +import {Match} from '../../src/RepositoryLinter/Match' +import {Runner} from '../../src/RepositoryLinter/Runner.js' + +describe('Runner', function () { + beforeEach(function () { + this.syncStub = stub() + this.matchCreateStub = stub(Match, 'create') + }) + + afterEach(function () { + this.syncStub.reset() + this.matchCreateStub.restore() + }) + + it('Should return a list of matches', function () { + this.syncStub.returns(['path/file.json']) + + Runner.create({sync: this.syncStub}).assertion('**/*.json') + + expect(this.matchCreateStub.firstCall.firstArg).to.be.eql('path/file.json') + expect(this.syncStub.firstCall.firstArg).to.be.eql('**/*.json') + }) +}) diff --git a/packages/sui-lint/test/RepositoryLinter/indexSpec.js b/packages/sui-lint/test/RepositoryLinter/indexSpec.js deleted file mode 100644 index e69de29bb..000000000 From 1fa5d6730d9714e52cfb57a97d883f45ab15083c Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 17:20:23 +0100 Subject: [PATCH 022/103] test(packages/eslint-plugin-sui): move to test/server folder --- .../test/server/rules/factory-pattern.js | 41 ++++++++ .../server/rules/serialize-deserialize.js | 96 +++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 packages/eslint-plugin-sui/test/server/rules/factory-pattern.js create mode 100644 packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js diff --git a/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js b/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js new file mode 100644 index 000000000..9f961d232 --- /dev/null +++ b/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js @@ -0,0 +1,41 @@ +import dedent from 'dedent' +import {RuleTester} from 'eslint' + +import rule from '../../../src/rules/factory-pattern' + +// ------------------------------------------------------------------------------ +// Tests +// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester +// ------------------------------------------------------------------------------ + +const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) +ruleTester.run('factory-pattern', rule, { + valid: [ + { + code: dedent` + class User { + static create() { return new User() } + } + ` + } + ], + + invalid: [ + { + code: dedent` + class Model { + constructor() { this.name = 'John Doe' } + }`, + errors: [ + { + message: 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 + `, + type: 'ClassDeclaration' + } + ] + } + ] +}) diff --git a/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js new file mode 100644 index 000000000..b9d952b68 --- /dev/null +++ b/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js @@ -0,0 +1,96 @@ +import dedent from 'dedent' +import {RuleTester} from 'eslint' + +import rule from '../../../src/rules/serialize-deserialize' + +// ------------------------------------------------------------------------------ +// Tests +// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester +// ------------------------------------------------------------------------------ + +const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) +ruleTester.run('serialize-deserialize', rule, { + valid: [ + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + id: this.id, + name: this.name + } + } + } + ` + } + ], + + invalid: [ + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return this.id + } + } + `, + errors: [ + { + message: dedent`toJSON should return an object` + } + ] + }, + { + code: dedent` + class User { + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + Noid: this.id, + Noname: this.name + } + } + } + `, + errors: [ + { + message: dedent` + If your class has a 'toJSON' method. You have to define a 'static create' method too. + The output of the 'toJSON' should be the same as the input of your 'static create' method + ` + } + ] + }, + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + Noid: this.id, + Noname: this.name + } + } + } + `, + errors: [{message: 'Missing toJSON properties (id, name)'}] + } + ] +}) From 503b1bd7d080944995efa192a241f063c1e53cfa Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 17:31:43 +0100 Subject: [PATCH 023/103] test(packages/lint-repository-sui): move tests to server --- .../test/{ => server}/rules/github-actionSpec.js | 2 +- .../test/{ => server}/rules/node-versionSpec.js | 2 +- .../test/{ => server}/rules/package-lockSpec.js | 2 +- .../test/{ => server}/rules/react-versionSpec.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename packages/lint-repository-sui/test/{ => server}/rules/github-actionSpec.js (96%) rename packages/lint-repository-sui/test/{ => server}/rules/node-versionSpec.js (95%) rename packages/lint-repository-sui/test/{ => server}/rules/package-lockSpec.js (92%) rename packages/lint-repository-sui/test/{ => server}/rules/react-versionSpec.js (95%) diff --git a/packages/lint-repository-sui/test/rules/github-actionSpec.js b/packages/lint-repository-sui/test/server/rules/github-actionSpec.js similarity index 96% rename from packages/lint-repository-sui/test/rules/github-actionSpec.js rename to packages/lint-repository-sui/test/server/rules/github-actionSpec.js index 17f4ecb5e..9f78fd091 100644 --- a/packages/lint-repository-sui/test/rules/github-actionSpec.js +++ b/packages/lint-repository-sui/test/server/rules/github-actionSpec.js @@ -1,7 +1,7 @@ import dedent from 'dedent' import handler from '../../src/rules/github-action.js' -import {RuleTester} from '../TestHelpers' +import {RuleTester} from '../../TestHelpers.js' RuleTester.create('github-action', handler).run({ valid: [ diff --git a/packages/lint-repository-sui/test/rules/node-versionSpec.js b/packages/lint-repository-sui/test/server/rules/node-versionSpec.js similarity index 95% rename from packages/lint-repository-sui/test/rules/node-versionSpec.js rename to packages/lint-repository-sui/test/server/rules/node-versionSpec.js index 380bfcbd2..31a46d51a 100644 --- a/packages/lint-repository-sui/test/rules/node-versionSpec.js +++ b/packages/lint-repository-sui/test/server/rules/node-versionSpec.js @@ -1,7 +1,7 @@ import dedent from 'dedent' import handler from '../../src/rules/node-version.js' -import {MatchStub, RuleTester} from '../TestHelpers' +import {MatchStub, RuleTester} from '../../TestHelpers.js' RuleTester.create('node-version', handler).run({ valid: [ diff --git a/packages/lint-repository-sui/test/rules/package-lockSpec.js b/packages/lint-repository-sui/test/server/rules/package-lockSpec.js similarity index 92% rename from packages/lint-repository-sui/test/rules/package-lockSpec.js rename to packages/lint-repository-sui/test/server/rules/package-lockSpec.js index dd3cc21a0..533ccafe7 100644 --- a/packages/lint-repository-sui/test/rules/package-lockSpec.js +++ b/packages/lint-repository-sui/test/server/rules/package-lockSpec.js @@ -1,7 +1,7 @@ import dedent from 'dedent' import handler from '../../src/rules/package-lock.js' -import {RuleTester} from '../TestHelpers' +import {RuleTester} from '../../TestHelpers.js' RuleTester.create('package-lock', handler).run({ valid: [ diff --git a/packages/lint-repository-sui/test/rules/react-versionSpec.js b/packages/lint-repository-sui/test/server/rules/react-versionSpec.js similarity index 95% rename from packages/lint-repository-sui/test/rules/react-versionSpec.js rename to packages/lint-repository-sui/test/server/rules/react-versionSpec.js index a7a50ca49..f890d9a39 100644 --- a/packages/lint-repository-sui/test/rules/react-versionSpec.js +++ b/packages/lint-repository-sui/test/server/rules/react-versionSpec.js @@ -1,7 +1,7 @@ import dedent from 'dedent' import handler from '../../src/rules/react-version.js' -import {MatchStub, RuleTester} from '../TestHelpers' +import {MatchStub, RuleTester} from '../../TestHelpers.js' RuleTester.create('react-version', handler).run({ valid: [ From 29e81668de6b8e6b80782201f5a46ebf25b6fdb6 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 17:32:34 +0100 Subject: [PATCH 024/103] test(packages/sui-lint): move to server folder --- .../sui-lint/test/{ => server}/RepositoryLinter/ConfigSpec.js | 0 .../sui-lint/test/{ => server}/RepositoryLinter/ContextSpec.js | 0 packages/sui-lint/test/{ => server}/RepositoryLinter/MatchSpec.js | 0 .../sui-lint/test/{ => server}/RepositoryLinter/ResultsSpec.js | 0 .../sui-lint/test/{ => server}/RepositoryLinter/RunnerSpec.js | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename packages/sui-lint/test/{ => server}/RepositoryLinter/ConfigSpec.js (100%) rename packages/sui-lint/test/{ => server}/RepositoryLinter/ContextSpec.js (100%) rename packages/sui-lint/test/{ => server}/RepositoryLinter/MatchSpec.js (100%) rename packages/sui-lint/test/{ => server}/RepositoryLinter/ResultsSpec.js (100%) rename packages/sui-lint/test/{ => server}/RepositoryLinter/RunnerSpec.js (100%) diff --git a/packages/sui-lint/test/RepositoryLinter/ConfigSpec.js b/packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js similarity index 100% rename from packages/sui-lint/test/RepositoryLinter/ConfigSpec.js rename to packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js diff --git a/packages/sui-lint/test/RepositoryLinter/ContextSpec.js b/packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js similarity index 100% rename from packages/sui-lint/test/RepositoryLinter/ContextSpec.js rename to packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js diff --git a/packages/sui-lint/test/RepositoryLinter/MatchSpec.js b/packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js similarity index 100% rename from packages/sui-lint/test/RepositoryLinter/MatchSpec.js rename to packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js diff --git a/packages/sui-lint/test/RepositoryLinter/ResultsSpec.js b/packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js similarity index 100% rename from packages/sui-lint/test/RepositoryLinter/ResultsSpec.js rename to packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js diff --git a/packages/sui-lint/test/RepositoryLinter/RunnerSpec.js b/packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js similarity index 100% rename from packages/sui-lint/test/RepositoryLinter/RunnerSpec.js rename to packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js From 424b443d9d2d9ce6f3a1fa55b089dc7d23a95734 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 17:33:43 +0100 Subject: [PATCH 025/103] test(packages/eslint-plugin-sui): remove tests folder --- .../tests/src/rules/factory-pattern.js | 41 -------- .../tests/src/rules/serialize-deserialize.js | 96 ------------------- 2 files changed, 137 deletions(-) delete mode 100644 packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js delete mode 100644 packages/eslint-plugin-sui/tests/src/rules/serialize-deserialize.js diff --git a/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js b/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js deleted file mode 100644 index 9f961d232..000000000 --- a/packages/eslint-plugin-sui/tests/src/rules/factory-pattern.js +++ /dev/null @@ -1,41 +0,0 @@ -import dedent from 'dedent' -import {RuleTester} from 'eslint' - -import rule from '../../../src/rules/factory-pattern' - -// ------------------------------------------------------------------------------ -// Tests -// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester -// ------------------------------------------------------------------------------ - -const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) -ruleTester.run('factory-pattern', rule, { - valid: [ - { - code: dedent` - class User { - static create() { return new User() } - } - ` - } - ], - - invalid: [ - { - code: dedent` - class Model { - constructor() { this.name = 'John Doe' } - }`, - errors: [ - { - message: 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 - `, - type: 'ClassDeclaration' - } - ] - } - ] -}) diff --git a/packages/eslint-plugin-sui/tests/src/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/tests/src/rules/serialize-deserialize.js deleted file mode 100644 index b9d952b68..000000000 --- a/packages/eslint-plugin-sui/tests/src/rules/serialize-deserialize.js +++ /dev/null @@ -1,96 +0,0 @@ -import dedent from 'dedent' -import {RuleTester} from 'eslint' - -import rule from '../../../src/rules/serialize-deserialize' - -// ------------------------------------------------------------------------------ -// Tests -// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester -// ------------------------------------------------------------------------------ - -const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) -ruleTester.run('serialize-deserialize', rule, { - valid: [ - { - code: dedent` - class User { - static create({id, name}) { return new User(id, name) } - constructor(id, name) { - this.id = id - this.name = name - } - toJSON() { - return { - id: this.id, - name: this.name - } - } - } - ` - } - ], - - invalid: [ - { - code: dedent` - class User { - static create({id, name}) { return new User(id, name) } - constructor(id, name) { - this.id = id - this.name = name - } - toJSON() { - return this.id - } - } - `, - errors: [ - { - message: dedent`toJSON should return an object` - } - ] - }, - { - code: dedent` - class User { - constructor(id, name) { - this.id = id - this.name = name - } - toJSON() { - return { - Noid: this.id, - Noname: this.name - } - } - } - `, - errors: [ - { - message: dedent` - If your class has a 'toJSON' method. You have to define a 'static create' method too. - The output of the 'toJSON' should be the same as the input of your 'static create' method - ` - } - ] - }, - { - code: dedent` - class User { - static create({id, name}) { return new User(id, name) } - constructor(id, name) { - this.id = id - this.name = name - } - toJSON() { - return { - Noid: this.id, - Noname: this.name - } - } - } - `, - errors: [{message: 'Missing toJSON properties (id, name)'}] - } - ] -}) From 69c7d64af536cddca4a783febd308a55fd94f4ba Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 17:53:24 +0100 Subject: [PATCH 026/103] test(packages/eslint-plugin-sui): fix import path --- packages/eslint-plugin-sui/test/server/rules/factory-pattern.js | 2 +- .../test/server/rules/serialize-deserialize.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js b/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js index 9f961d232..458134e3d 100644 --- a/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js +++ b/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js @@ -1,7 +1,7 @@ import dedent from 'dedent' import {RuleTester} from 'eslint' -import rule from '../../../src/rules/factory-pattern' +import rule from '../../../src/rules/factory-pattern.js' // ------------------------------------------------------------------------------ // Tests diff --git a/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js index b9d952b68..e42234289 100644 --- a/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js +++ b/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js @@ -1,7 +1,7 @@ import dedent from 'dedent' import {RuleTester} from 'eslint' -import rule from '../../../src/rules/serialize-deserialize' +import rule from '../../../src/rules/serialize-deserialize.js' // ------------------------------------------------------------------------------ // Tests From 6ea3cd7edd2f2dc713f90dd65c5eeacb867831c3 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 17:53:58 +0100 Subject: [PATCH 027/103] test(packages/lint-repository-sui): fix path import --- .../lint-repository-sui/test/server/rules/github-actionSpec.js | 2 +- .../lint-repository-sui/test/server/rules/node-versionSpec.js | 2 +- .../lint-repository-sui/test/server/rules/package-lockSpec.js | 2 +- .../lint-repository-sui/test/server/rules/react-versionSpec.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/lint-repository-sui/test/server/rules/github-actionSpec.js b/packages/lint-repository-sui/test/server/rules/github-actionSpec.js index 9f78fd091..67f21665a 100644 --- a/packages/lint-repository-sui/test/server/rules/github-actionSpec.js +++ b/packages/lint-repository-sui/test/server/rules/github-actionSpec.js @@ -1,6 +1,6 @@ import dedent from 'dedent' -import handler from '../../src/rules/github-action.js' +import handler from '../../../src/rules/github-action.js' import {RuleTester} from '../../TestHelpers.js' RuleTester.create('github-action', handler).run({ diff --git a/packages/lint-repository-sui/test/server/rules/node-versionSpec.js b/packages/lint-repository-sui/test/server/rules/node-versionSpec.js index 31a46d51a..242ee636d 100644 --- a/packages/lint-repository-sui/test/server/rules/node-versionSpec.js +++ b/packages/lint-repository-sui/test/server/rules/node-versionSpec.js @@ -1,6 +1,6 @@ import dedent from 'dedent' -import handler from '../../src/rules/node-version.js' +import handler from '../../../src/rules/node-version.js' import {MatchStub, RuleTester} from '../../TestHelpers.js' RuleTester.create('node-version', handler).run({ diff --git a/packages/lint-repository-sui/test/server/rules/package-lockSpec.js b/packages/lint-repository-sui/test/server/rules/package-lockSpec.js index 533ccafe7..6684cfc4e 100644 --- a/packages/lint-repository-sui/test/server/rules/package-lockSpec.js +++ b/packages/lint-repository-sui/test/server/rules/package-lockSpec.js @@ -1,6 +1,6 @@ import dedent from 'dedent' -import handler from '../../src/rules/package-lock.js' +import handler from '../../../src/rules/package-lock.js' import {RuleTester} from '../../TestHelpers.js' RuleTester.create('package-lock', handler).run({ diff --git a/packages/lint-repository-sui/test/server/rules/react-versionSpec.js b/packages/lint-repository-sui/test/server/rules/react-versionSpec.js index f890d9a39..abb63b6ca 100644 --- a/packages/lint-repository-sui/test/server/rules/react-versionSpec.js +++ b/packages/lint-repository-sui/test/server/rules/react-versionSpec.js @@ -1,6 +1,6 @@ import dedent from 'dedent' -import handler from '../../src/rules/react-version.js' +import handler from '../../../src/rules/react-version.js' import {MatchStub, RuleTester} from '../../TestHelpers.js' RuleTester.create('react-version', handler).run({ From b01ae7ea6f5f3648f94ec6a73f6de42b063a50ec Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 17:54:34 +0100 Subject: [PATCH 028/103] test(packages/sui-lint): fix import path --- packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js | 2 +- packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js | 4 ++-- packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js | 2 +- packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js | 2 +- packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js b/packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js index 2960d8d0a..0261735b2 100644 --- a/packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js +++ b/packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js @@ -1,7 +1,7 @@ import {expect} from 'chai' import {stub} from 'sinon' -import {Config} from '../../src/RepositoryLinter/Config.js' +import {Config} from '../../../src/RepositoryLinter/Config.js' describe('Config', function () { beforeEach(function () { diff --git a/packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js b/packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js index fd640bcd4..babc8691e 100644 --- a/packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js +++ b/packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js @@ -1,8 +1,8 @@ import {expect} from 'chai' import {spy, stub} from 'sinon' -import {Context} from '../../src/RepositoryLinter/Context.js' -import {Match} from '../../src/RepositoryLinter/Match.js' +import {Context} from '../../../src/RepositoryLinter/Context.js' +import {Match} from '../../../src/RepositoryLinter/Match.js' const LEVELS = { OFF: 0, diff --git a/packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js b/packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js index 5db0c60c7..e609d2e6e 100644 --- a/packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js +++ b/packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js @@ -1,7 +1,7 @@ import {expect} from 'chai' import {stub} from 'sinon' -import {CustomFileReader, Match} from '../../src/RepositoryLinter/Match.js' +import {CustomFileReader, Match} from '../../../src/RepositoryLinter/Match.js' describe('Match', function () { beforeEach(function () { diff --git a/packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js b/packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js index ad9cc62c5..3f40cecb6 100644 --- a/packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js +++ b/packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js @@ -2,7 +2,7 @@ import {expect} from 'chai' import {Table} from 'console-table-printer' import {stub} from 'sinon' -import {Results} from '../../src/RepositoryLinter/Results.js' +import {Results} from '../../../src/RepositoryLinter/Results.js' describe('Results', function () { beforeEach(function () { diff --git a/packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js b/packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js index 5d69d1e58..a1970002f 100644 --- a/packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js +++ b/packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js @@ -1,8 +1,8 @@ import {expect} from 'chai' import {stub} from 'sinon' -import {Match} from '../../src/RepositoryLinter/Match' -import {Runner} from '../../src/RepositoryLinter/Runner.js' +import {Match} from '../../../src/RepositoryLinter/Match.js' +import {Runner} from '../../../src/RepositoryLinter/Runner.js' describe('Runner', function () { beforeEach(function () { From df3de9f1daa4ec04f856c8d0812042385b626c81 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 18:04:55 +0100 Subject: [PATCH 029/103] test(packages/eslint-plugin-sui): move folder --- .../test/server/rules/factory-pattern.js | 41 -------- .../server/rules/serialize-deserialize.js | 96 ------------------- 2 files changed, 137 deletions(-) delete mode 100644 packages/eslint-plugin-sui/test/server/rules/factory-pattern.js delete mode 100644 packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js diff --git a/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js b/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js deleted file mode 100644 index 458134e3d..000000000 --- a/packages/eslint-plugin-sui/test/server/rules/factory-pattern.js +++ /dev/null @@ -1,41 +0,0 @@ -import dedent from 'dedent' -import {RuleTester} from 'eslint' - -import rule from '../../../src/rules/factory-pattern.js' - -// ------------------------------------------------------------------------------ -// Tests -// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester -// ------------------------------------------------------------------------------ - -const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) -ruleTester.run('factory-pattern', rule, { - valid: [ - { - code: dedent` - class User { - static create() { return new User() } - } - ` - } - ], - - invalid: [ - { - code: dedent` - class Model { - constructor() { this.name = 'John Doe' } - }`, - errors: [ - { - message: 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 - `, - type: 'ClassDeclaration' - } - ] - } - ] -}) diff --git a/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js deleted file mode 100644 index e42234289..000000000 --- a/packages/eslint-plugin-sui/test/server/rules/serialize-deserialize.js +++ /dev/null @@ -1,96 +0,0 @@ -import dedent from 'dedent' -import {RuleTester} from 'eslint' - -import rule from '../../../src/rules/serialize-deserialize.js' - -// ------------------------------------------------------------------------------ -// Tests -// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester -// ------------------------------------------------------------------------------ - -const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) -ruleTester.run('serialize-deserialize', rule, { - valid: [ - { - code: dedent` - class User { - static create({id, name}) { return new User(id, name) } - constructor(id, name) { - this.id = id - this.name = name - } - toJSON() { - return { - id: this.id, - name: this.name - } - } - } - ` - } - ], - - invalid: [ - { - code: dedent` - class User { - static create({id, name}) { return new User(id, name) } - constructor(id, name) { - this.id = id - this.name = name - } - toJSON() { - return this.id - } - } - `, - errors: [ - { - message: dedent`toJSON should return an object` - } - ] - }, - { - code: dedent` - class User { - constructor(id, name) { - this.id = id - this.name = name - } - toJSON() { - return { - Noid: this.id, - Noname: this.name - } - } - } - `, - errors: [ - { - message: dedent` - If your class has a 'toJSON' method. You have to define a 'static create' method too. - The output of the 'toJSON' should be the same as the input of your 'static create' method - ` - } - ] - }, - { - code: dedent` - class User { - static create({id, name}) { return new User(id, name) } - constructor(id, name) { - this.id = id - this.name = name - } - toJSON() { - return { - Noid: this.id, - Noname: this.name - } - } - } - `, - errors: [{message: 'Missing toJSON properties (id, name)'}] - } - ] -}) From a704fc2914965f43229c22f5c1b3cea422173c4a Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 18:05:28 +0100 Subject: [PATCH 030/103] test(packages/lint-repository-sui): move folder --- .../test/server/rules/github-actionSpec.js | 53 ------------------- .../test/server/rules/node-versionSpec.js | 44 --------------- .../test/server/rules/package-lockSpec.js | 25 --------- .../test/server/rules/react-versionSpec.js | 44 --------------- 4 files changed, 166 deletions(-) delete mode 100644 packages/lint-repository-sui/test/server/rules/github-actionSpec.js delete mode 100644 packages/lint-repository-sui/test/server/rules/node-versionSpec.js delete mode 100644 packages/lint-repository-sui/test/server/rules/package-lockSpec.js delete mode 100644 packages/lint-repository-sui/test/server/rules/react-versionSpec.js diff --git a/packages/lint-repository-sui/test/server/rules/github-actionSpec.js b/packages/lint-repository-sui/test/server/rules/github-actionSpec.js deleted file mode 100644 index 67f21665a..000000000 --- a/packages/lint-repository-sui/test/server/rules/github-actionSpec.js +++ /dev/null @@ -1,53 +0,0 @@ -import dedent from 'dedent' - -import handler from '../../../src/rules/github-action.js' -import {RuleTester} from '../../TestHelpers.js' - -RuleTester.create('github-action', handler).run({ - valid: [ - { - '.github/workflows': [], - name: 'The porject has define a worflows folder', - monitoring: true - }, - { - '.github/**/main.yml': [], - name: 'The porject has define a worflow for the main branch', - monitoring: true - }, - { - '.github/**/pullrequest.yml': [], - name: 'The porject has define a worflow for PRs', - monitoring: true - } - ], - invalid: [ - { - missmatch: '.github/workflows', - name: 'The porject has NOT define a worflows folder', - report: dedent` - Every project needs to have a .github/worflows folder to be able to run CI/CD in GHA. - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: false - }, - { - missmatch: '.github/**/main.yml', - name: 'The porject has NOT define a worflow for the main branch', - report: dedent` - Every project needs to have a workflow to run on master. - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: false - }, - { - missmatch: '.github/**/pullrequest.yml', - name: 'The porject has NOT define a worflow for PRs', - report: dedent` - Every project needs to have a workflow to run on every PR. - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: false - } - ] -}) diff --git a/packages/lint-repository-sui/test/server/rules/node-versionSpec.js b/packages/lint-repository-sui/test/server/rules/node-versionSpec.js deleted file mode 100644 index 242ee636d..000000000 --- a/packages/lint-repository-sui/test/server/rules/node-versionSpec.js +++ /dev/null @@ -1,44 +0,0 @@ -import dedent from 'dedent' - -import handler from '../../../src/rules/node-version.js' -import {MatchStub, RuleTester} from '../../TestHelpers.js' - -RuleTester.create('node-version', handler).run({ - valid: [ - { - '.nvmrc': [MatchStub.create({raw: '20'})], - name: 'nvmrc Exists and has setup the version 20', - monitoring: '20' - } - ], - invalid: [ - { - '.nvmrc': [MatchStub.create({raw: '20'}), MatchStub.create({raw: 17})], - name: 'Exits more than one nvmrc file', - report: dedent` - Your project has more than one .nvmrc file. That can be dangerous. - Please, use onle ONE in the root of your project. - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: 0 - }, - { - '.nvmrc': [MatchStub.create({raw: '16'})], - name: 'Exits more than one nvmrc file', - report: dedent` - Your current Node version is 16. - Please be sure that your repository use the latest Node Version 20. - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: 0 - }, - { - missmatch: '', - report: dedent` - Every project have to have a .npmrc file to define the node versión. - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: 0 - } - ] -}) diff --git a/packages/lint-repository-sui/test/server/rules/package-lockSpec.js b/packages/lint-repository-sui/test/server/rules/package-lockSpec.js deleted file mode 100644 index 6684cfc4e..000000000 --- a/packages/lint-repository-sui/test/server/rules/package-lockSpec.js +++ /dev/null @@ -1,25 +0,0 @@ -import dedent from 'dedent' - -import handler from '../../../src/rules/package-lock.js' -import {RuleTester} from '../../TestHelpers.js' - -RuleTester.create('package-lock', handler).run({ - valid: [ - { - 'package-lock.json': [], - name: 'Project has package-lock.json in the root folder', - monitoring: true - } - ], - invalid: [ - { - missmatch: '', - name: 'Project doesnt has package-lock in the root folder', - report: dedent` - Every project needs to have a package-lock.json file to be used in CI/CD. - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: false - } - ] -}) diff --git a/packages/lint-repository-sui/test/server/rules/react-versionSpec.js b/packages/lint-repository-sui/test/server/rules/react-versionSpec.js deleted file mode 100644 index abb63b6ca..000000000 --- a/packages/lint-repository-sui/test/server/rules/react-versionSpec.js +++ /dev/null @@ -1,44 +0,0 @@ -import dedent from 'dedent' - -import handler from '../../../src/rules/react-version.js' -import {MatchStub, RuleTester} from '../../TestHelpers.js' - -RuleTester.create('react-version', handler).run({ - valid: [ - { - 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/react': {version: '18.0.0'}}}})], - name: 'React 18 installed', - monitoring: '18' - } - ], - invalid: [ - { - 'package-lock.json': [MatchStub.create({parsed: {packages: {}}})], - name: 'React not installed', - report: dedent` - Your project doesnt have installed React. - Please install at least the version 18. - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: 0 - }, - { - 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/react': {version: '17.0.0'}}}})], - name: 'React wrong version', - report: dedent` - Please be sure that your repository use the latest React Version 18. - Your current version is 17. - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: '17' - }, - { - missmatch: '', - report: dedent` - To calculate the react version first we need to have a package-lock.json in the root - If you are not sure about how do it, please contact with Platform Web. - `, - monitoring: 0 - } - ] -}) From 4b029c895841c095a793fe61def8bbb747c526f6 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 18:06:10 +0100 Subject: [PATCH 031/103] test(packages/sui-lint): move folder --- .../server/RepositoryLinter/ConfigSpec.js | 38 ---- .../server/RepositoryLinter/ContextSpec.js | 206 ------------------ .../test/server/RepositoryLinter/MatchSpec.js | 99 --------- .../server/RepositoryLinter/ResultsSpec.js | 83 ------- .../server/RepositoryLinter/RunnerSpec.js | 26 --- 5 files changed, 452 deletions(-) delete mode 100644 packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js delete mode 100644 packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js delete mode 100644 packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js delete mode 100644 packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js delete mode 100644 packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js diff --git a/packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js b/packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js deleted file mode 100644 index 0261735b2..000000000 --- a/packages/sui-lint/test/server/RepositoryLinter/ConfigSpec.js +++ /dev/null @@ -1,38 +0,0 @@ -import {expect} from 'chai' -import {stub} from 'sinon' - -import {Config} from '../../../src/RepositoryLinter/Config.js' - -describe('Config', function () { - beforeEach(function () { - this.requireConfigStub = stub(Config.prototype, 'requireConfig') - this.requirePkgStub = stub(Config.prototype, 'requirePkg') - }) - - afterEach(function () { - this.requireConfigStub.reset() - this.requirePkgStub.reset() - }) - - it('Should return a rules object', async function () { - // Given - const handler = { - meta: {messages: {badVersion: 'Your react version is not 20'}}, - create: function () {} - } - this.requireConfigStub.returns({ - plugins: ['tester'], - rules: { - 'tester/react-version': 1 - } - }) - this.requirePkgStub.returns({rules: {'react-version': handler}}) - - // When - const rulesLoaded = await Config.create().load() - - // Then - expect(this.requirePkgStub.calledWith('tester')).to.be.eql(true) - expect(rulesLoaded).to.be.eqls({'tester/react-version': {handler, level: 1}}) - }) -}) diff --git a/packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js b/packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js deleted file mode 100644 index babc8691e..000000000 --- a/packages/sui-lint/test/server/RepositoryLinter/ContextSpec.js +++ /dev/null @@ -1,206 +0,0 @@ -import {expect} from 'chai' -import {spy, stub} from 'sinon' - -import {Context} from '../../../src/RepositoryLinter/Context.js' -import {Match} from '../../../src/RepositoryLinter/Match.js' - -const LEVELS = { - OFF: 0, - WARNING: 1, - ERROR: 2 -} - -describe('Context', function () { - beforeEach(function () { - this.reportSpy = spy(Context.prototype, 'report') - this.monitoringSpy = spy(Context.prototype, 'monitoring') - this.runnerStub = {assertion: stub()} - this.handlerStub = { - meta: {messages: {badVersion: 'Message for your bad version'}}, - __assertionStub: stub(), - __missmatchStub: stub(), - create() { - return { - 'package.json': this.__assertionStub, - missmatch: this.__missmatchStub - } - } - } - - this.handlerInnerStub = { - meta: {messages: {badVersion: 'Message for your bad version', badKey: 'Message for bad {{key}}'}}, - create(context) { - return { - 'package.json': matches => { - context.report({messageId: 'badVersion'}) - context.monitoring(true) - }, - missmatch: key => { - context.report({messageId: 'badKey', data: {key}}) - context.monitoring(false) - } - } - } - } - - this.handlerReducerMonitoringStub = { - meta: {messages: {badVersion: 'Message for your bad version', badKey: 'Message for bad {{key}}'}}, - reduceMonitoring: stub(), - create(context) { - return { - 'package.json': matches => { - context.monitoring(true) - context.monitoring(false) - } - } - } - } - }) - - afterEach(function () { - this.handlerStub.__assertionStub.reset() - this.handlerStub.__missmatchStub.reset() - - this.monitoringSpy.restore() - this.reportSpy.restore() - - this.runnerStub.assertion.reset() - - this.handlerReducerMonitoringStub.reduceMonitoring?.reset() - }) - - it('Should call to the assertions in the handler when there is Match', function () { - // Given - const emptyMatch = Match.empty() - this.runnerStub.assertion.returns([emptyMatch]) - - // When - Context.create(LEVELS.WARNING, this.handlerStub, 'tests/node-version', this.runnerStub).run() - - // Then - expect(this.handlerStub.__assertionStub.firstCall.firstArg).to.be.instanceof(Array) - expect(this.handlerStub.__assertionStub.firstCall.firstArg[0]).to.be.eql(emptyMatch) - }) - - it('Should call to the missmatch in the handler when there is not Match with the "failing" key', function () { - // Given - this.runnerStub.assertion.returns([]) - - // When - Context.create(LEVELS.WARNING, this.handlerStub, 'tests/node-version', this.runnerStub).run() - - // Then - expect(this.handlerStub.__assertionStub.firstCall).to.be.eql(null) - expect(this.handlerStub.__missmatchStub.firstCall.firstArg).to.be.eql('package.json') - }) - - it('Should create new monitorings and messages from assertion function', function () { - // Given - this.runnerStub.assertion.returns([Match.empty()]) - - // When - Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() - - // Then - expect(this.reportSpy.firstCall.firstArg).to.be.eql({messageId: 'badVersion'}) - expect(this.monitoringSpy.firstCall.firstArg).to.be.eql(true) - }) - - it('Should create new monitorings and messages from missmatch function', function () { - // Given - this.runnerStub.assertion.returns([]) - - // When - Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() - - // Then - expect(this.reportSpy.firstCall.firstArg).to.be.eql({messageId: 'badKey', data: {key: 'package.json'}}) - expect(this.monitoringSpy.firstCall.firstArg).to.be.eql(false) - }) - - it('Should properly format the messages', function () { - // Given - this.runnerStub.assertion.returns([]) - - // When - const context = Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() - - // Then - expect(context.messages).to.be.eql([ - { - rule: 'tests/node-version', - message: 'Message for bad package.json', - level: 1, - messageId: 'badKey', - data: {key: 'package.json'} - } - ]) - }) - - it('Should properly format the monitoring', function () { - // Given - this.runnerStub.assertion.returns([Match.empty()]) - - // When - const context = Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() - - // Then - expect(context.signal).to.be.eql({ - rule: 'tests/node-version', - value: true, - level: 1 - }) - }) - - it('Should require a reduceMonitoring function when there are more than one monitor', function () { - // Given - this.runnerStub.assertion.returns([Match.empty()]) - this.handlerReducerMonitoringStub.reduceMonitoring.returns(false) - - // When - const context = Context.create( - LEVELS.WARNING, - this.handlerReducerMonitoringStub, - 'tests/node-version', - this.runnerStub - ).run() - - // Then - expect(context.signal).to.be.eql({ - rule: 'tests/node-version', - value: false, - level: 1 - }) - expect(this.handlerReducerMonitoringStub.reduceMonitoring.firstCall.firstArg).to.be.eql([ - { - assertion: 'package.json', - rule: 'tests/node-version', - value: true, - level: 1 - }, - { - assertion: 'package.json', - rule: 'tests/node-version', - value: false, - level: 1 - } - ]) - }) - - it('Should throw an exception if the reduceMonitoring function is undefined', function () { - // Given - this.runnerStub.assertion.returns([Match.empty()]) - delete this.handlerReducerMonitoringStub.reduceMonitoring - - // When - const context = Context.create( - LEVELS.WARNING, - this.handlerReducerMonitoringStub, - 'tests/node-version', - this.runnerStub - ).run() - - // Then - expect(() => context.signal).to.be.throw(Context.MISSING_REDUCER_MONITORING_MSG) - }) -}) diff --git a/packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js b/packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js deleted file mode 100644 index e609d2e6e..000000000 --- a/packages/sui-lint/test/server/RepositoryLinter/MatchSpec.js +++ /dev/null @@ -1,99 +0,0 @@ -import {expect} from 'chai' -import {stub} from 'sinon' - -import {CustomFileReader, Match} from '../../../src/RepositoryLinter/Match.js' - -describe('Match', function () { - beforeEach(function () { - this.isDirStub = stub(CustomFileReader.prototype, 'isDirectory') - - this.parseYMLStub = stub(CustomFileReader.prototype, 'parseYML') - this.parseJSONStub = stub(CustomFileReader.prototype, 'parseJSON') - this.rawStub = stub(CustomFileReader.prototype, 'raw') - }) - - afterEach(function () { - this.isDirStub.restore() - - this.parseYMLStub.restore() - this.parseJSONStub.restore() - this.rawStub.restore() - }) - - it('Should detect directories', function () { - // Given - this.isDirStub.returns(true) - - // When - const match = Match.create('/dir/path') - - // Then - expect(match.isDir).to.be.eqls(true) - }) - - it('Should detect files w/out extensions', function () { - // Given - this.isDirStub.returns(false) - this.rawStub.returns('20') - - // When - const match = Match.create('/dir/path') - - // Then - expect(match.isDir).to.be.eqls(false) - expect(this.rawStub.calledWith('/dir/path')).to.be.eql(true) - }) - - it('Should parse JSON files', function () { - // Given - this.isDirStub.returns(false) - this.parseJSONStub.returns({a: 1}) - - // When - const match = Match.create('/dir/file.json') - - // Then - expect(match.parsed).to.be.eqls({a: 1}) - expect(this.parseJSONStub.calledWith('/dir/file.json')).to.be.eql(true) - }) - - it('Should parse yml files', function () { - // Given - this.isDirStub.returns(false) - this.parseYMLStub.returns({a: 1}) - - // When - const match = Match.create('/dir/file.yml') - - // Then - expect(match.parsed).to.be.eqls({a: 1}) - expect(this.parseYMLStub.calledWith('/dir/file.yml')).to.be.eql(true) - }) - - it('Should parse yaml files', function () { - // Given - this.isDirStub.returns(false) - this.parseYMLStub.returns({a: 1}) - - // When - const match = Match.create('/dir/file.yaml') - - // Then - expect(match.parsed).to.be.eqls({a: 1}) - expect(this.parseYMLStub.calledWith('/dir/file.yaml')).to.be.eql(true) - }) - - it('Should read unkown files', function () { - // Given - this.isDirStub.returns(false) - this.rawStub.returns('Hello') - - // When - const match = Match.create('/dir/file.txt') - - // Then - expect(match.raw).to.be.eqls('Hello') - expect(match.parsed).to.be.eqls(undefined) - expect(this.rawStub.calledWith('/dir/file.txt')).to.be.eql(true) - }) -}) diff --git a/packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js b/packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js deleted file mode 100644 index 3f40cecb6..000000000 --- a/packages/sui-lint/test/server/RepositoryLinter/ResultsSpec.js +++ /dev/null @@ -1,83 +0,0 @@ -import {expect} from 'chai' -import {Table} from 'console-table-printer' -import {stub} from 'sinon' - -import {Results} from '../../../src/RepositoryLinter/Results.js' - -describe('Results', function () { - beforeEach(function () { - this.addRowStub = stub(Table.prototype, 'addRow') - this.printTableStub = stub(Table.prototype, 'printTable') - - this.logStub = stub(Results.prototype, 'log') - }) - - afterEach(function () { - this.addRowStub.restore() - this.printTableStub.restore() - - this.logStub.restore() - }) - - it('Should print Happy Message it there is not messages', function () { - const executions = [ - { - messages: [], - signal: false - } - ] - Results.create(executions).logTable() - - expect(this.logStub.calledWith(Results.HAPPY_MESSAGE)).to.be.eq(true) - }) - - it('Should print a table with all the messages', function () { - const executions = [ - {messages: [{rule: 'tester/node-version', message: 'Node version fail', level: 1}], signal: 12}, - {messages: [{rule: 'tester/react-version', message: 'React version fail', level: 2}], signal: 17}, - {messages: [], signal: true} - ] - Results.create(executions).logTable() - - expect(this.addRowStub.firstCall.firstArg).to.be.eql({rule: 'tester/node-version', message: 'Node version fail'}) - expect(this.addRowStub.firstCall.lastArg).to.be.eql({color: 'yellow'}) - - expect(this.addRowStub.secondCall.firstArg).to.be.eql({rule: 'tester/react-version', message: 'React version fail'}) - expect(this.addRowStub.secondCall.lastArg).to.be.eql({color: 'red'}) - }) - - it('Should print a table with all the monitorings', function () { - const executions = [ - {messages: [{rule: 'tester/node-version', message: 'Node version fail', level: 1}], signal: 12}, - {messages: [{rule: 'tester/react-version', message: 'React version fail', level: 2}], signal: 17}, - {messages: [], signal: true} - ] - Results.create(executions).logMonitorings() - - expect(this.addRowStub.firstCall.firstArg).to.be.eql(12) - expect(this.addRowStub.secondCall.firstArg).to.be.eql(17) - expect(this.addRowStub.thirdCall.firstArg).to.be.eql(true) - }) - - it('Should print a JSON output', function () { - const executions = [ - {messages: [{rule: 'tester/node-version', message: 'Node version fail', level: 1}], signal: 12}, - {messages: [{rule: 'tester/react-version', message: 'React version fail', level: 2}], signal: 17}, - {messages: [], signal: true} - ] - Results.create(executions).logJSON() - - expect( - this.logStub.calledWith( - JSON.stringify( - [ - {rule: 'tester/node-version', message: 'Node version fail', level: 1}, - {rule: 'tester/react-version', message: 'React version fail', level: 2} - ], - null, - 2 - ) - ) - ).to.be.eq(true) - }) -}) diff --git a/packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js b/packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js deleted file mode 100644 index a1970002f..000000000 --- a/packages/sui-lint/test/server/RepositoryLinter/RunnerSpec.js +++ /dev/null @@ -1,26 +0,0 @@ -import {expect} from 'chai' -import {stub} from 'sinon' - -import {Match} from '../../../src/RepositoryLinter/Match.js' -import {Runner} from '../../../src/RepositoryLinter/Runner.js' - -describe('Runner', function () { - beforeEach(function () { - this.syncStub = stub() - this.matchCreateStub = stub(Match, 'create') - }) - - afterEach(function () { - this.syncStub.reset() - this.matchCreateStub.restore() - }) - - it('Should return a list of matches', function () { - this.syncStub.returns(['path/file.json']) - - Runner.create({sync: this.syncStub}).assertion('**/*.json') - - expect(this.matchCreateStub.firstCall.firstArg).to.be.eql('path/file.json') - expect(this.syncStub.firstCall.firstArg).to.be.eql('**/*.json') - }) -}) From 176eeafd0f966f834cd9c29fedb2494e708184f0 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Mon, 4 Mar 2024 18:07:02 +0100 Subject: [PATCH 032/103] test(Root): move folder --- .../test/server/factory-pattern.js | 41 ++++ .../test/server/serialize-deserialize.js | 96 ++++++++ .../test/server/github-actionSpec.js | 53 +++++ .../test/server/node-versionSpec.js | 44 ++++ .../test/server/package-lockSpec.js | 25 +++ .../test/server/react-versionSpec.js | 44 ++++ packages/sui-lint/test/server/ConfigSpec.js | 38 ++++ packages/sui-lint/test/server/ContextSpec.js | 206 ++++++++++++++++++ packages/sui-lint/test/server/MatchSpec.js | 99 +++++++++ packages/sui-lint/test/server/ResultsSpec.js | 83 +++++++ packages/sui-lint/test/server/RunnerSpec.js | 26 +++ 11 files changed, 755 insertions(+) create mode 100644 packages/eslint-plugin-sui/test/server/factory-pattern.js create mode 100644 packages/eslint-plugin-sui/test/server/serialize-deserialize.js create mode 100644 packages/lint-repository-sui/test/server/github-actionSpec.js create mode 100644 packages/lint-repository-sui/test/server/node-versionSpec.js create mode 100644 packages/lint-repository-sui/test/server/package-lockSpec.js create mode 100644 packages/lint-repository-sui/test/server/react-versionSpec.js create mode 100644 packages/sui-lint/test/server/ConfigSpec.js create mode 100644 packages/sui-lint/test/server/ContextSpec.js create mode 100644 packages/sui-lint/test/server/MatchSpec.js create mode 100644 packages/sui-lint/test/server/ResultsSpec.js create mode 100644 packages/sui-lint/test/server/RunnerSpec.js diff --git a/packages/eslint-plugin-sui/test/server/factory-pattern.js b/packages/eslint-plugin-sui/test/server/factory-pattern.js new file mode 100644 index 000000000..2e63d949a --- /dev/null +++ b/packages/eslint-plugin-sui/test/server/factory-pattern.js @@ -0,0 +1,41 @@ +import dedent from 'dedent' +import {RuleTester} from 'eslint' + +import rule from '../../src/rules/factory-pattern.js' + +// ------------------------------------------------------------------------------ +// Tests +// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester +// ------------------------------------------------------------------------------ + +const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) +ruleTester.run('factory-pattern', rule, { + valid: [ + { + code: dedent` + class User { + static create() { return new User() } + } + ` + } + ], + + invalid: [ + { + code: dedent` + class Model { + constructor() { this.name = 'John Doe' } + }`, + errors: [ + { + message: 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 + `, + type: 'ClassDeclaration' + } + ] + } + ] +}) diff --git a/packages/eslint-plugin-sui/test/server/serialize-deserialize.js b/packages/eslint-plugin-sui/test/server/serialize-deserialize.js new file mode 100644 index 000000000..e5fd6d925 --- /dev/null +++ b/packages/eslint-plugin-sui/test/server/serialize-deserialize.js @@ -0,0 +1,96 @@ +import dedent from 'dedent' +import {RuleTester} from 'eslint' + +import rule from '../../src/rules/serialize-deserialize.js' + +// ------------------------------------------------------------------------------ +// Tests +// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester +// ------------------------------------------------------------------------------ + +const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) +ruleTester.run('serialize-deserialize', rule, { + valid: [ + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + id: this.id, + name: this.name + } + } + } + ` + } + ], + + invalid: [ + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return this.id + } + } + `, + errors: [ + { + message: dedent`toJSON should return an object` + } + ] + }, + { + code: dedent` + class User { + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + Noid: this.id, + Noname: this.name + } + } + } + `, + errors: [ + { + message: dedent` + If your class has a 'toJSON' method. You have to define a 'static create' method too. + The output of the 'toJSON' should be the same as the input of your 'static create' method + ` + } + ] + }, + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + Noid: this.id, + Noname: this.name + } + } + } + `, + errors: [{message: 'Missing toJSON properties (id, name)'}] + } + ] +}) diff --git a/packages/lint-repository-sui/test/server/github-actionSpec.js b/packages/lint-repository-sui/test/server/github-actionSpec.js new file mode 100644 index 000000000..346cf8e42 --- /dev/null +++ b/packages/lint-repository-sui/test/server/github-actionSpec.js @@ -0,0 +1,53 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/github-action.js' +import {RuleTester} from '../TestHelpers.js' + +RuleTester.create('github-action', handler).run({ + valid: [ + { + '.github/workflows': [], + name: 'The porject has define a worflows folder', + monitoring: true + }, + { + '.github/**/main.yml': [], + name: 'The porject has define a worflow for the main branch', + monitoring: true + }, + { + '.github/**/pullrequest.yml': [], + name: 'The porject has define a worflow for PRs', + monitoring: true + } + ], + invalid: [ + { + missmatch: '.github/workflows', + name: 'The porject has NOT define a worflows folder', + report: dedent` + Every project needs to have a .github/worflows folder to be able to run CI/CD in GHA. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + }, + { + missmatch: '.github/**/main.yml', + name: 'The porject has NOT define a worflow for the main branch', + report: dedent` + Every project needs to have a workflow to run on master. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + }, + { + missmatch: '.github/**/pullrequest.yml', + name: 'The porject has NOT define a worflow for PRs', + report: dedent` + Every project needs to have a workflow to run on every PR. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + } + ] +}) diff --git a/packages/lint-repository-sui/test/server/node-versionSpec.js b/packages/lint-repository-sui/test/server/node-versionSpec.js new file mode 100644 index 000000000..c113232b9 --- /dev/null +++ b/packages/lint-repository-sui/test/server/node-versionSpec.js @@ -0,0 +1,44 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/node-version.js' +import {MatchStub, RuleTester} from '../TestHelpers.js' + +RuleTester.create('node-version', handler).run({ + valid: [ + { + '.nvmrc': [MatchStub.create({raw: '20'})], + name: 'nvmrc Exists and has setup the version 20', + monitoring: '20' + } + ], + invalid: [ + { + '.nvmrc': [MatchStub.create({raw: '20'}), MatchStub.create({raw: 17})], + name: 'Exits more than one nvmrc file', + report: dedent` + Your project has more than one .nvmrc file. That can be dangerous. + Please, use onle ONE in the root of your project. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + }, + { + '.nvmrc': [MatchStub.create({raw: '16'})], + name: 'Exits more than one nvmrc file', + report: dedent` + Your current Node version is 16. + Please be sure that your repository use the latest Node Version 20. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + }, + { + missmatch: '', + report: dedent` + Every project have to have a .npmrc file to define the node versión. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + } + ] +}) diff --git a/packages/lint-repository-sui/test/server/package-lockSpec.js b/packages/lint-repository-sui/test/server/package-lockSpec.js new file mode 100644 index 000000000..92773f311 --- /dev/null +++ b/packages/lint-repository-sui/test/server/package-lockSpec.js @@ -0,0 +1,25 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/package-lock.js' +import {RuleTester} from '../TestHelpers.js' + +RuleTester.create('package-lock', handler).run({ + valid: [ + { + 'package-lock.json': [], + name: 'Project has package-lock.json in the root folder', + monitoring: true + } + ], + invalid: [ + { + missmatch: '', + name: 'Project doesnt has package-lock in the root folder', + report: dedent` + Every project needs to have a package-lock.json file to be used in CI/CD. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + } + ] +}) diff --git a/packages/lint-repository-sui/test/server/react-versionSpec.js b/packages/lint-repository-sui/test/server/react-versionSpec.js new file mode 100644 index 000000000..6f3ceded4 --- /dev/null +++ b/packages/lint-repository-sui/test/server/react-versionSpec.js @@ -0,0 +1,44 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/react-version.js' +import {MatchStub, RuleTester} from '../TestHelpers.js' + +RuleTester.create('react-version', handler).run({ + valid: [ + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/react': {version: '18.0.0'}}}})], + name: 'React 18 installed', + monitoring: '18' + } + ], + invalid: [ + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {}}})], + name: 'React not installed', + report: dedent` + Your project doesnt have installed React. + Please install at least the version 18. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + }, + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/react': {version: '17.0.0'}}}})], + name: 'React wrong version', + report: dedent` + Please be sure that your repository use the latest React Version 18. + Your current version is 17. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: '17' + }, + { + missmatch: '', + report: dedent` + To calculate the react version first we need to have a package-lock.json in the root + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + } + ] +}) diff --git a/packages/sui-lint/test/server/ConfigSpec.js b/packages/sui-lint/test/server/ConfigSpec.js new file mode 100644 index 000000000..0261735b2 --- /dev/null +++ b/packages/sui-lint/test/server/ConfigSpec.js @@ -0,0 +1,38 @@ +import {expect} from 'chai' +import {stub} from 'sinon' + +import {Config} from '../../../src/RepositoryLinter/Config.js' + +describe('Config', function () { + beforeEach(function () { + this.requireConfigStub = stub(Config.prototype, 'requireConfig') + this.requirePkgStub = stub(Config.prototype, 'requirePkg') + }) + + afterEach(function () { + this.requireConfigStub.reset() + this.requirePkgStub.reset() + }) + + it('Should return a rules object', async function () { + // Given + const handler = { + meta: {messages: {badVersion: 'Your react version is not 20'}}, + create: function () {} + } + this.requireConfigStub.returns({ + plugins: ['tester'], + rules: { + 'tester/react-version': 1 + } + }) + this.requirePkgStub.returns({rules: {'react-version': handler}}) + + // When + const rulesLoaded = await Config.create().load() + + // Then + expect(this.requirePkgStub.calledWith('tester')).to.be.eql(true) + expect(rulesLoaded).to.be.eqls({'tester/react-version': {handler, level: 1}}) + }) +}) diff --git a/packages/sui-lint/test/server/ContextSpec.js b/packages/sui-lint/test/server/ContextSpec.js new file mode 100644 index 000000000..fd640bcd4 --- /dev/null +++ b/packages/sui-lint/test/server/ContextSpec.js @@ -0,0 +1,206 @@ +import {expect} from 'chai' +import {spy, stub} from 'sinon' + +import {Context} from '../../src/RepositoryLinter/Context.js' +import {Match} from '../../src/RepositoryLinter/Match.js' + +const LEVELS = { + OFF: 0, + WARNING: 1, + ERROR: 2 +} + +describe('Context', function () { + beforeEach(function () { + this.reportSpy = spy(Context.prototype, 'report') + this.monitoringSpy = spy(Context.prototype, 'monitoring') + this.runnerStub = {assertion: stub()} + this.handlerStub = { + meta: {messages: {badVersion: 'Message for your bad version'}}, + __assertionStub: stub(), + __missmatchStub: stub(), + create() { + return { + 'package.json': this.__assertionStub, + missmatch: this.__missmatchStub + } + } + } + + this.handlerInnerStub = { + meta: {messages: {badVersion: 'Message for your bad version', badKey: 'Message for bad {{key}}'}}, + create(context) { + return { + 'package.json': matches => { + context.report({messageId: 'badVersion'}) + context.monitoring(true) + }, + missmatch: key => { + context.report({messageId: 'badKey', data: {key}}) + context.monitoring(false) + } + } + } + } + + this.handlerReducerMonitoringStub = { + meta: {messages: {badVersion: 'Message for your bad version', badKey: 'Message for bad {{key}}'}}, + reduceMonitoring: stub(), + create(context) { + return { + 'package.json': matches => { + context.monitoring(true) + context.monitoring(false) + } + } + } + } + }) + + afterEach(function () { + this.handlerStub.__assertionStub.reset() + this.handlerStub.__missmatchStub.reset() + + this.monitoringSpy.restore() + this.reportSpy.restore() + + this.runnerStub.assertion.reset() + + this.handlerReducerMonitoringStub.reduceMonitoring?.reset() + }) + + it('Should call to the assertions in the handler when there is Match', function () { + // Given + const emptyMatch = Match.empty() + this.runnerStub.assertion.returns([emptyMatch]) + + // When + Context.create(LEVELS.WARNING, this.handlerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(this.handlerStub.__assertionStub.firstCall.firstArg).to.be.instanceof(Array) + expect(this.handlerStub.__assertionStub.firstCall.firstArg[0]).to.be.eql(emptyMatch) + }) + + it('Should call to the missmatch in the handler when there is not Match with the "failing" key', function () { + // Given + this.runnerStub.assertion.returns([]) + + // When + Context.create(LEVELS.WARNING, this.handlerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(this.handlerStub.__assertionStub.firstCall).to.be.eql(null) + expect(this.handlerStub.__missmatchStub.firstCall.firstArg).to.be.eql('package.json') + }) + + it('Should create new monitorings and messages from assertion function', function () { + // Given + this.runnerStub.assertion.returns([Match.empty()]) + + // When + Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(this.reportSpy.firstCall.firstArg).to.be.eql({messageId: 'badVersion'}) + expect(this.monitoringSpy.firstCall.firstArg).to.be.eql(true) + }) + + it('Should create new monitorings and messages from missmatch function', function () { + // Given + this.runnerStub.assertion.returns([]) + + // When + Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(this.reportSpy.firstCall.firstArg).to.be.eql({messageId: 'badKey', data: {key: 'package.json'}}) + expect(this.monitoringSpy.firstCall.firstArg).to.be.eql(false) + }) + + it('Should properly format the messages', function () { + // Given + this.runnerStub.assertion.returns([]) + + // When + const context = Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(context.messages).to.be.eql([ + { + rule: 'tests/node-version', + message: 'Message for bad package.json', + level: 1, + messageId: 'badKey', + data: {key: 'package.json'} + } + ]) + }) + + it('Should properly format the monitoring', function () { + // Given + this.runnerStub.assertion.returns([Match.empty()]) + + // When + const context = Context.create(LEVELS.WARNING, this.handlerInnerStub, 'tests/node-version', this.runnerStub).run() + + // Then + expect(context.signal).to.be.eql({ + rule: 'tests/node-version', + value: true, + level: 1 + }) + }) + + it('Should require a reduceMonitoring function when there are more than one monitor', function () { + // Given + this.runnerStub.assertion.returns([Match.empty()]) + this.handlerReducerMonitoringStub.reduceMonitoring.returns(false) + + // When + const context = Context.create( + LEVELS.WARNING, + this.handlerReducerMonitoringStub, + 'tests/node-version', + this.runnerStub + ).run() + + // Then + expect(context.signal).to.be.eql({ + rule: 'tests/node-version', + value: false, + level: 1 + }) + expect(this.handlerReducerMonitoringStub.reduceMonitoring.firstCall.firstArg).to.be.eql([ + { + assertion: 'package.json', + rule: 'tests/node-version', + value: true, + level: 1 + }, + { + assertion: 'package.json', + rule: 'tests/node-version', + value: false, + level: 1 + } + ]) + }) + + it('Should throw an exception if the reduceMonitoring function is undefined', function () { + // Given + this.runnerStub.assertion.returns([Match.empty()]) + delete this.handlerReducerMonitoringStub.reduceMonitoring + + // When + const context = Context.create( + LEVELS.WARNING, + this.handlerReducerMonitoringStub, + 'tests/node-version', + this.runnerStub + ).run() + + // Then + expect(() => context.signal).to.be.throw(Context.MISSING_REDUCER_MONITORING_MSG) + }) +}) diff --git a/packages/sui-lint/test/server/MatchSpec.js b/packages/sui-lint/test/server/MatchSpec.js new file mode 100644 index 000000000..5db0c60c7 --- /dev/null +++ b/packages/sui-lint/test/server/MatchSpec.js @@ -0,0 +1,99 @@ +import {expect} from 'chai' +import {stub} from 'sinon' + +import {CustomFileReader, Match} from '../../src/RepositoryLinter/Match.js' + +describe('Match', function () { + beforeEach(function () { + this.isDirStub = stub(CustomFileReader.prototype, 'isDirectory') + + this.parseYMLStub = stub(CustomFileReader.prototype, 'parseYML') + this.parseJSONStub = stub(CustomFileReader.prototype, 'parseJSON') + this.rawStub = stub(CustomFileReader.prototype, 'raw') + }) + + afterEach(function () { + this.isDirStub.restore() + + this.parseYMLStub.restore() + this.parseJSONStub.restore() + this.rawStub.restore() + }) + + it('Should detect directories', function () { + // Given + this.isDirStub.returns(true) + + // When + const match = Match.create('/dir/path') + + // Then + expect(match.isDir).to.be.eqls(true) + }) + + it('Should detect files w/out extensions', function () { + // Given + this.isDirStub.returns(false) + this.rawStub.returns('20') + + // When + const match = Match.create('/dir/path') + + // Then + expect(match.isDir).to.be.eqls(false) + expect(this.rawStub.calledWith('/dir/path')).to.be.eql(true) + }) + + it('Should parse JSON files', function () { + // Given + this.isDirStub.returns(false) + this.parseJSONStub.returns({a: 1}) + + // When + const match = Match.create('/dir/file.json') + + // Then + expect(match.parsed).to.be.eqls({a: 1}) + expect(this.parseJSONStub.calledWith('/dir/file.json')).to.be.eql(true) + }) + + it('Should parse yml files', function () { + // Given + this.isDirStub.returns(false) + this.parseYMLStub.returns({a: 1}) + + // When + const match = Match.create('/dir/file.yml') + + // Then + expect(match.parsed).to.be.eqls({a: 1}) + expect(this.parseYMLStub.calledWith('/dir/file.yml')).to.be.eql(true) + }) + + it('Should parse yaml files', function () { + // Given + this.isDirStub.returns(false) + this.parseYMLStub.returns({a: 1}) + + // When + const match = Match.create('/dir/file.yaml') + + // Then + expect(match.parsed).to.be.eqls({a: 1}) + expect(this.parseYMLStub.calledWith('/dir/file.yaml')).to.be.eql(true) + }) + + it('Should read unkown files', function () { + // Given + this.isDirStub.returns(false) + this.rawStub.returns('Hello') + + // When + const match = Match.create('/dir/file.txt') + + // Then + expect(match.raw).to.be.eqls('Hello') + expect(match.parsed).to.be.eqls(undefined) + expect(this.rawStub.calledWith('/dir/file.txt')).to.be.eql(true) + }) +}) diff --git a/packages/sui-lint/test/server/ResultsSpec.js b/packages/sui-lint/test/server/ResultsSpec.js new file mode 100644 index 000000000..ad9cc62c5 --- /dev/null +++ b/packages/sui-lint/test/server/ResultsSpec.js @@ -0,0 +1,83 @@ +import {expect} from 'chai' +import {Table} from 'console-table-printer' +import {stub} from 'sinon' + +import {Results} from '../../src/RepositoryLinter/Results.js' + +describe('Results', function () { + beforeEach(function () { + this.addRowStub = stub(Table.prototype, 'addRow') + this.printTableStub = stub(Table.prototype, 'printTable') + + this.logStub = stub(Results.prototype, 'log') + }) + + afterEach(function () { + this.addRowStub.restore() + this.printTableStub.restore() + + this.logStub.restore() + }) + + it('Should print Happy Message it there is not messages', function () { + const executions = [ + { + messages: [], + signal: false + } + ] + Results.create(executions).logTable() + + expect(this.logStub.calledWith(Results.HAPPY_MESSAGE)).to.be.eq(true) + }) + + it('Should print a table with all the messages', function () { + const executions = [ + {messages: [{rule: 'tester/node-version', message: 'Node version fail', level: 1}], signal: 12}, + {messages: [{rule: 'tester/react-version', message: 'React version fail', level: 2}], signal: 17}, + {messages: [], signal: true} + ] + Results.create(executions).logTable() + + expect(this.addRowStub.firstCall.firstArg).to.be.eql({rule: 'tester/node-version', message: 'Node version fail'}) + expect(this.addRowStub.firstCall.lastArg).to.be.eql({color: 'yellow'}) + + expect(this.addRowStub.secondCall.firstArg).to.be.eql({rule: 'tester/react-version', message: 'React version fail'}) + expect(this.addRowStub.secondCall.lastArg).to.be.eql({color: 'red'}) + }) + + it('Should print a table with all the monitorings', function () { + const executions = [ + {messages: [{rule: 'tester/node-version', message: 'Node version fail', level: 1}], signal: 12}, + {messages: [{rule: 'tester/react-version', message: 'React version fail', level: 2}], signal: 17}, + {messages: [], signal: true} + ] + Results.create(executions).logMonitorings() + + expect(this.addRowStub.firstCall.firstArg).to.be.eql(12) + expect(this.addRowStub.secondCall.firstArg).to.be.eql(17) + expect(this.addRowStub.thirdCall.firstArg).to.be.eql(true) + }) + + it('Should print a JSON output', function () { + const executions = [ + {messages: [{rule: 'tester/node-version', message: 'Node version fail', level: 1}], signal: 12}, + {messages: [{rule: 'tester/react-version', message: 'React version fail', level: 2}], signal: 17}, + {messages: [], signal: true} + ] + Results.create(executions).logJSON() + + expect( + this.logStub.calledWith( + JSON.stringify( + [ + {rule: 'tester/node-version', message: 'Node version fail', level: 1}, + {rule: 'tester/react-version', message: 'React version fail', level: 2} + ], + null, + 2 + ) + ) + ).to.be.eq(true) + }) +}) diff --git a/packages/sui-lint/test/server/RunnerSpec.js b/packages/sui-lint/test/server/RunnerSpec.js new file mode 100644 index 000000000..282c75406 --- /dev/null +++ b/packages/sui-lint/test/server/RunnerSpec.js @@ -0,0 +1,26 @@ +import {expect} from 'chai' +import {stub} from 'sinon' + +import {Match} from '../../src/RepositoryLinter/Match.js' +import {Runner} from '../../src/RepositoryLinter/Runner.js' + +describe('Runner', function () { + beforeEach(function () { + this.syncStub = stub() + this.matchCreateStub = stub(Match, 'create') + }) + + afterEach(function () { + this.syncStub.reset() + this.matchCreateStub.restore() + }) + + it('Should return a list of matches', function () { + this.syncStub.returns(['path/file.json']) + + Runner.create({sync: this.syncStub}).assertion('**/*.json') + + expect(this.matchCreateStub.firstCall.firstArg).to.be.eql('path/file.json') + expect(this.syncStub.firstCall.firstArg).to.be.eql('**/*.json') + }) +}) From 1c34ef239558f8d4106fbbfdfd9b8c31f1a12eb8 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Tue, 5 Mar 2024 10:52:40 +0100 Subject: [PATCH 033/103] Update packages/sui-lint/bin/sui-lint-js.js Co-authored-by: Kiko Ruiz Lloret --- packages/sui-lint/bin/sui-lint-js.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index 91a826d04..65aca8901 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -14,7 +14,7 @@ program .option('--fix', 'fix automatically problems with js files') .option('--ignore-patterns ', 'Path patterns to ignore for linting') .option('--reporter ', 'Send results to DD using sui-logger') - .option('--pattern ', 'Patter of files to lint') + .option('--pattern ', 'Pattern of files to lint') .parse(process.argv) const {addFixes, fix, ignorePatterns = [], staged, pattern, reporter} = program.opts() From ab16dc574dd55c05ba032fd6db2b2b327cb00767 Mon Sep 17 00:00:00 2001 From: Kiko Ruiz Lloret Date: Tue, 5 Mar 2024 13:12:36 +0100 Subject: [PATCH 034/103] feat(packages/lint-repository-sui): add typescript lint repository rule --- packages/lint-repository-sui/src/index.js | 4 +- .../src/rules/typescript.js | 80 +++++++++++++++++++ .../test/server/typescriptSpec.js | 49 ++++++++++++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 packages/lint-repository-sui/src/rules/typescript.js create mode 100644 packages/lint-repository-sui/test/server/typescriptSpec.js diff --git a/packages/lint-repository-sui/src/index.js b/packages/lint-repository-sui/src/index.js index 3ec72031b..d1f8a51d1 100644 --- a/packages/lint-repository-sui/src/index.js +++ b/packages/lint-repository-sui/src/index.js @@ -2,6 +2,7 @@ const NodeVersion = require('./rules/node-version.js') const ReactVersion = require('./rules/react-version.js') const PackageLock = require('./rules/package-lock.js') const GithubAction = require('./rules/github-action.js') +const TypeScript = require('./rules/typescript.js') // ------------------------------------------------------------------------------ // Plugin Definition @@ -13,6 +14,7 @@ module.exports = { 'node-version': NodeVersion, 'react-version': ReactVersion, 'package-lock': PackageLock, - 'github-action': GithubAction + 'github-action': GithubAction, + typescript: TypeScript } } diff --git a/packages/lint-repository-sui/src/rules/typescript.js b/packages/lint-repository-sui/src/rules/typescript.js new file mode 100644 index 000000000..a2a4dba4e --- /dev/null +++ b/packages/lint-repository-sui/src/rules/typescript.js @@ -0,0 +1,80 @@ +const dedent = require('string-dedent') + +const MIN_TYPESCRIPT_VERSION = 5 + +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'Check that your repository has a `tsconfig.json` file', + recommended: true, + url: null + }, + fixable: null, + schema: [], + messages: { + missingTypescriptDependency: dedent` + Your project doesn't have installed TypeScript. + Please install at least the version ${MIN_TYPESCRIPT_VERSION}. + If you are not sure about it, please contact Web Platform. + `, + badTypescriptVersion: dedent` + Please be sure that your repository use the latest TypeScript version ${MIN_TYPESCRIPT_VERSION}. + Your current version is {{version}}. + If you are not sure about it, please contact Web Platform. + `, + noTSConfigFile: dedent` + Every project must have a \`tsconfig.json\` file to setup TypeScript in the project. + If you are not sure about how do it, please contact with Web Platform team. + ` + } + }, + create: function (context) { + return { + 'tsconfig.json': () => { + // TO-DO: Check TypeScript configuration is the one we expect. + + return context.monitoring(true) + }, + 'package-lock.json': matches => { + const [packageLock] = matches + let version = packageLock?.parsed?.packages?.['node_modules/typescript']?.version + + // Check if repository is defining TS as dependency. + if (!version) { + context.report({ + messageId: 'missingTypescriptDependency' + }) + + return context.monitoring(false) + } + + version = version.split('.')[0] + + // Check if repository is using minimum expected version. + if (Number.parseInt(version) < MIN_TYPESCRIPT_VERSION) { + context.report({ + messageId: 'badTypescriptVersion', + data: {version} + }) + + return context.monitoring(false) + } + + return context.monitoring(true) + }, + missmatch: () => { + context.report({ + messageId: 'noTSConfigFile' + }) + + return context.monitoring(false) + } + } + }, + reduceMonitoring: function (monitorings) { + return monitorings.reduce((acc, signal) => { + return acc && signal.value + }, true) + } +} diff --git a/packages/lint-repository-sui/test/server/typescriptSpec.js b/packages/lint-repository-sui/test/server/typescriptSpec.js new file mode 100644 index 000000000..41d63d86b --- /dev/null +++ b/packages/lint-repository-sui/test/server/typescriptSpec.js @@ -0,0 +1,49 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/typescript.js' +import {MatchStub, RuleTester} from '../TestHelpers.js' + +RuleTester.create('typescript', handler).run({ + valid: [ + { + 'tsconfig.json': [], + name: 'File `tsconfig.json` exists', + monitoring: true + }, + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/typescript': {version: '5.0.4'}}}})], + name: 'TypeScript version is correct', + monitoring: true + } + ], + invalid: [ + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/typescript': {version: '4.2.0'}}}})], + name: 'TypeScript version is not correct', + report: dedent` + Please be sure that your repository use the latest TypeScript version 5. + Your current version is 4. + If you are not sure about it, please contact Web Platform. + `, + monitoring: false + }, + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {}}})], + name: 'TypeScript dependency is not installed', + report: dedent` + Your project doesn't have installed TypeScript. + Please install at least the version 5. + If you are not sure about it, please contact Web Platform. + `, + monitoring: false + }, + { + missmatch: '', + report: dedent` + Every project must have a \`tsconfig.json\` file to setup TypeScript in the project. + If you are not sure about how do it, please contact with Web Platform team. + `, + monitoring: false + } + ] +}) From 508fd8493180bbd6bab63e710e47fd2a6396f166 Mon Sep 17 00:00:00 2001 From: Kiko Ruiz Lloret Date: Tue, 5 Mar 2024 13:12:43 +0100 Subject: [PATCH 035/103] feat(packages/sui-lint): add typescript lint repository rule --- packages/sui-lint/package.json | 4 ++-- packages/sui-lint/repository.config.js | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index 9f4181b1e..32926f790 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -39,8 +39,8 @@ "eslint-plugin-react": "7.30.1", "eslint-plugin-react-hooks": "4.6.0", "eslint-plugin-simple-import-sort": "7.0.0", - "eslint-plugin-sui": "beta", - "lint-repository-sui": "beta", + "eslint-plugin-sui": "../eslint-plugin-sui", + "lint-repository-sui": "../lint-repository-sui", "postcss-scss": "4.0.4", "prettier": "2.7.1", "stylelint": "14.11.0", diff --git a/packages/sui-lint/repository.config.js b/packages/sui-lint/repository.config.js index a4f0bf935..bd8ef0308 100644 --- a/packages/sui-lint/repository.config.js +++ b/packages/sui-lint/repository.config.js @@ -10,6 +10,7 @@ module.exports = { 'sui/node-version': RULES.WARNING, 'sui/react-version': RULES.WARNING, 'sui/package-lock': RULES.WARNING, - 'sui/github-action': RULES.WARNING + 'sui/github-action': RULES.WARNING, + 'sui/typescript': RULES.WARNING } } From 17c5f5e792dcefeae3a24b5e4c6da827d267b9ce Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Tue, 5 Mar 2024 14:56:08 +0100 Subject: [PATCH 036/103] refactor(packages/eslint-plugin-sui): avoid magic string add url to aggrements --- packages/eslint-plugin-sui/package.json | 10 ++++------ .../eslint-plugin-sui/src/rules/factory-pattern.js | 2 +- .../eslint-plugin-sui/src/rules/forbidden-require.js | 2 +- .../src/rules/function-named-parameters.js | 2 +- .../eslint-plugin-sui/src/rules/naming-convention.js | 2 +- .../src/rules/serialize-deserialize.js | 2 +- 6 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index bb67458ac..4bab4403e 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -18,17 +18,15 @@ "update:eslint-docs": "eslint-doc-generator" }, "dependencies": { - "requireindex": "^1.2.0", - "string-dedent": "^3.0.1" + "requireindex": "1.2.0", + "string-dedent": "3.0.1" }, "devDependencies": { - "@s-ui/test": "^8.33.0", + "@s-ui/test": "8", "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" + "eslint-plugin-node": "^11.1.0" }, "engines": { "node": "^14.17.0 || ^16.0.0 || >= 18.0.0" diff --git a/packages/eslint-plugin-sui/src/rules/factory-pattern.js b/packages/eslint-plugin-sui/src/rules/factory-pattern.js index 97cf15290..23a13c446 100644 --- a/packages/eslint-plugin-sui/src/rules/factory-pattern.js +++ b/packages/eslint-plugin-sui/src/rules/factory-pattern.js @@ -17,7 +17,7 @@ module.exports = { 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' + url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements' }, fixable: null, schema: [], diff --git a/packages/eslint-plugin-sui/src/rules/forbidden-require.js b/packages/eslint-plugin-sui/src/rules/forbidden-require.js index ec3dbe74e..5e31b845b 100644 --- a/packages/eslint-plugin-sui/src/rules/forbidden-require.js +++ b/packages/eslint-plugin-sui/src/rules/forbidden-require.js @@ -17,7 +17,7 @@ module.exports = { docs: { description: 'ensure to use only ESM (import) style', recommended: true, - url: null + url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements' }, fixable: null, schema: [], diff --git a/packages/eslint-plugin-sui/src/rules/function-named-parameters.js b/packages/eslint-plugin-sui/src/rules/function-named-parameters.js index d7fcd975a..e4327df59 100644 --- a/packages/eslint-plugin-sui/src/rules/function-named-parameters.js +++ b/packages/eslint-plugin-sui/src/rules/function-named-parameters.js @@ -17,7 +17,7 @@ module.exports = { docs: { description: 'ensure to use named parameters', recommended: true, - url: null + url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements' }, fixable: null, schema: [], diff --git a/packages/eslint-plugin-sui/src/rules/naming-convention.js b/packages/eslint-plugin-sui/src/rules/naming-convention.js index 68477031f..7da81c742 100644 --- a/packages/eslint-plugin-sui/src/rules/naming-convention.js +++ b/packages/eslint-plugin-sui/src/rules/naming-convention.js @@ -17,7 +17,7 @@ module.exports = { docs: { description: 'ensure to use a proper naming convention', recommended: true, - url: null + url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements' }, fixable: null, schema: [], diff --git a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js index 82232e19a..0a62ac3be 100644 --- a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js +++ b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js @@ -16,7 +16,7 @@ module.exports = { docs: { description: 'ensure entity create - toJSON', recommended: false, - url: null + url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements' }, fixable: null, schema: [], From 9d51b66cf99ecbfefa8861920a3ec23a0446cb22 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Tue, 5 Mar 2024 14:57:56 +0100 Subject: [PATCH 037/103] feat(packages/lint-repository-sui): Avoid magic strings --- packages/lint-repository-sui/package.json | 2 +- packages/lint-repository-sui/src/rules/node-version.js | 6 ++++-- packages/lint-repository-sui/src/rules/react-version.js | 8 +++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/lint-repository-sui/package.json b/packages/lint-repository-sui/package.json index 46455cfa6..8bbd986c7 100644 --- a/packages/lint-repository-sui/package.json +++ b/packages/lint-repository-sui/package.json @@ -10,6 +10,6 @@ "test": "npx sui-test server" }, "devDependencies": { - "@s-ui/test": "^8.33.0" + "@s-ui/test": "8" } } diff --git a/packages/lint-repository-sui/src/rules/node-version.js b/packages/lint-repository-sui/src/rules/node-version.js index 0e53b0f68..802bc7f80 100644 --- a/packages/lint-repository-sui/src/rules/node-version.js +++ b/packages/lint-repository-sui/src/rules/node-version.js @@ -1,5 +1,7 @@ const dedent = require('string-dedent') +const REACT_VERSION = '20' + module.exports = { meta: { type: 'problem', @@ -18,7 +20,7 @@ module.exports = { `, badNodeVersion: dedent` Your current Node version is {{version}}. - Please be sure that your repository use the latest Node Version 20. + Please be sure that your repository use the latest Node Version ${REACT_VERSION}. If you are not sure about how do it, please contact with Platform Web. `, noNMVRCFile: dedent` @@ -39,7 +41,7 @@ module.exports = { const [nvmrcMatch] = matches const version = nvmrcMatch.raw.trim() - if (version !== '20') { + if (version !== REACT_VERSION) { context.report({ messageId: 'badNodeVersion', data: {version} diff --git a/packages/lint-repository-sui/src/rules/react-version.js b/packages/lint-repository-sui/src/rules/react-version.js index 38411eb7f..acca08e0c 100644 --- a/packages/lint-repository-sui/src/rules/react-version.js +++ b/packages/lint-repository-sui/src/rules/react-version.js @@ -1,5 +1,7 @@ const dedent = require('string-dedent') +const REACT_VERSION = '18' + module.exports = { meta: { type: 'problem', @@ -12,13 +14,13 @@ module.exports = { schema: [], messages: { badReactVersion: dedent` - Please be sure that your repository use the latest React Version 18. + Please be sure that your repository use the latest React Version ${REACT_VERSION}. Your current version is {{version}}. If you are not sure about how do it, please contact with Platform Web. `, missingReactDependencie: dedent` Your project doesnt have installed React. - Please install at least the version 18. + Please install at least the version ${REACT_VERSION}. If you are not sure about how do it, please contact with Platform Web. `, missingPackageLock: dedent` @@ -42,7 +44,7 @@ module.exports = { version = version.split('.')[0] - if (version !== '18') { + if (version !== REACT_VERSION) { context.report({ messageId: 'badReactVersion', data: {version} From de098d69d5cfa0bf3e26bd4eaac21b4ed9d25902 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Tue, 5 Mar 2024 14:58:30 +0100 Subject: [PATCH 038/103] refactor(packages/sui-lint): refactor copies --- packages/sui-lint/bin/sui-lint-js.js | 3 ++- packages/sui-lint/src/RepositoryLinter/Results.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index 65aca8901..e4445b543 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -13,7 +13,7 @@ program .option('--staged') .option('--fix', 'fix automatically problems with js files') .option('--ignore-patterns ', 'Path patterns to ignore for linting') - .option('--reporter ', 'Send results to DD using sui-logger') + .option('--reporter ', 'Send results using a custom reporter') .option('--pattern ', 'Pattern of files to lint') .parse(process.argv) @@ -58,6 +58,7 @@ const baseConfig = { const {JSReporter} = await import(reporter) const reportered = await JSReporter.create() await reportered.map(results).send() + console.log('[sui-lint] All your stats has been sent', reporter) } if (fix) { diff --git a/packages/sui-lint/src/RepositoryLinter/Results.js b/packages/sui-lint/src/RepositoryLinter/Results.js index 6b837ca75..00de9d780 100644 --- a/packages/sui-lint/src/RepositoryLinter/Results.js +++ b/packages/sui-lint/src/RepositoryLinter/Results.js @@ -52,7 +52,7 @@ module.exports.Results = class Results { if (this.#monitorings.length === 0) return this.log(dedent` - There is not signal to be send to DD. Use 'context.monitoring' to add signals at your execution + There is not signal to be send. Use 'context.monitoring' to add signals at your execution `) From 70d37f8afc84aa93daea20cd55d1c75f50c79e69 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Tue, 5 Mar 2024 15:56:49 +0100 Subject: [PATCH 039/103] feat(packages/sui-js): Add sha256 hash method --- packages/sui-js/src/hash/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/sui-js/src/hash/index.js b/packages/sui-js/src/hash/index.js index 8176a44fe..9c2877e8a 100644 --- a/packages/sui-js/src/hash/index.js +++ b/packages/sui-js/src/hash/index.js @@ -2,4 +2,14 @@ const m = [-680876936,-389564586,606105819,-1044525330,-176418897,1200080426,-1473231341,-45705983,1770035416,-1958414417,-42063,-1990404162,1804603682,-40341101,-1502002290,1236535329,-165796510,-1069501632,643717713,-373897302,-701558691,38016083,-660478335,-405537848,568446438,-1019803690,-187363961,1163531501,-1444681467,-51403784,1735328473,-1926607734,-378558,-2022574463,1839030562,-35309556,-1530992060,1272893353,-155497632,-1094730640,681279174,-358537222,-722521979,76029189,-640364487,-421815835,530742520,-995338651,-198630844,1126891415,-1416354905,-57434055,1700485571,-1894986606,-1051523,-2054922799,1873313359,-30611744,-1560198380,1309151649,-145523070,-1120210379,718787259,-343485551] // eslint-disable-line const md5=function(c){var e,g,f,a,h=[];c=unescape(encodeURI(c));for(var b=c.length,k=[e=1732584193,g=-271733879,~e,~g],d=0;d<=b;)h[d>>2]|=(c.charCodeAt(d)||128)<<8*(d++%4);h[c=16*(b+8>>6)+14]=8*b;for(d=0;da;)b=[f=b[3],(e=b[1]|0)+((f=b[0]+[e&(g=b[2])|~e&f,f&e|~f&g,e^g^f,g^(e|~f)][b=a>>4]+(m[a]+(h[[a,5*a+1,3*a+5,7*a][b]%16+d]|0)))<<(b=[7,12,17,22,5,9,14,20,4,11,16,23,6,10,15,21][4*b+a++%4])|f>>>32-b),e,g];for(a=4;a;)k[--a]=k[a]+b[a]}for(c="";32>a;)c+=(k[a>>3]>>4*(1^a++&7)&15).toString(16);return c} // eslint-disable-line +// extracted from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string +const sha256 = async value => { + const msgUint8 = new TextEncoder().encode(value) + const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8) + const hashArray = Array.from(new Uint8Array(hashBuffer)) + const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('') + return hashHex +} + export const createHash = md5 +export const createAsyncSha256 = sha256 From 5fc7bdaa1f57d16cb305a52c0c7923577f97a4d5 Mon Sep 17 00:00:00 2001 From: Lola Rufino Date: Wed, 6 Mar 2024 13:39:54 +0100 Subject: [PATCH 040/103] fix(packages/sui-svg): upgrade vite version in sui svg for security reasons --- packages/sui-svg/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sui-svg/package.json b/packages/sui-svg/package.json index 5d016a756..ac48189c1 100644 --- a/packages/sui-svg/package.json +++ b/packages/sui-svg/package.json @@ -22,7 +22,7 @@ "react-dom": "17", "sass": "1", "svgo": "2.8.0", - "vite": "3.2.7" + "vite": "3.2.8" }, "license": "MIT" } From 0b19a1b8df16410ac6f42918672c2869c5ddf21e Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 7 Mar 2024 07:55:31 +0000 Subject: [PATCH 041/103] release(packages/sui-svg): v3.24.0 [skip ci] --- packages/sui-svg/CHANGELOG.md | 14 ++++++++++---- packages/sui-svg/package.json | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/sui-svg/CHANGELOG.md b/packages/sui-svg/CHANGELOG.md index 8c7e22ae0..5b308308a 100644 --- a/packages/sui-svg/CHANGELOG.md +++ b/packages/sui-svg/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 3.24.0 (2024-03-07) + + +### Bug Fixes + +* upgrade vite version in sui svg for security reasons ([5fc7bda](https://github.com/SUI-Components/sui/commit/5fc7bdaa1f57d16cb305a52c0c7923577f97a4d5)) + + + # 3.23.0 (2023-08-14) @@ -468,7 +477,4 @@ from the SuiAtomIcon component * **sui-svg:** first commit ([142511c](https://github.com/SUI-Components/sui/commit/142511cab32b2680ff58628d6473383cb8686f97)) * **sui-svg:** remove not needed dependencies ([2639e7e](https://github.com/SUI-Components/sui/commit/2639e7e09c0d20961a3478eeff0916904fa118bb)) -* **sui-svg:** update dependencies + new dev server approach ([d8e7440](https://github.com/SUI-Components/sui/commit/d8e7440efdefb584cedf6b03784d984a56adbab0)) - - - +* **sui-svg:** update dependencies + new dev server approach ([d8e7440](https://github.com/SUI-Components/sui/commit/d8e7440efdefb584cedf6b03784d984a56adbab0)) \ No newline at end of file diff --git a/packages/sui-svg/package.json b/packages/sui-svg/package.json index ac48189c1..14424365a 100644 --- a/packages/sui-svg/package.json +++ b/packages/sui-svg/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/svg", - "version": "3.23.0", + "version": "3.24.0", "description": "Converts svg files into React Components", "main": "index.js", "type": "module", From a785ed9682a04f1962b8fac39b55c41560e6e2d0 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 7 Mar 2024 09:12:41 +0000 Subject: [PATCH 042/103] release(packages/sui-js): v2.30.0 [skip ci] --- packages/sui-js/CHANGELOG.md | 14 ++++++++++---- packages/sui-js/package.json | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/sui-js/CHANGELOG.md b/packages/sui-js/CHANGELOG.md index 030cfb466..581c5c5b5 100644 --- a/packages/sui-js/CHANGELOG.md +++ b/packages/sui-js/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 2.30.0 (2024-03-07) + + +### Features + +* Add sha256 hash method ([70d37f8](https://github.com/SUI-Components/sui/commit/70d37f8afc84aa93daea20cd55d1c75f50c79e69)) + + + # 2.29.0 (2023-12-04) @@ -555,7 +564,4 @@ * **sui-js:** init sui-js package ([84719ae](https://github.com/SUI-Components/sui/commit/84719ae1a45f5456f7b39120bb9859dbbe6ae4f2)) * **sui-js:** insert a façade ([8380efb](https://github.com/SUI-Components/sui/commit/8380efbe44bb94e5d4a17ee2849524adc12d22e7)) * **sui-js:** remove unused dependencies ([4bc24c2](https://github.com/SUI-Components/sui/commit/4bc24c2eff61603cf59aa1fdc80d46aac1000df8)) -* **sui-js:** rename browser in to ua-parser ([dda6032](https://github.com/SUI-Components/sui/commit/dda6032235ef53a29f48fed9625a5a268fadfa0c)) - - - +* **sui-js:** rename browser in to ua-parser ([dda6032](https://github.com/SUI-Components/sui/commit/dda6032235ef53a29f48fed9625a5a268fadfa0c)) \ No newline at end of file diff --git a/packages/sui-js/package.json b/packages/sui-js/package.json index 73870fbfb..5b958d3b5 100644 --- a/packages/sui-js/package.json +++ b/packages/sui-js/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/js", - "version": "2.29.0", + "version": "2.30.0", "description": "Set of JS utilities", "type": "module", "scripts": { From 369b096faf6ff77791d41eac9b136ff68ff814b8 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Fri, 8 Mar 2024 12:03:25 +0100 Subject: [PATCH 043/103] feat(packages/eslint-plugin-sui): Improve rules to be more resilient --- packages/eslint-plugin-sui/package.json | 10 ++-- .../src/rules/factory-pattern.js | 4 +- .../src/rules/serialize-deserialize.js | 22 ++++++-- .../test/server/factory-pattern.js | 53 ++++++++++++++++++- .../test/server/serialize-deserialize.js | 23 +++++++- 5 files changed, 97 insertions(+), 15 deletions(-) diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index 4bab4403e..0bfb0bedb 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -14,7 +14,7 @@ "lint": "npm-run-all \"lint:*\"", "lint:eslint-docs": "npm-run-all \"update:eslint-docs -- --check\"", "lint:js": "eslint .", - "test": "npx sui-test server -P ./tests/**/*.js", + "test": "npx sui-test server", "update:eslint-docs": "eslint-doc-generator" }, "dependencies": { @@ -23,10 +23,10 @@ }, "devDependencies": { "@s-ui/test": "8", - "eslint": "^8.19.0", - "eslint-doc-generator": "^1.0.0", - "eslint-plugin-eslint-plugin": "^5.0.0", - "eslint-plugin-node": "^11.1.0" + "eslint": "8.19.0", + "eslint-doc-generator": "1.0.0", + "eslint-plugin-eslint-plugin": "5.0.0", + "eslint-plugin-node": "11.1.0" }, "engines": { "node": "^14.17.0 || ^16.0.0 || >= 18.0.0" diff --git a/packages/eslint-plugin-sui/src/rules/factory-pattern.js b/packages/eslint-plugin-sui/src/rules/factory-pattern.js index 23a13c446..51168657b 100644 --- a/packages/eslint-plugin-sui/src/rules/factory-pattern.js +++ b/packages/eslint-plugin-sui/src/rules/factory-pattern.js @@ -48,14 +48,14 @@ module.exports = { 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 + methodDefinition.value?.body?.body?.find?.(body => body.type === 'ReturnStatement')?.argument?.callee?.name === node?.id?.name // eslint-disable-line ) }) ) if (!hasStaticFactoryMethod) { context.report({ - node, + node: node?.id ?? node.superClass ?? node, messageId: 'notFoundFactoryFunction' }) } diff --git a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js index 0a62ac3be..c8faadbf8 100644 --- a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js +++ b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js @@ -30,6 +30,10 @@ module.exports = { missingCreateMethod: dedent` If your class has a 'toJSON' method. You have to define a 'static create' method too. The output of the 'toJSON' should be the same as the input of your 'static create' method + `, + forbiddenSpreadElements: dedent` + Spread operation are not allowed as part of the toJSON function. + The output of the 'toJSON' should be the same as the input of your 'static create' method ` } }, @@ -39,7 +43,7 @@ module.exports = { ClassDeclaration(node) { const create = node.body.body.find(i => i.key.name === 'create') const toJSON = node.body.body.find(i => i.key.name === 'toJSON') - const className = node.id.name + const className = node?.id?.name ?? '' if (['UseCase', 'Service', 'Repository'].some(allowWord => className.includes(allowWord))) return // eslint-disable-line @@ -47,13 +51,13 @@ module.exports = { if (create && !toJSON) return context.report({ - node: create, + node: create.key, messageId: 'missingToJSONMethod' }) if (toJSON && !create) return context.report({ - node: toJSON, + node: toJSON.key, messageId: 'missingCreateMethod' }) @@ -65,9 +69,17 @@ module.exports = { const createProperties = createParams.properties const toJSONProperties = toJSON.value.body.body[0].argument.properties + const spreadElement = toJSONProperties?.find(node => node.type === 'SpreadElement') + if(spreadElement) { + return context.report({ + node: spreadElement, + messageId: 'forbiddenSpreadElements' + }) + } + if (!toJSONProperties) { return context.report({ - node: toJSON, + node: toJSON.key, messageId: 'invalidTOJSONProperties' }) } @@ -78,7 +90,7 @@ module.exports = { const missingToJSONProps = createProps.filter(p => !toJSONProps.find(e => e === p)) if (missingToJSONProps.length) { context.report({ - node: toJSON, + node: toJSON.key, messageId: 'toJSONProperties', data: { props: missingToJSONProps.join(', ') diff --git a/packages/eslint-plugin-sui/test/server/factory-pattern.js b/packages/eslint-plugin-sui/test/server/factory-pattern.js index 2e63d949a..2adf1d6f6 100644 --- a/packages/eslint-plugin-sui/test/server/factory-pattern.js +++ b/packages/eslint-plugin-sui/test/server/factory-pattern.js @@ -8,7 +8,7 @@ import rule from '../../src/rules/factory-pattern.js' // more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester // ------------------------------------------------------------------------------ -const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) +const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 2018, sourceType: 'module'}}) ruleTester.run('factory-pattern', rule, { valid: [ { @@ -33,7 +33,56 @@ ruleTester.run('factory-pattern', rule, { Avoid to use the 'new' keyword directly in your code. Use always a factory function `, - type: 'ClassDeclaration' + } + ] + }, + { + code: dedent` + class Config { + static create() { + return {API_URL: 'google.com'} + } + } + `, + errors: [ + { + message: 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 + `, + } + ] + }, + { + code: dedent` + class Config { + static create() { + return () => {} + } + } + `, + errors: [ + { + message: 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 + `, + } + ] + }, + { + code: dedent` + export default class extends Model {} + `, + errors: [ + { + message: 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 + `, } ] } diff --git a/packages/eslint-plugin-sui/test/server/serialize-deserialize.js b/packages/eslint-plugin-sui/test/server/serialize-deserialize.js index e5fd6d925..ea8ea49b5 100644 --- a/packages/eslint-plugin-sui/test/server/serialize-deserialize.js +++ b/packages/eslint-plugin-sui/test/server/serialize-deserialize.js @@ -8,7 +8,7 @@ import rule from '../../src/rules/serialize-deserialize.js' // more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester // ------------------------------------------------------------------------------ -const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 6}}) +const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 2018}}) ruleTester.run('serialize-deserialize', rule, { valid: [ { @@ -91,6 +91,27 @@ ruleTester.run('serialize-deserialize', rule, { } `, errors: [{message: 'Missing toJSON properties (id, name)'}] + }, + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + return { + Noid: this.id, + ...this.user.toJSON() + } + } + } + `, + errors: [{message: dedent` + Spread operation are not allowed as part of the toJSON function. + The output of the 'toJSON' should be the same as the input of your 'static create' method + `}] } ] }) From 164ddf24f3bf05010c8f125f2e97f25031667a6f Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Fri, 8 Mar 2024 12:04:31 +0100 Subject: [PATCH 044/103] feat(packages/lint-repository-sui): Use only the major version of Node --- packages/lint-repository-sui/src/rules/node-version.js | 8 ++++---- .../lint-repository-sui/test/server/node-versionSpec.js | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/lint-repository-sui/src/rules/node-version.js b/packages/lint-repository-sui/src/rules/node-version.js index 802bc7f80..5b15ebbda 100644 --- a/packages/lint-repository-sui/src/rules/node-version.js +++ b/packages/lint-repository-sui/src/rules/node-version.js @@ -1,6 +1,6 @@ const dedent = require('string-dedent') -const REACT_VERSION = '20' +const NODE_VERSION = '20' module.exports = { meta: { @@ -20,7 +20,7 @@ module.exports = { `, badNodeVersion: dedent` Your current Node version is {{version}}. - Please be sure that your repository use the latest Node Version ${REACT_VERSION}. + Please be sure that your repository use the latest Node Version ${NODE_VERSION}. If you are not sure about how do it, please contact with Platform Web. `, noNMVRCFile: dedent` @@ -40,8 +40,8 @@ module.exports = { } const [nvmrcMatch] = matches - const version = nvmrcMatch.raw.trim() - if (version !== REACT_VERSION) { + const [version] = nvmrcMatch.raw.trim().split('.') + if (version !== NODE_VERSION) { context.report({ messageId: 'badNodeVersion', data: {version} diff --git a/packages/lint-repository-sui/test/server/node-versionSpec.js b/packages/lint-repository-sui/test/server/node-versionSpec.js index c113232b9..7314dd4e0 100644 --- a/packages/lint-repository-sui/test/server/node-versionSpec.js +++ b/packages/lint-repository-sui/test/server/node-versionSpec.js @@ -3,6 +3,8 @@ import dedent from 'dedent' import handler from '../../src/rules/node-version.js' import {MatchStub, RuleTester} from '../TestHelpers.js' + + RuleTester.create('node-version', handler).run({ valid: [ { @@ -23,7 +25,7 @@ RuleTester.create('node-version', handler).run({ monitoring: 0 }, { - '.nvmrc': [MatchStub.create({raw: '16'})], + '.nvmrc': [MatchStub.create({raw: '16.1.3'})], name: 'Exits more than one nvmrc file', report: dedent` Your current Node version is 16. From c42244c87d7e38392458751c1bfea9dbfa5e2809 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Fri, 8 Mar 2024 12:06:08 +0100 Subject: [PATCH 045/103] feat(packages/sui-lint): Allow relative path in reporter flag and select TS or JS eslint config --- packages/sui-lint/bin/sui-lint-js.js | 8 +- packages/sui-lint/bin/sui-lint-repository.js | 6 +- .../sui-lint/{eslintrc.js => eslintrc.js.js} | 10 +- packages/sui-lint/eslintrc.ts.js | 231 ++++++++++++++++++ packages/sui-lint/package.json | 4 +- 5 files changed, 249 insertions(+), 10 deletions(-) rename packages/sui-lint/{eslintrc.js => eslintrc.js.js} (97%) create mode 100644 packages/sui-lint/eslintrc.ts.js diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index e4445b543..55b884dc2 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -2,11 +2,13 @@ /* eslint-disable no-console */ // @ts-check +const path = require('path') +const fs = require("fs") const program = require('commander') const {checkFilesToLint, getFilesToLint, getGitIgnoredFiles, stageFilesIfRequired} = require('../src/helpers.js') const {ESLint} = require('eslint') -const config = require('../eslintrc.js') +const config = fs.existsSync(process.cwd() + '/tsconfig.json') ? require('../eslintrc.ts') : require('../eslintrc.js') program .option('--add-fixes') @@ -55,7 +57,9 @@ const baseConfig = { if (reporter) { console.log('[sui-lint] Sending stats using the reporter ', reporter) - const {JSReporter} = await import(reporter) + const reporterPath = path.isAbsolute(reporter) ? reporter : path.join(process.cwd() + '/' + reporter) + console.log({reporter, isAbsolute: path.isAbsolute(reporter), reporterPath}) + const {JSReporter} = await import(reporterPath) const reportered = await JSReporter.create() await reportered.map(results).send() console.log('[sui-lint] All your stats has been sent', reporter) diff --git a/packages/sui-lint/bin/sui-lint-repository.js b/packages/sui-lint/bin/sui-lint-repository.js index e8d845225..86f96d144 100755 --- a/packages/sui-lint/bin/sui-lint-repository.js +++ b/packages/sui-lint/bin/sui-lint-repository.js @@ -1,5 +1,6 @@ #!/usr/bin/env node /* eslint-disable no-console */ +const path = require('path') const program = require('commander') const {RepositoryLinter} = require('../src/RepositoryLinter') @@ -25,7 +26,10 @@ const {reporter, outputJson} = program.opts() if (reporter) { console.log('\n[sui-lint] Sending stats using the reporter\n\n', reporter) - const {RepositoryReporter} = await import(reporter) + const reporterPath = path.isAbsolute(reporter) ? reporter : path.join(process.cwd() + '/' + reporter) + console.log({reporter, isAbsolute: path.isAbsolute(reporter), reporterPath}) + const {RepositoryReporter} = await import(reporterPath) + const reportered = RepositoryReporter.create() await reportered.map(results).send() results.logMonitorings() diff --git a/packages/sui-lint/eslintrc.js b/packages/sui-lint/eslintrc.js.js similarity index 97% rename from packages/sui-lint/eslintrc.js rename to packages/sui-lint/eslintrc.js.js index 7f089f0e2..5eeb20833 100644 --- a/packages/sui-lint/eslintrc.js +++ b/packages/sui-lint/eslintrc.js.js @@ -175,8 +175,8 @@ module.exports = { 'react-hooks', 'simple-import-sort', 'jest', - 'jest-dom' - // 'sui' + 'jest-dom', + 'sui' ], rules: { ...REACT_RULES, @@ -199,9 +199,9 @@ module.exports = { 'prefer-regex-literals': RULES.WARNING, 'prettier/prettier': [RULES.ERROR, prettierOptions], 'simple-import-sort/imports': [RULES.WARNING, {groups: IMPORT_SORT_GROUPS}], - 'react/jsx-no-bind': RULES.OFF - // 'sui/factory-pattern': RULES.ERROR, - // 'sui/serialize-deserialize': RULES.ERROR + 'react/jsx-no-bind': RULES.OFF, + 'sui/factory-pattern': RULES.WARNING, + 'sui/serialize-deserialize': RULES.WARNING }, overrides: [ { diff --git a/packages/sui-lint/eslintrc.ts.js b/packages/sui-lint/eslintrc.ts.js new file mode 100644 index 000000000..9f4b2658e --- /dev/null +++ b/packages/sui-lint/eslintrc.ts.js @@ -0,0 +1,231 @@ +const prettierOptions = require('./.prettierrc.js') + +const RULES = { + OFF: 0, + WARNING: 1, + ERROR: 2 +} + +const REACT_RULES = { + 'react-hooks/exhaustive-deps': RULES.WARNING, // Checks effect dependencies + 'react-hooks/rules-of-hooks': RULES.ERROR, // Checks rules of Hooks + 'react/default-props-match-prop-types': RULES.WARNING, + 'react/jsx-handler-names': RULES.WARNING, + 'react/jsx-no-bind': RULES.WARNING, + 'react/jsx-no-duplicate-props': [RULES.WARNING, {ignoreCase: true}], + 'react/jsx-no-undef': RULES.WARNING, + 'react/jsx-pascal-case': [ + RULES.WARNING, + { + allowAllCaps: true, + ignore: [] + } + ], + 'react/jsx-uses-react': RULES.OFF, + 'react/jsx-uses-vars': RULES.WARNING, + 'react/no-deprecated': RULES.WARNING, + 'react/no-did-update-set-state': RULES.ERROR, + 'react/no-direct-mutation-state': RULES.ERROR, + 'react/no-is-mounted': RULES.WARNING, + 'react/no-multi-comp': [RULES.WARNING, {ignoreStateless: true}], + 'react/no-unused-prop-types': RULES.WARNING, + 'react/no-unknown-property': RULES.ERROR, + 'react/prop-types': RULES.ERROR, + 'react/react-in-jsx-scope': RULES.OFF, + 'react/require-render-return': RULES.WARNING, + 'react/no-unstable-nested-components': RULES.WARNING +} + +const TESTING_RULES = { + 'chai-friendly/no-unused-expressions': [RULES.ERROR, {allowShortCircuit: true, allowTernary: true}], + 'no-only-tests/no-only-tests': RULES.ERROR +} + +const JEST_TESTING_RULES = { + 'react/display-name': RULES.OFF, + 'jest/consistent-test-it': RULES.OFF, + 'jest/expect-expect': RULES.OFF, + 'jest/max-expects': RULES.OFF, + 'jest/max-nested-describe': RULES.ERROR, + 'jest/no-alias-methods': RULES.OFF, + 'jest/no-commented-out-tests': RULES.WARNING, + 'jest/no-conditional-expect': RULES.ERROR, + 'jest/no-conditional-in-test': RULES.ERROR, + 'jest/no-deprecated-functions': RULES.ERROR, + 'jest/no-disabled-tests': RULES.WARNING, + 'jest/no-done-callback': RULES.ERROR, + 'jest/no-duplicate-hooks': RULES.OFF, + 'jest/no-export': RULES.ERROR, + 'jest/no-focused-tests': RULES.ERROR, + 'jest/no-hooks': RULES.OFF, + 'jest/no-identical-title': RULES.ERROR, + 'jest/no-if': RULES.ERROR, + 'jest/no-interpolation-in-snapshots': RULES.ERROR, + 'jest/no-jasmine-globals': RULES.OFF, + 'jest/no-large-snapshots': [RULES.WARNING, {maxSize: 300}], + 'jest/no-mocks-import': RULES.ERROR, + 'jest/no-restricted-matchers': RULES.OFF, + 'jest/no-standalone-expect': RULES.OFF, + 'jest/no-test-prefixes': RULES.ERROR, + 'jest/no-test-return-statement': RULES.OFF, + 'jest/prefer-called-with': RULES.ERROR, + 'jest/prefer-comparison-matcher': RULES.ERROR, + 'jest/prefer-each': RULES.ERROR, + 'jest/prefer-equality-matcher': RULES.ERROR, + 'jest/prefer-expect-assertions': RULES.OFF, + 'jest/prefer-expect-resolves': RULES.OFF, + 'jest/prefer-hooks-in-order': RULES.ERROR, + 'jest/prefer-hooks-on-top': RULES.ERROR, + 'jest/prefer-lowercase-title': RULES.OFF, + 'jest/prefer-mock-promise-shorthand': RULES.ERROR, + 'jest/prefer-snapshot-hint': RULES.ERROR, + 'jest/prefer-spy-on': RULES.OFF, + 'jest/prefer-strict-equal': RULES.OFF, + 'jest/prefer-to-be': RULES.OFF, + 'jest/prefer-to-contain': RULES.WARNING, + 'jest/prefer-to-have-length': RULES.WARNING, + 'jest/prefer-todo': RULES.WARNING, + 'jest/require-hook': RULES.OFF, + 'jest/require-to-throw-message': RULES.OFF, + 'jest/require-top-level-describe': RULES.OFF, + 'jest/unbound-method': RULES.OFF, + 'jest/valid-describe-callback': RULES.ERROR, + 'jest/valid-expect': RULES.ERROR, + 'jest/valid-expect-in-promise': RULES.ERROR, + 'jest/valid-title': RULES.WARNING, + 'jest-dom/prefer-checked': RULES.ERROR, + 'jest-dom/prefer-empty': RULES.ERROR, + 'jest-dom/prefer-enabled-disabled': RULES.ERROR, + 'jest-dom/prefer-focus': RULES.ERROR, + 'jest-dom/prefer-in-document': RULES.ERROR, + 'jest-dom/prefer-required': RULES.ERROR, + 'jest-dom/prefer-to-have-attribute': RULES.ERROR, + 'jest-dom/prefer-to-have-class': RULES.ERROR, + 'jest-dom/prefer-to-have-style': RULES.ERROR, + 'jest-dom/prefer-to-have-text-content': RULES.ERROR, + 'jest-dom/prefer-to-have-value': RULES.ERROR +} + +const IMPORT_SORT_GROUPS = [ + // Side effect and polyfill imports. + ['^\\u0000'], + // Built-in node dependencies + [ + '^(assert|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|https|module|net|os|path|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|tty|url|util|vm|zlib|freelist|v8|process|async_hooks|http2|perf_hooks)(/.*|$)' + ], + // Packages. `react` related packages come first. + ['^react'], + // Standalone packages. + ['^\\w'], + // Generic organization packages. + ['^@'], + // S-UI & ADV-UI organization packages. + ['^@s-ui', '^@adv-ui'], + // Relative imports. Put `./` last. + ['^\\.\\.(?!/?$)', '^\\.\\./?$', '^\\./(?=.*/)(?!/?$)', '^\\.(?!/?$)', '^\\./?$'], + // Style imports. + ['^.+\\.s?css$'] +] + +let resolvedBabelPresetSui +try { + resolvedBabelPresetSui = require.resolve('babel-preset-sui') +} catch {} + +const parser = resolvedBabelPresetSui ? '@babel/eslint-parser' : undefined + +module.exports = { + parser, + + env: { + es6: true, + mocha: true, + 'jest/globals': true + }, + + globals: { + 'cypress/globals': true, + preval: 'readonly' + }, + + parserOptions: { + ecmaFeatures: { + jsx: true + }, + ecmaVersion: 12, + babelOptions: { + configFile: resolvedBabelPresetSui + } + }, + + settings: { + react: { + version: 'detect' + } + }, + + extends: ['standard', 'plugin:cypress/recommended', 'prettier'], + + plugins: [ + '@babel', + 'chai-friendly', + 'no-only-tests', + 'prettier', + 'react', + 'react-hooks', + 'simple-import-sort', + 'jest', + 'jest-dom', + 'sui' + ], + rules: { + ...REACT_RULES, + ...TESTING_RULES, + '@babel/no-unused-expressions': RULES.OFF, + 'accessor-pairs': RULES.OFF, + 'array-callback-return': RULES.WARNING, + 'import/no-webpack-loader-syntax': RULES.WARNING, + 'import/extensions': [RULES.WARNING, 'always', {ignorePackages: true}], + 'n/no-path-concat': RULES.WARNING, + 'no-console': RULES.WARNING, + 'no-debugger': RULES.ERROR, + 'no-nested-ternary': RULES.WARNING, + 'no-prototype-builtins': RULES.OFF, + 'no-return-await': RULES.WARNING, + 'no-unused-expressions': RULES.OFF, + 'no-unused-vars': [RULES.ERROR, {args: 'none', ignoreRestSiblings: true, varsIgnorePattern: 'React'}], + 'no-var': RULES.WARNING, + strict: RULES.OFF, + 'prefer-regex-literals': RULES.WARNING, + 'prettier/prettier': [RULES.ERROR, prettierOptions], + 'simple-import-sort/imports': [RULES.WARNING, {groups: IMPORT_SORT_GROUPS}], + 'react/jsx-no-bind': RULES.OFF, + 'sui/factory-pattern': RULES.WARNING, + 'sui/serialize-deserialize': RULES.WARNING + }, + overrides: [ + { + files: ['**/*.+(ts|tsx)'], + extends: ['standard-with-typescript', 'standard-react', 'prettier'], + parser: '@typescript-eslint/parser', + parserOptions: { + project: './tsconfig.json' + }, + rules: { + 'import/extensions': RULES.OFF, + 'no-return-await': RULES.OFF, + 'prettier/prettier': [RULES.ERROR, prettierOptions], + 'react/react-in-jsx-scope': RULES.OFF, + 'react/no-unused-prop-types': RULES.OFF, + '@typescript-eslint/explicit-function-return-type': [RULES.OFF, {allowTypedFunctionExpressions: false}], + 'chai-friendly/no-unused-expressions': RULES.ERROR, + '@typescript-eslint/no-unused-expressions': RULES.OFF, + '@typescript-eslint/return-await': RULES.OFF + } + }, + { + files: ['**/__tests__/**/*.js'], + rules: JEST_TESTING_RULES + } + ] +} diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index 32926f790..9f4181b1e 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -39,8 +39,8 @@ "eslint-plugin-react": "7.30.1", "eslint-plugin-react-hooks": "4.6.0", "eslint-plugin-simple-import-sort": "7.0.0", - "eslint-plugin-sui": "../eslint-plugin-sui", - "lint-repository-sui": "../lint-repository-sui", + "eslint-plugin-sui": "beta", + "lint-repository-sui": "beta", "postcss-scss": "4.0.4", "prettier": "2.7.1", "stylelint": "14.11.0", From 48b4ef65b0d7d24adf934f9ca5ec54357f170047 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Fri, 8 Mar 2024 13:50:05 +0100 Subject: [PATCH 046/103] feat(packages/sui-lint): Add flag to force lint all files --- packages/sui-lint/bin/sui-lint-js.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index 55b884dc2..a5cce7a1c 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -13,13 +13,14 @@ const config = fs.existsSync(process.cwd() + '/tsconfig.json') ? require('../esl program .option('--add-fixes') .option('--staged') + .option('--force-full-lint', 'force to lint all the JS files') .option('--fix', 'fix automatically problems with js files') .option('--ignore-patterns ', 'Path patterns to ignore for linting') .option('--reporter ', 'Send results using a custom reporter') .option('--pattern ', 'Pattern of files to lint') .parse(process.argv) -const {addFixes, fix, ignorePatterns = [], staged, pattern, reporter} = program.opts() +const {addFixes, fix, ignorePatterns = [], staged, pattern, reporter, forceFullLint} = program.opts() const {CI} = process.env const EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] @@ -53,7 +54,11 @@ const baseConfig = { useEslintrc: false }) - const results = await eslint.lintFiles(files) + if (forceFullLint) { + console.log('[sui-lint] force to lint all our JS files') + } + + const results = await eslint.lintFiles(!forceFullLint ? files : DEFAULT_PATTERN) if (reporter) { console.log('[sui-lint] Sending stats using the reporter ', reporter) From 3865416ff0553fb36593a9cb138bd15c4cc29775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Leo=CC=81n?= Date: Fri, 8 Mar 2024 13:51:07 +0100 Subject: [PATCH 047/103] feat(packages/eslint-plugin-sui): fix more than one line in toJson --- packages/eslint-plugin-sui/package.json | 2 +- .../src/rules/serialize-deserialize.js | 3 +- .../test/server/serialize-deserialize.js | 28 +++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index 0bfb0bedb..d33ba7133 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-sui", - "version": "0.0.0", + "version": "1.0.0-beta.9", "description": "Set of sui lint rules", "keywords": [ "eslint", diff --git a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js index c8faadbf8..9d1bf68a9 100644 --- a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js +++ b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js @@ -67,7 +67,8 @@ module.exports = { } const createProperties = createParams.properties - const toJSONProperties = toJSON.value.body.body[0].argument.properties + const toJSONProperties = toJSON.value.body.body?.find(node => node.type === 'ReturnStatement')?.argument + ?.properties const spreadElement = toJSONProperties?.find(node => node.type === 'SpreadElement') if(spreadElement) { diff --git a/packages/eslint-plugin-sui/test/server/serialize-deserialize.js b/packages/eslint-plugin-sui/test/server/serialize-deserialize.js index ea8ea49b5..6ff0e8dbb 100644 --- a/packages/eslint-plugin-sui/test/server/serialize-deserialize.js +++ b/packages/eslint-plugin-sui/test/server/serialize-deserialize.js @@ -20,6 +20,26 @@ ruleTester.run('serialize-deserialize', rule, { this.name = name } toJSON() { + return { + id: this.id, + name: this.name + } + } + } + ` + }, + { + code: dedent` + class User { + static create({id, name}) { return new User(id, name) } + constructor(id, name) { + this.id = id + this.name = name + } + toJSON() { + const name="John" + const surname="Doe" + return { id: this.id, name: this.name @@ -108,10 +128,14 @@ ruleTester.run('serialize-deserialize', rule, { } } `, - errors: [{message: dedent` + errors: [ + { + message: dedent` Spread operation are not allowed as part of the toJSON function. The output of the 'toJSON' should be the same as the input of your 'static create' method - `}] + ` + } + ] } ] }) From 75d31c62190d715a9281f3be7e461251e877a440 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 14 Mar 2024 10:38:28 +0100 Subject: [PATCH 048/103] feat(packages/sui-lint): add new dependency --- packages/sui-lint/package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index 9f4181b1e..fa6ab1976 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -21,11 +21,10 @@ "@typescript-eslint/eslint-plugin": "5.62.0", "commander": "8.3.0", "console-table-printer": "2.12.0", - "js-yaml": "4.1.0", - "fast-glob": "3.3.2", "eslint": "8.56.0", "eslint-config-prettier": "8.5.0", "eslint-config-standard": "17.0.0", + "eslint-config-standard-react": "^13.0.0", "eslint-config-standard-with-typescript": "22.0.0", "eslint-plugin-chai-friendly": "0.7.2", "eslint-plugin-cypress": "2.12.1", @@ -40,6 +39,8 @@ "eslint-plugin-react-hooks": "4.6.0", "eslint-plugin-simple-import-sort": "7.0.0", "eslint-plugin-sui": "beta", + "fast-glob": "3.3.2", + "js-yaml": "4.1.0", "lint-repository-sui": "beta", "postcss-scss": "4.0.4", "prettier": "2.7.1", From 0324821fc9dc0d57dbdc78db86d587439650f85e Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 14 Mar 2024 11:09:16 +0100 Subject: [PATCH 049/103] feat(packages/sui-lint): add new dependency --- packages/lint-repository-sui/test/server/node-versionSpec.js | 2 -- packages/sui-lint/bin/sui-lint-js.js | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/lint-repository-sui/test/server/node-versionSpec.js b/packages/lint-repository-sui/test/server/node-versionSpec.js index 7314dd4e0..daf20325d 100644 --- a/packages/lint-repository-sui/test/server/node-versionSpec.js +++ b/packages/lint-repository-sui/test/server/node-versionSpec.js @@ -3,8 +3,6 @@ import dedent from 'dedent' import handler from '../../src/rules/node-version.js' import {MatchStub, RuleTester} from '../TestHelpers.js' - - RuleTester.create('node-version', handler).run({ valid: [ { diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index a5cce7a1c..7d2c6735c 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -3,7 +3,7 @@ // @ts-check const path = require('path') -const fs = require("fs") +const fs = require('fs') const program = require('commander') const {checkFilesToLint, getFilesToLint, getGitIgnoredFiles, stageFilesIfRequired} = require('../src/helpers.js') From 27c6e29eed1b33f2fb5a06cff53413717a88a856 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 14 Mar 2024 11:14:49 +0100 Subject: [PATCH 050/103] feat(packages/sui-lint): add new dependency --- .../eslint-plugin-sui/src/rules/serialize-deserialize.js | 2 +- packages/eslint-plugin-sui/test/server/factory-pattern.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js index 9d1bf68a9..33c6c2232 100644 --- a/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js +++ b/packages/eslint-plugin-sui/src/rules/serialize-deserialize.js @@ -71,7 +71,7 @@ module.exports = { ?.properties const spreadElement = toJSONProperties?.find(node => node.type === 'SpreadElement') - if(spreadElement) { + if (spreadElement) { return context.report({ node: spreadElement, messageId: 'forbiddenSpreadElements' diff --git a/packages/eslint-plugin-sui/test/server/factory-pattern.js b/packages/eslint-plugin-sui/test/server/factory-pattern.js index 2adf1d6f6..cbde9a821 100644 --- a/packages/eslint-plugin-sui/test/server/factory-pattern.js +++ b/packages/eslint-plugin-sui/test/server/factory-pattern.js @@ -32,7 +32,7 @@ ruleTester.run('factory-pattern', rule, { 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 - `, + ` } ] }, @@ -50,7 +50,7 @@ ruleTester.run('factory-pattern', rule, { 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 - `, + ` } ] }, @@ -68,7 +68,7 @@ ruleTester.run('factory-pattern', rule, { 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 - `, + ` } ] }, @@ -82,7 +82,7 @@ ruleTester.run('factory-pattern', rule, { 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 - `, + ` } ] } From b66fd7dfae0c3eeeecd6f1d034259defddd24090 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 14 Mar 2024 11:20:55 +0100 Subject: [PATCH 051/103] feat(packages/sui-lint): disable sui checks --- packages/sui-lint/eslintrc.js.js | 10 +++++----- packages/sui-lint/eslintrc.ts.js | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/sui-lint/eslintrc.js.js b/packages/sui-lint/eslintrc.js.js index 5eeb20833..3681f04a3 100644 --- a/packages/sui-lint/eslintrc.js.js +++ b/packages/sui-lint/eslintrc.js.js @@ -175,8 +175,8 @@ module.exports = { 'react-hooks', 'simple-import-sort', 'jest', - 'jest-dom', - 'sui' + 'jest-dom' + // 'sui' ], rules: { ...REACT_RULES, @@ -199,9 +199,9 @@ module.exports = { 'prefer-regex-literals': RULES.WARNING, 'prettier/prettier': [RULES.ERROR, prettierOptions], 'simple-import-sort/imports': [RULES.WARNING, {groups: IMPORT_SORT_GROUPS}], - 'react/jsx-no-bind': RULES.OFF, - 'sui/factory-pattern': RULES.WARNING, - 'sui/serialize-deserialize': RULES.WARNING + 'react/jsx-no-bind': RULES.OFF + // 'sui/factory-pattern': RULES.WARNING, + // 'sui/serialize-deserialize': RULES.WARNING }, overrides: [ { diff --git a/packages/sui-lint/eslintrc.ts.js b/packages/sui-lint/eslintrc.ts.js index 9f4b2658e..985ac0dda 100644 --- a/packages/sui-lint/eslintrc.ts.js +++ b/packages/sui-lint/eslintrc.ts.js @@ -175,8 +175,8 @@ module.exports = { 'react-hooks', 'simple-import-sort', 'jest', - 'jest-dom', - 'sui' + 'jest-dom' + // 'sui' ], rules: { ...REACT_RULES, @@ -199,9 +199,9 @@ module.exports = { 'prefer-regex-literals': RULES.WARNING, 'prettier/prettier': [RULES.ERROR, prettierOptions], 'simple-import-sort/imports': [RULES.WARNING, {groups: IMPORT_SORT_GROUPS}], - 'react/jsx-no-bind': RULES.OFF, - 'sui/factory-pattern': RULES.WARNING, - 'sui/serialize-deserialize': RULES.WARNING + 'react/jsx-no-bind': RULES.OFF + // 'sui/factory-pattern': RULES.WARNING, + // 'sui/serialize-deserialize': RULES.WARNING }, overrides: [ { From 57e55cfeba74087b2da535cadbe85e07190cdf8d Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 14 Mar 2024 11:42:29 +0100 Subject: [PATCH 052/103] feat(packages/sui-lint): disable sui checks --- package.json | 2 +- packages/sui-lint/bin/sui-lint-js.js | 2 +- packages/sui-lint/{eslintrc.js.js => eslintrc.js} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename packages/sui-lint/{eslintrc.js.js => eslintrc.js} (100%) diff --git a/package.json b/package.json index f0951fa56..e41587415 100644 --- a/package.json +++ b/package.json @@ -79,4 +79,4 @@ "stylelint": { "extends": "./node_modules/@s-ui/lint/stylelint.config.js" } -} \ No newline at end of file +} diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index 7d2c6735c..cdef41c06 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -8,7 +8,7 @@ const program = require('commander') const {checkFilesToLint, getFilesToLint, getGitIgnoredFiles, stageFilesIfRequired} = require('../src/helpers.js') const {ESLint} = require('eslint') -const config = fs.existsSync(process.cwd() + '/tsconfig.json') ? require('../eslintrc.ts') : require('../eslintrc.js') +const config = fs.existsSync(process.cwd() + '/tsconfig.json') ? require('../eslintrc.ts.js') : require('../eslintrc.js') program .option('--add-fixes') diff --git a/packages/sui-lint/eslintrc.js.js b/packages/sui-lint/eslintrc.js similarity index 100% rename from packages/sui-lint/eslintrc.js.js rename to packages/sui-lint/eslintrc.js From d2c4be0dd28a648edb28a59f77d4cf1f1cf04ccd Mon Sep 17 00:00:00 2001 From: "jordi.munoz@adevinta.com" Date: Fri, 15 Mar 2024 13:36:40 +0100 Subject: [PATCH 053/103] chore(packages/sui-bundler): add hydration overlay and webpack inject attributes plugin deps --- packages/sui-bundler/package.json | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/sui-bundler/package.json b/packages/sui-bundler/package.json index 3b3aac52a..d952b1484 100644 --- a/packages/sui-bundler/package.json +++ b/packages/sui-bundler/package.json @@ -22,6 +22,8 @@ "homepage": "https://github.com/SUI-Components/sui/tree/master/packages/sui-bundler#readme", "dependencies": { "@babel/core": "7.18.10", + "@builder.io/react-hydration-overlay": "0.0.8", + "@pmmmwh/react-refresh-webpack-plugin": "0.5.10", "@s-ui/helpers": "1", "@s-ui/sass-loader": "1", "address": "1.2.2", @@ -36,22 +38,22 @@ "escape-string-regexp": "4.0.0", "fast-glob": "3.2.11", "find-free-ports": "3.0.0", + "html-webpack-inject-attributes-plugin": "1.0.6", "html-webpack-plugin": "5.5.0", "https-browserify": "1.0.0", "mini-css-extract-plugin": "2.7.7", - "postcss": "8.4.31", "postcss-loader": "7.3.4", + "postcss": "8.4.31", "process": "0.11.10", + "react-refresh": "0.14.0", "sass": "1.54.5", "stream-http": "3.2.0", "strip-ansi": "6.0.1", "style-loader": "3.3.1", "url": "0.11.0", - "webpack": "5.82.1", "webpack-dev-server": "4.10.0", "webpack-manifest-plugin": "5.0.0", "webpack-node-externals": "3.0.0", - "@pmmmwh/react-refresh-webpack-plugin": "0.5.10", - "react-refresh": "0.14.0" + "webpack": "5.82.1" } } From f69fb9f60f45efc7fde32e9a46c6f172b5b6cbb2 Mon Sep 17 00:00:00 2001 From: "jordi.munoz@adevinta.com" Date: Fri, 15 Mar 2024 13:38:11 +0100 Subject: [PATCH 054/103] feat(packages/sui-bundler): add hydration overlay on webpack.config.client.dev.js --- packages/sui-bundler/webpack.config.client.dev.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/sui-bundler/webpack.config.client.dev.js b/packages/sui-bundler/webpack.config.client.dev.js index cd23dbe80..3f57be464 100644 --- a/packages/sui-bundler/webpack.config.client.dev.js +++ b/packages/sui-bundler/webpack.config.client.dev.js @@ -6,6 +6,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin') const {WebpackManifestPlugin} = require('webpack-manifest-plugin') const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') +const {withHydrationOverlayWebpack} = require('@builder.io/react-hydration-overlay/webpack') const {envVars, MAIN_ENTRY_POINT, config, cleanList, when} = require('./shared/index.js') const definePlugin = require('./shared/define.js') @@ -122,4 +123,7 @@ const webpackConfig = { devtool: config.sourcemaps && config.sourcemaps.dev ? config.sourcemaps.dev : false } -module.exports = webpackConfig +module.exports = withHydrationOverlayWebpack({ + appRootSelector: '#root', + isMainAppEntryPoint: entryPointName => entryPointName === 'app' +})(webpackConfig) From bfc20371dec312708222e22b7a44830a736a0732 Mon Sep 17 00:00:00 2001 From: "jordi.munoz@adevinta.com" Date: Fri, 15 Mar 2024 13:39:33 +0100 Subject: [PATCH 055/103] feat(packages/sui-bundler): add crossorigin=anonymous attribute to script tags on client dev --- packages/sui-bundler/webpack.config.client.dev.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/sui-bundler/webpack.config.client.dev.js b/packages/sui-bundler/webpack.config.client.dev.js index 3f57be464..5b7681975 100644 --- a/packages/sui-bundler/webpack.config.client.dev.js +++ b/packages/sui-bundler/webpack.config.client.dev.js @@ -6,6 +6,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin') const {WebpackManifestPlugin} = require('webpack-manifest-plugin') const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin') const MiniCssExtractPlugin = require('mini-css-extract-plugin') +const HtmlWebpackInjectAttributesPlugin = require('html-webpack-inject-attributes-plugin') const {withHydrationOverlayWebpack} = require('@builder.io/react-hydration-overlay/webpack') const {envVars, MAIN_ENTRY_POINT, config, cleanList, when} = require('./shared/index.js') @@ -84,7 +85,10 @@ const webpackConfig = { }), new WebpackManifestPlugin({fileName: 'asset-manifest.json'}), new webpack.HotModuleReplacementPlugin(), - new ReactRefreshWebpackPlugin({overlay: false}) + new ReactRefreshWebpackPlugin({overlay: false}), + new HtmlWebpackInjectAttributesPlugin({ + crossorigin: 'anonymous' + }) ], resolveLoader, module: { From c071d2a7aa6f6f1a5e8f89c8d808547f5a21c0b3 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Mon, 18 Mar 2024 13:48:40 +0000 Subject: [PATCH 056/103] release(packages/sui-bundler): v9.57.0 [skip ci] --- packages/sui-bundler/CHANGELOG.md | 10 ++++++++++ packages/sui-bundler/package.json | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/sui-bundler/CHANGELOG.md b/packages/sui-bundler/CHANGELOG.md index c480da741..d745c76a8 100644 --- a/packages/sui-bundler/CHANGELOG.md +++ b/packages/sui-bundler/CHANGELOG.md @@ -1,5 +1,15 @@ # CHANGELOG +# 9.57.0 (2024-03-18) + + +### Features + +* add crossorigin=anonymous attribute to script tags on client dev ([bfc2037](https://github.com/SUI-Components/sui/commit/bfc20371dec312708222e22b7a44830a736a0732)) +* add hydration overlay on webpack.config.client.dev.js ([f69fb9f](https://github.com/SUI-Components/sui/commit/f69fb9f60f45efc7fde32e9a46c6f172b5b6cbb2)) + + + # 9.56.0 (2024-02-07) diff --git a/packages/sui-bundler/package.json b/packages/sui-bundler/package.json index d952b1484..45eb8fd6d 100644 --- a/packages/sui-bundler/package.json +++ b/packages/sui-bundler/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/bundler", - "version": "9.56.0", + "version": "9.57.0", "description": "Config-free bundler for ES6 React apps.", "bin": { "sui-bundler": "./bin/sui-bundler.js" From 958cfc6b858eae5cfea94bc51f32fb6806531675 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 21 Mar 2024 12:18:48 +0100 Subject: [PATCH 057/103] feat(packages/sui-lint): fix lint --- packages/sui-lint/bin/sui-lint-js.js | 4 +++- packages/sui-lint/eslintrc.js | 11 ++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/sui-lint/bin/sui-lint-js.js b/packages/sui-lint/bin/sui-lint-js.js index cdef41c06..68bd23484 100755 --- a/packages/sui-lint/bin/sui-lint-js.js +++ b/packages/sui-lint/bin/sui-lint-js.js @@ -8,7 +8,9 @@ const program = require('commander') const {checkFilesToLint, getFilesToLint, getGitIgnoredFiles, stageFilesIfRequired} = require('../src/helpers.js') const {ESLint} = require('eslint') -const config = fs.existsSync(process.cwd() + '/tsconfig.json') ? require('../eslintrc.ts.js') : require('../eslintrc.js') +const config = fs.existsSync(process.cwd() + '/tsconfig.json') + ? require('../eslintrc.ts.js') + : require('../eslintrc.js') program .option('--add-fixes') diff --git a/packages/sui-lint/eslintrc.js b/packages/sui-lint/eslintrc.js index 3681f04a3..02ee6aa3c 100644 --- a/packages/sui-lint/eslintrc.js +++ b/packages/sui-lint/eslintrc.js @@ -176,7 +176,6 @@ module.exports = { 'simple-import-sort', 'jest', 'jest-dom' - // 'sui' ], rules: { ...REACT_RULES, @@ -200,10 +199,16 @@ module.exports = { 'prettier/prettier': [RULES.ERROR, prettierOptions], 'simple-import-sort/imports': [RULES.WARNING, {groups: IMPORT_SORT_GROUPS}], 'react/jsx-no-bind': RULES.OFF - // 'sui/factory-pattern': RULES.WARNING, - // 'sui/serialize-deserialize': RULES.WARNING }, overrides: [ + // { + // files: ['**/domain/src/**'], + // plugins: ['sui'], + // rules: { + // 'sui/factory-pattern': RULES.WARNING, + // 'sui/serialize-deserialize': RULES.WARNING + // } + // }, { files: ['**/*.+(ts|tsx)'], extends: ['standard-with-typescript'], From 19dfdd6f7cec7ff50f246864f3fe1e4e05a2d8d4 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 21 Mar 2024 12:37:55 +0100 Subject: [PATCH 058/103] chore(packages/eslint-plugin-sui): add npx --- packages/eslint-plugin-sui/package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index d33ba7133..e6f9ccb1b 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -11,11 +11,11 @@ "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 .", + "lint": "npx npm-run-all \"lint:*\"", + "lint:eslint-docs": "npx npm-run-all \"update:eslint-docs -- --check\"", + "lint:js": "npx eslint .", "test": "npx sui-test server", - "update:eslint-docs": "eslint-doc-generator" + "update:eslint-docs": "npx eslint-doc-generator" }, "dependencies": { "requireindex": "1.2.0", From 956d039e3066c7f75d71fa9e9b93b30ab20521be Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 21 Mar 2024 11:55:58 +0000 Subject: [PATCH 059/103] release(packages/eslint-plugin-sui): v1.0.0 [skip ci] --- packages/eslint-plugin-sui/CHANGELOG.md | 16 ++++++++++++++++ packages/eslint-plugin-sui/package.json | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 packages/eslint-plugin-sui/CHANGELOG.md diff --git a/packages/eslint-plugin-sui/CHANGELOG.md b/packages/eslint-plugin-sui/CHANGELOG.md new file mode 100644 index 000000000..561eac100 --- /dev/null +++ b/packages/eslint-plugin-sui/CHANGELOG.md @@ -0,0 +1,16 @@ +# CHANGELOG + +# 1.0.0 (2024-03-21) + + +### Features + +* add empty rule ([e2cef86](https://github.com/SUI-Components/sui/commit/e2cef862c94d6c0e8d8ea3f67df5e33a1dbaa6ae)) +* add new dependency ([27c6e29](https://github.com/SUI-Components/sui/commit/27c6e29eed1b33f2fb5a06cff53413717a88a856)) +* create few rules ([7e41dc7](https://github.com/SUI-Components/sui/commit/7e41dc7323cbe9dc782d3eb78a90fef651b72e19)) +* fix more than one line in toJson ([3865416](https://github.com/SUI-Components/sui/commit/3865416ff0553fb36593a9cb138bd15c4cc29775)) +* Improve rules to be more resilient ([369b096](https://github.com/SUI-Components/sui/commit/369b096faf6ff77791d41eac9b136ff68ff814b8)) +* make warning ([c6ffa98](https://github.com/SUI-Components/sui/commit/c6ffa98cea6b262be5d203eaa91de5b82444aa46)) + + + diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index e6f9ccb1b..608cbdcca 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-sui", - "version": "1.0.0-beta.9", + "version": "1.0.0", "description": "Set of sui lint rules", "keywords": [ "eslint", From c77f8a0a2077ffd8a244a9b23e2f0bcc922a8fd6 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 21 Mar 2024 11:56:02 +0000 Subject: [PATCH 060/103] release(packages/lint-repository-sui): v0.1.0 [skip ci] --- packages/lint-repository-sui/CHANGELOG.md | 20 ++++++++++++++++++++ packages/lint-repository-sui/package.json | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 packages/lint-repository-sui/CHANGELOG.md diff --git a/packages/lint-repository-sui/CHANGELOG.md b/packages/lint-repository-sui/CHANGELOG.md new file mode 100644 index 000000000..fc812bbb5 --- /dev/null +++ b/packages/lint-repository-sui/CHANGELOG.md @@ -0,0 +1,20 @@ +# CHANGELOG + +# 0.1.0 (2024-03-21) + + +### Bug Fixes + +* Fix typo in rule ([36a3830](https://github.com/SUI-Components/sui/commit/36a3830f0d0056d0dbc01dbd526ce8261fdb723d)) + + +### Features + +* add new dependency ([0324821](https://github.com/SUI-Components/sui/commit/0324821fc9dc0d57dbdc78db86d587439650f85e)) +* add typescript lint repository rule ([ab16dc5](https://github.com/SUI-Components/sui/commit/ab16dc574dd55c05ba032fd6db2b2b327cb00767)) +* Avoid magic strings ([9d51b66](https://github.com/SUI-Components/sui/commit/9d51b66cf99ecbfefa8861920a3ec23a0446cb22)) +* Create first version of the Golden Path Rules ([836098d](https://github.com/SUI-Components/sui/commit/836098d30de4fe2f5420530bd10902d2d2d278cb)) +* Use only the major version of Node ([164ddf2](https://github.com/SUI-Components/sui/commit/164ddf24f3bf05010c8f125f2e97f25031667a6f)) + + + diff --git a/packages/lint-repository-sui/package.json b/packages/lint-repository-sui/package.json index 8bbd986c7..35e2c5e93 100644 --- a/packages/lint-repository-sui/package.json +++ b/packages/lint-repository-sui/package.json @@ -1,6 +1,6 @@ { "name": "lint-repository-sui", - "version": "0.0.0", + "version": "0.1.0", "description": "Set of sui lint rules to lint a repository", "keywords": [], "author": "Sui", From e849656c468fbecd5613ac2de0a573983b5eb52b Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 21 Mar 2024 11:56:06 +0000 Subject: [PATCH 061/103] release(packages/sui-lint): v4.37.0 [skip ci] --- packages/sui-lint/CHANGELOG.md | 33 +++++++++++++++++++++++++++++---- packages/sui-lint/package.json | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/sui-lint/CHANGELOG.md b/packages/sui-lint/CHANGELOG.md index 1d655d448..b4ef11ef6 100644 --- a/packages/sui-lint/CHANGELOG.md +++ b/packages/sui-lint/CHANGELOG.md @@ -1,5 +1,33 @@ # CHANGELOG +# 4.37.0 (2024-03-21) + + +### Bug Fixes + +* Fix JSReporter import and plugins version ([07293da](https://github.com/SUI-Components/sui/commit/07293da8db0823911b3a6f7518d5de4fc1cbf24d)) +* Fix lint ([0836810](https://github.com/SUI-Components/sui/commit/0836810202a370e600fd1cb25250a18595aa7e81)) +* Fix lint ([e0fd013](https://github.com/SUI-Components/sui/commit/e0fd01378a1f3c381d99802d1feb827d55f0eae3)) +* Fix version ([02cd409](https://github.com/SUI-Components/sui/commit/02cd409e73ac28dc21bda52c1229adcde74fc28d)) + + +### Features + +* Add flag to force lint all files ([48b4ef6](https://github.com/SUI-Components/sui/commit/48b4ef65b0d7d24adf934f9ca5ec54357f170047)) +* add new dependency ([0324821](https://github.com/SUI-Components/sui/commit/0324821fc9dc0d57dbdc78db86d587439650f85e)) +* add new dependency ([75d31c6](https://github.com/SUI-Components/sui/commit/75d31c62190d715a9281f3be7e461251e877a440)) +* Add send method to the reporter ([7d34092](https://github.com/SUI-Components/sui/commit/7d34092f0c083bdb8daefb3c254603196839b7b2)) +* Add send method to the reporter ([1ce2585](https://github.com/SUI-Components/sui/commit/1ce2585af0a11d9dd11a21fc9cd9e7366a3122c3)) +* add typescript lint repository rule ([508fd84](https://github.com/SUI-Components/sui/commit/508fd8493180bbd6bab63e710e47fd2a6396f166)) +* Allow relative path in reporter flag and select TS or JS eslint config ([c42244c](https://github.com/SUI-Components/sui/commit/c42244c87d7e38392458751c1bfea9dbfa5e2809)) +* Create reporter flag ([0a2563e](https://github.com/SUI-Components/sui/commit/0a2563ee0809c2e348684fdf72134cdc085e6060)) +* Create RepositoryLinter and desactivate eslint-preset-sui rules ([ed4035f](https://github.com/SUI-Components/sui/commit/ed4035f8b503c9c7eea41e762115fe4e6339ee51)) +* disable sui checks ([57e55cf](https://github.com/SUI-Components/sui/commit/57e55cfeba74087b2da535cadbe85e07190cdf8d)) +* disable sui checks ([b66fd7d](https://github.com/SUI-Components/sui/commit/b66fd7dfae0c3eeeecd6f1d034259defddd24090)) +* fix lint ([958cfc6](https://github.com/SUI-Components/sui/commit/958cfc6b858eae5cfea94bc51f32fb6806531675)) + + + # 4.36.0 (2024-01-12) @@ -1089,7 +1117,4 @@ ### BREAKING CHANGES -* **sui-lint:** CLI has changed and not compatible to previous version - - - +* **sui-lint:** CLI has changed and not compatible to previous version \ No newline at end of file diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index fa6ab1976..a9eb8a005 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/lint", - "version": "4.36.0", + "version": "4.37.0", "description": "Linting CLI for sui packages", "main": "./bin/sui-lint.js", "bin": { From 1d832f4b3efc651910a9b339286e5ac9693100bc Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 21 Mar 2024 13:00:43 +0100 Subject: [PATCH 062/103] feat(packages/eslint-plugin-sui): bump version --- packages/eslint-plugin-sui/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index 608cbdcca..431103c3d 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-sui", - "version": "1.0.0", + "version": "1.1.0", "description": "Set of sui lint rules", "keywords": [ "eslint", From 7c8b1bd740eb99e3ef1530ca17e7ea77e3749063 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 21 Mar 2024 13:01:04 +0100 Subject: [PATCH 063/103] feat(packages/lint-repository-sui): bump version --- packages/lint-repository-sui/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lint-repository-sui/package.json b/packages/lint-repository-sui/package.json index 35e2c5e93..82939afd3 100644 --- a/packages/lint-repository-sui/package.json +++ b/packages/lint-repository-sui/package.json @@ -1,6 +1,6 @@ { "name": "lint-repository-sui", - "version": "0.1.0", + "version": "1.1.0", "description": "Set of sui lint rules to lint a repository", "keywords": [], "author": "Sui", From eaa25a26ac0337b6739c9b058d11942af895ae09 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 21 Mar 2024 13:01:40 +0100 Subject: [PATCH 064/103] feat(packages/sui-lint): bump version --- packages/sui-lint/package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index a9eb8a005..477965849 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/lint", - "version": "4.37.0", + "version": "4.39.0", "description": "Linting CLI for sui packages", "main": "./bin/sui-lint.js", "bin": { @@ -38,10 +38,10 @@ "eslint-plugin-react": "7.30.1", "eslint-plugin-react-hooks": "4.6.0", "eslint-plugin-simple-import-sort": "7.0.0", - "eslint-plugin-sui": "beta", + "eslint-plugin-sui": "1", "fast-glob": "3.3.2", "js-yaml": "4.1.0", - "lint-repository-sui": "beta", + "lint-repository-sui": "1", "postcss-scss": "4.0.4", "prettier": "2.7.1", "stylelint": "14.11.0", From f66023b7c228232282bed64885f75b1150b76e95 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 21 Mar 2024 12:04:21 +0000 Subject: [PATCH 065/103] release(packages/eslint-plugin-sui): v1.2.0 [skip ci] --- packages/eslint-plugin-sui/CHANGELOG.md | 14 ++++++++++---- packages/eslint-plugin-sui/package.json | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin-sui/CHANGELOG.md b/packages/eslint-plugin-sui/CHANGELOG.md index 561eac100..f19d1542a 100644 --- a/packages/eslint-plugin-sui/CHANGELOG.md +++ b/packages/eslint-plugin-sui/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 1.2.0 (2024-03-21) + + +### Features + +* bump version ([1d832f4](https://github.com/SUI-Components/sui/commit/1d832f4b3efc651910a9b339286e5ac9693100bc)) + + + # 1.0.0 (2024-03-21) @@ -10,7 +19,4 @@ * create few rules ([7e41dc7](https://github.com/SUI-Components/sui/commit/7e41dc7323cbe9dc782d3eb78a90fef651b72e19)) * fix more than one line in toJson ([3865416](https://github.com/SUI-Components/sui/commit/3865416ff0553fb36593a9cb138bd15c4cc29775)) * Improve rules to be more resilient ([369b096](https://github.com/SUI-Components/sui/commit/369b096faf6ff77791d41eac9b136ff68ff814b8)) -* make warning ([c6ffa98](https://github.com/SUI-Components/sui/commit/c6ffa98cea6b262be5d203eaa91de5b82444aa46)) - - - +* make warning ([c6ffa98](https://github.com/SUI-Components/sui/commit/c6ffa98cea6b262be5d203eaa91de5b82444aa46)) \ No newline at end of file diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index 431103c3d..7eee2a9ec 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-sui", - "version": "1.1.0", + "version": "1.2.0", "description": "Set of sui lint rules", "keywords": [ "eslint", From 1e5050086f9ecda35580b41e5553194c970ef1a2 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 21 Mar 2024 12:04:24 +0000 Subject: [PATCH 066/103] release(packages/lint-repository-sui): v1.2.0 [skip ci] --- packages/lint-repository-sui/CHANGELOG.md | 14 ++++++++++---- packages/lint-repository-sui/package.json | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/lint-repository-sui/CHANGELOG.md b/packages/lint-repository-sui/CHANGELOG.md index fc812bbb5..05da8d137 100644 --- a/packages/lint-repository-sui/CHANGELOG.md +++ b/packages/lint-repository-sui/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 1.2.0 (2024-03-21) + + +### Features + +* bump version ([7c8b1bd](https://github.com/SUI-Components/sui/commit/7c8b1bd740eb99e3ef1530ca17e7ea77e3749063)) + + + # 0.1.0 (2024-03-21) @@ -14,7 +23,4 @@ * add typescript lint repository rule ([ab16dc5](https://github.com/SUI-Components/sui/commit/ab16dc574dd55c05ba032fd6db2b2b327cb00767)) * Avoid magic strings ([9d51b66](https://github.com/SUI-Components/sui/commit/9d51b66cf99ecbfefa8861920a3ec23a0446cb22)) * Create first version of the Golden Path Rules ([836098d](https://github.com/SUI-Components/sui/commit/836098d30de4fe2f5420530bd10902d2d2d278cb)) -* Use only the major version of Node ([164ddf2](https://github.com/SUI-Components/sui/commit/164ddf24f3bf05010c8f125f2e97f25031667a6f)) - - - +* Use only the major version of Node ([164ddf2](https://github.com/SUI-Components/sui/commit/164ddf24f3bf05010c8f125f2e97f25031667a6f)) \ No newline at end of file diff --git a/packages/lint-repository-sui/package.json b/packages/lint-repository-sui/package.json index 82939afd3..8b1b23090 100644 --- a/packages/lint-repository-sui/package.json +++ b/packages/lint-repository-sui/package.json @@ -1,6 +1,6 @@ { "name": "lint-repository-sui", - "version": "1.1.0", + "version": "1.2.0", "description": "Set of sui lint rules to lint a repository", "keywords": [], "author": "Sui", From d7e882c1b21cdcd896cf0a1124a526ad658b1303 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 21 Mar 2024 12:04:28 +0000 Subject: [PATCH 067/103] release(packages/sui-lint): v4.40.0 [skip ci] --- packages/sui-lint/CHANGELOG.md | 9 +++++++++ packages/sui-lint/package.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/sui-lint/CHANGELOG.md b/packages/sui-lint/CHANGELOG.md index b4ef11ef6..94eefb9aa 100644 --- a/packages/sui-lint/CHANGELOG.md +++ b/packages/sui-lint/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 4.40.0 (2024-03-21) + + +### Features + +* bump version ([eaa25a2](https://github.com/SUI-Components/sui/commit/eaa25a26ac0337b6739c9b058d11942af895ae09)) + + + # 4.37.0 (2024-03-21) diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index 477965849..c5cbff008 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/lint", - "version": "4.39.0", + "version": "4.40.0", "description": "Linting CLI for sui packages", "main": "./bin/sui-lint.js", "bin": { From dd18e41fc448a27d09cfe7e7c255efa7297880ac Mon Sep 17 00:00:00 2001 From: "jordi.munoz@adevinta.com" Date: Thu, 21 Mar 2024 14:52:44 +0100 Subject: [PATCH 068/103] feat(packages/sui-js): using a pure js function to make this utility hashing function isomorphic --- packages/sui-js/src/hash/index.js | 150 ++++++++++++++++++++++++++++-- 1 file changed, 140 insertions(+), 10 deletions(-) diff --git a/packages/sui-js/src/hash/index.js b/packages/sui-js/src/hash/index.js index 9c2877e8a..c5276b1a8 100644 --- a/packages/sui-js/src/hash/index.js +++ b/packages/sui-js/src/hash/index.js @@ -1,14 +1,144 @@ // extracted from https://github.com/jbt/tiny-hashes/blob/master/md5/md5.js -const m = [-680876936,-389564586,606105819,-1044525330,-176418897,1200080426,-1473231341,-45705983,1770035416,-1958414417,-42063,-1990404162,1804603682,-40341101,-1502002290,1236535329,-165796510,-1069501632,643717713,-373897302,-701558691,38016083,-660478335,-405537848,568446438,-1019803690,-187363961,1163531501,-1444681467,-51403784,1735328473,-1926607734,-378558,-2022574463,1839030562,-35309556,-1530992060,1272893353,-155497632,-1094730640,681279174,-358537222,-722521979,76029189,-640364487,-421815835,530742520,-995338651,-198630844,1126891415,-1416354905,-57434055,1700485571,-1894986606,-1051523,-2054922799,1873313359,-30611744,-1560198380,1309151649,-145523070,-1120210379,718787259,-343485551] // eslint-disable-line -const md5=function(c){var e,g,f,a,h=[];c=unescape(encodeURI(c));for(var b=c.length,k=[e=1732584193,g=-271733879,~e,~g],d=0;d<=b;)h[d>>2]|=(c.charCodeAt(d)||128)<<8*(d++%4);h[c=16*(b+8>>6)+14]=8*b;for(d=0;da;)b=[f=b[3],(e=b[1]|0)+((f=b[0]+[e&(g=b[2])|~e&f,f&e|~f&g,e^g^f,g^(e|~f)][b=a>>4]+(m[a]+(h[[a,5*a+1,3*a+5,7*a][b]%16+d]|0)))<<(b=[7,12,17,22,5,9,14,20,4,11,16,23,6,10,15,21][4*b+a++%4])|f>>>32-b),e,g];for(a=4;a;)k[--a]=k[a]+b[a]}for(c="";32>a;)c+=(k[a>>3]>>4*(1^a++&7)&15).toString(16);return c} // eslint-disable-line - -// extracted from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string -const sha256 = async value => { - const msgUint8 = new TextEncoder().encode(value) - const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8) - const hashArray = Array.from(new Uint8Array(hashBuffer)) - const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('') - return hashHex +const m = [ + -680876936, -389564586, 606105819, -1044525330, -176418897, 1200080426, -1473231341, -45705983, 1770035416, + -1958414417, -42063, -1990404162, 1804603682, -40341101, -1502002290, 1236535329, -165796510, -1069501632, 643717713, + -373897302, -701558691, 38016083, -660478335, -405537848, 568446438, -1019803690, -187363961, 1163531501, -1444681467, + -51403784, 1735328473, -1926607734, -378558, -2022574463, 1839030562, -35309556, -1530992060, 1272893353, -155497632, + -1094730640, 681279174, -358537222, -722521979, 76029189, -640364487, -421815835, 530742520, -995338651, -198630844, + 1126891415, -1416354905, -57434055, 1700485571, -1894986606, -1051523, -2054922799, 1873313359, -30611744, + -1560198380, 1309151649, -145523070, -1120210379, 718787259, -343485551 +] // eslint-disable-line +const md5 = function (c) { + let e + let g + let f + let a + const h = [] + c = unescape(encodeURI(c)) + for (var b = c.length, k = [(e = 1732584193), (g = -271733879), ~e, ~g], d = 0; d <= b; ) + h[d >> 2] |= (c.charCodeAt(d) || 128) << (8 * (d++ % 4)) + h[(c = 16 * ((b + 8) >> 6) + 14)] = 8 * b + for (d = 0; d < c; d += 16) { + b = k + for (a = 0; a < 64; ) + b = [ + (f = b[3]), + (e = b[1] | 0) + + (((f = + b[0] + + [(e & (g = b[2])) | (~e & f), (f & e) | (~f & g), e ^ g ^ f, g ^ (e | ~f)][(b = a >> 4)] + + (m[a] + (h[([a, 5 * a + 1, 3 * a + 5, 7 * a][b] % 16) + d] | 0))) << + (b = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21][4 * b + (a++ % 4)])) | + (f >>> (32 - b))), + e, + g + ] + for (a = 4; a; ) k[--a] = k[a] + b[a] + } + for (c = ''; a < 32; ) c += ((k[a >> 3] >> (4 * (1 ^ (a++ & 7)))) & 15).toString(16) + return c +} // eslint-disable-line + +// extracted from https://geraintluff.github.io/sha256/ +const sha256 = async ascii => { + function rightRotate(value, amount) { + return (value >>> amount) | (value << (32 - amount)) + } + + const mathPow = Math.pow + const maxWord = mathPow(2, 32) + const lengthProperty = 'length' + let i, j // Used as a counter across the whole file + let result = '' + + const words = [] + const asciiBitLength = ascii[lengthProperty] * 8 + + //* caching results is optional - remove/add slash from front of this line to toggle + // Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes + // (we actually calculate the first 64, but extra values are just ignored) + let hash = (sha256.h = sha256.h || []) + // Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes + const k = (sha256.k = sha256.k || []) + let primeCounter = k[lengthProperty] + /* / + var hash = [], k = []; + var primeCounter = 0; + // */ + + const isComposite = {} + for (let candidate = 2; primeCounter < 64; candidate++) { + if (!isComposite[candidate]) { + for (i = 0; i < 313; i += candidate) { + isComposite[i] = candidate + } + hash[primeCounter] = (mathPow(candidate, 0.5) * maxWord) | 0 + k[primeCounter++] = (mathPow(candidate, 1 / 3) * maxWord) | 0 + } + } + + ascii += '\x80' // Append Ƈ' bit (plus zero padding) + while ((ascii[lengthProperty] % 64) - 56) ascii += '\x00' // More zero padding + for (i = 0; i < ascii[lengthProperty]; i++) { + j = ascii.charCodeAt(i) + if (j >> 8) return // ASCII check: only accept characters in range 0-255 + words[i >> 2] |= j << (((3 - i) % 4) * 8) + } + words[words[lengthProperty]] = (asciiBitLength / maxWord) | 0 + words[words[lengthProperty]] = asciiBitLength + + // process each chunk + for (j = 0; j < words[lengthProperty]; ) { + const w = words.slice(j, (j += 16)) // The message is expanded into 64 words as part of the iteration + const oldHash = hash + // This is now the undefinedworking hash", often labelled as variables a...g + // (we have to truncate as well, otherwise extra entries at the end accumulate + hash = hash.slice(0, 8) + + for (i = 0; i < 64; i++) { + // Expand the message into 64 words + // Used below if + const w15 = w[i - 15] + const w2 = w[i - 2] + + // Iterate + const a = hash[0] + const e = hash[4] + const temp1 = + hash[7] + + (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) + // S1 + ((e & hash[5]) ^ (~e & hash[6])) + // ch + k[i] + + // Expand the message schedule if needed + (w[i] = + i < 16 + ? w[i] + : (w[i - 16] + + (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15 >>> 3)) + // s0 + w[i - 7] + + (rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2 >>> 10))) | // s1 + 0) + // This is only used once, so *could* be moved below, but it only saves 4 bytes and makes things unreadble + const temp2 = + (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) + // S0 + ((a & hash[1]) ^ (a & hash[2]) ^ (hash[1] & hash[2])) // maj + + hash = [(temp1 + temp2) | 0].concat(hash) // We don't bother trimming off the extra ones, they're harmless as long as we're truncating when we do the slice() + hash[4] = (hash[4] + temp1) | 0 + } + + for (i = 0; i < 8; i++) { + hash[i] = (hash[i] + oldHash[i]) | 0 + } + } + + for (i = 0; i < 8; i++) { + for (j = 3; j + 1; j--) { + const b = (hash[i] >> (j * 8)) & 255 + result += (b < 16 ? 0 : '') + b.toString(16) + } + } + return result } export const createHash = md5 From a43f5687fbe42ac970b18a1d1ecd918605a17234 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 21 Mar 2024 15:43:15 +0100 Subject: [PATCH 069/103] feat(packages/eslint-plugin-sui): make public --- packages/eslint-plugin-sui/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index 7eee2a9ec..a43b8192a 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -1,6 +1,7 @@ { "name": "eslint-plugin-sui", "version": "1.2.0", + "access": "public", "description": "Set of sui lint rules", "keywords": [ "eslint", From b261c3bb2f24601924757cc594a23bea5cee074a Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Thu, 21 Mar 2024 15:43:48 +0100 Subject: [PATCH 070/103] feat(packages/lint-repository-sui): make public --- packages/lint-repository-sui/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/lint-repository-sui/package.json b/packages/lint-repository-sui/package.json index 8b1b23090..8e34b53dd 100644 --- a/packages/lint-repository-sui/package.json +++ b/packages/lint-repository-sui/package.json @@ -1,5 +1,6 @@ { "name": "lint-repository-sui", + "access": "public", "version": "1.2.0", "description": "Set of sui lint rules to lint a repository", "keywords": [], From aaf4851add1f58b2b36daf9f138b419fa192e603 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 21 Mar 2024 14:46:33 +0000 Subject: [PATCH 071/103] release(packages/eslint-plugin-sui): v1.3.0 [skip ci] --- packages/eslint-plugin-sui/CHANGELOG.md | 9 +++++++++ packages/eslint-plugin-sui/package.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin-sui/CHANGELOG.md b/packages/eslint-plugin-sui/CHANGELOG.md index f19d1542a..17d7c7508 100644 --- a/packages/eslint-plugin-sui/CHANGELOG.md +++ b/packages/eslint-plugin-sui/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 1.3.0 (2024-03-21) + + +### Features + +* make public ([a43f568](https://github.com/SUI-Components/sui/commit/a43f5687fbe42ac970b18a1d1ecd918605a17234)) + + + # 1.2.0 (2024-03-21) diff --git a/packages/eslint-plugin-sui/package.json b/packages/eslint-plugin-sui/package.json index a43b8192a..bfb860bd7 100644 --- a/packages/eslint-plugin-sui/package.json +++ b/packages/eslint-plugin-sui/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-sui", - "version": "1.2.0", + "version": "1.3.0", "access": "public", "description": "Set of sui lint rules", "keywords": [ From ee4e1f51b3f4871f14dfed6db443b8f172533bdb Mon Sep 17 00:00:00 2001 From: sui-bot Date: Thu, 21 Mar 2024 14:46:36 +0000 Subject: [PATCH 072/103] release(packages/lint-repository-sui): v1.3.0 [skip ci] --- packages/lint-repository-sui/CHANGELOG.md | 9 +++++++++ packages/lint-repository-sui/package.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/lint-repository-sui/CHANGELOG.md b/packages/lint-repository-sui/CHANGELOG.md index 05da8d137..1db18171d 100644 --- a/packages/lint-repository-sui/CHANGELOG.md +++ b/packages/lint-repository-sui/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 1.3.0 (2024-03-21) + + +### Features + +* make public ([b261c3b](https://github.com/SUI-Components/sui/commit/b261c3bb2f24601924757cc594a23bea5cee074a)) + + + # 1.2.0 (2024-03-21) diff --git a/packages/lint-repository-sui/package.json b/packages/lint-repository-sui/package.json index 8e34b53dd..661cf9603 100644 --- a/packages/lint-repository-sui/package.json +++ b/packages/lint-repository-sui/package.json @@ -1,7 +1,7 @@ { "name": "lint-repository-sui", "access": "public", - "version": "1.2.0", + "version": "1.3.0", "description": "Set of sui lint rules to lint a repository", "keywords": [], "author": "Sui", From 760b85427c5240c56dbd140b3cf42f46051143fd Mon Sep 17 00:00:00 2001 From: "jordi.munoz@adevinta.com" Date: Fri, 22 Mar 2024 17:44:43 +0100 Subject: [PATCH 073/103] feat(packages/sui-js): use a more solid hashing function --- packages/sui-js/src/hash/index.js | 105 +---------------- packages/sui-js/src/hash/sha256.js | 180 +++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 100 deletions(-) create mode 100644 packages/sui-js/src/hash/sha256.js diff --git a/packages/sui-js/src/hash/index.js b/packages/sui-js/src/hash/index.js index c5276b1a8..6ece388b7 100644 --- a/packages/sui-js/src/hash/index.js +++ b/packages/sui-js/src/hash/index.js @@ -1,3 +1,5 @@ +import Sha256 from "./sha256.js"; + // extracted from https://github.com/jbt/tiny-hashes/blob/master/md5/md5.js const m = [ -680876936, -389564586, 606105819, -1044525330, -176418897, 1200080426, -1473231341, -45705983, 1770035416, @@ -39,106 +41,9 @@ const md5 = function (c) { return c } // eslint-disable-line -// extracted from https://geraintluff.github.io/sha256/ -const sha256 = async ascii => { - function rightRotate(value, amount) { - return (value >>> amount) | (value << (32 - amount)) - } - - const mathPow = Math.pow - const maxWord = mathPow(2, 32) - const lengthProperty = 'length' - let i, j // Used as a counter across the whole file - let result = '' - - const words = [] - const asciiBitLength = ascii[lengthProperty] * 8 - - //* caching results is optional - remove/add slash from front of this line to toggle - // Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes - // (we actually calculate the first 64, but extra values are just ignored) - let hash = (sha256.h = sha256.h || []) - // Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes - const k = (sha256.k = sha256.k || []) - let primeCounter = k[lengthProperty] - /* / - var hash = [], k = []; - var primeCounter = 0; - // */ - - const isComposite = {} - for (let candidate = 2; primeCounter < 64; candidate++) { - if (!isComposite[candidate]) { - for (i = 0; i < 313; i += candidate) { - isComposite[i] = candidate - } - hash[primeCounter] = (mathPow(candidate, 0.5) * maxWord) | 0 - k[primeCounter++] = (mathPow(candidate, 1 / 3) * maxWord) | 0 - } - } - - ascii += '\x80' // Append Ƈ' bit (plus zero padding) - while ((ascii[lengthProperty] % 64) - 56) ascii += '\x00' // More zero padding - for (i = 0; i < ascii[lengthProperty]; i++) { - j = ascii.charCodeAt(i) - if (j >> 8) return // ASCII check: only accept characters in range 0-255 - words[i >> 2] |= j << (((3 - i) % 4) * 8) - } - words[words[lengthProperty]] = (asciiBitLength / maxWord) | 0 - words[words[lengthProperty]] = asciiBitLength - - // process each chunk - for (j = 0; j < words[lengthProperty]; ) { - const w = words.slice(j, (j += 16)) // The message is expanded into 64 words as part of the iteration - const oldHash = hash - // This is now the undefinedworking hash", often labelled as variables a...g - // (we have to truncate as well, otherwise extra entries at the end accumulate - hash = hash.slice(0, 8) - - for (i = 0; i < 64; i++) { - // Expand the message into 64 words - // Used below if - const w15 = w[i - 15] - const w2 = w[i - 2] - - // Iterate - const a = hash[0] - const e = hash[4] - const temp1 = - hash[7] + - (rightRotate(e, 6) ^ rightRotate(e, 11) ^ rightRotate(e, 25)) + // S1 - ((e & hash[5]) ^ (~e & hash[6])) + // ch - k[i] + - // Expand the message schedule if needed - (w[i] = - i < 16 - ? w[i] - : (w[i - 16] + - (rightRotate(w15, 7) ^ rightRotate(w15, 18) ^ (w15 >>> 3)) + // s0 - w[i - 7] + - (rightRotate(w2, 17) ^ rightRotate(w2, 19) ^ (w2 >>> 10))) | // s1 - 0) - // This is only used once, so *could* be moved below, but it only saves 4 bytes and makes things unreadble - const temp2 = - (rightRotate(a, 2) ^ rightRotate(a, 13) ^ rightRotate(a, 22)) + // S0 - ((a & hash[1]) ^ (a & hash[2]) ^ (hash[1] & hash[2])) // maj - - hash = [(temp1 + temp2) | 0].concat(hash) // We don't bother trimming off the extra ones, they're harmless as long as we're truncating when we do the slice() - hash[4] = (hash[4] + temp1) | 0 - } - - for (i = 0; i < 8; i++) { - hash[i] = (hash[i] + oldHash[i]) | 0 - } - } - - for (i = 0; i < 8; i++) { - for (j = 3; j + 1; j--) { - const b = (hash[i] >> (j * 8)) & 255 - result += (b < 16 ? 0 : '') + b.toString(16) - } - } - return result +// extracted from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string +const sha256 = async value => { + return Sha256.hash(value) } export const createHash = md5 diff --git a/packages/sui-js/src/hash/sha256.js b/packages/sui-js/src/hash/sha256.js new file mode 100644 index 000000000..fae665281 --- /dev/null +++ b/packages/sui-js/src/hash/sha256.js @@ -0,0 +1,180 @@ +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ +/* SHA-256 (FIPS 180-4) implementation in JavaScript (c) Chris Veness 2002-2019 */ +/* MIT Licence */ +/* www.movable-type.co.uk/scripts/sha256.html */ +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + +/** + * SHA-256 hash function reference implementation. + * + * This is an annotated direct implementation of FIPS 180-4, without any optimisations. It is + * intended to aid understanding of the algorithm rather than for production use. + * + * While it could be used where performance is not critical, I would recommend using the ‘Web + * Cryptography API’ (developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest) for the browser, + * or the ‘crypto’ library (nodejs.org/api/crypto.html#crypto_class_hash) in Node.js. + * + * See csrc.nist.gov/groups/ST/toolkit/secure_hashing.html + * csrc.nist.gov/groups/ST/toolkit/examples.html + */ +class Sha256 { + + /** + * Generates SHA-256 hash of string. + * + * @param {string} msg - (Unicode) string to be hashed. + * @param {Object} [options] + * @param {string} [options.msgFormat=string] - Message format: 'string' for JavaScript string + * (gets converted to UTF-8 for hashing); 'hex-bytes' for string of hex bytes ('616263' ≡ 'abc') . + * @param {string} [options.outFormat=hex] - Output format: 'hex' for string of contiguous + * hex bytes; 'hex-w' for grouping hex bytes into groups of (4 byte / 8 character) words. + * @returns {string} Hash of msg as hex character string. + * + * @example + * import Sha256 from './sha256.js'; + * const hash = Sha256.hash('abc'); // 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' + */ + static hash(msg, options) { + const defaults = { msgFormat: 'string', outFormat: 'hex' }; + const opt = Object.assign(defaults, options); + + // note use throughout this routine of 'n >>> 0' to coerce Number 'n' to unsigned 32-bit integer + + switch (opt.msgFormat) { + default: // default is to convert string to UTF-8, as SHA only deals with byte-streams + case 'string': msg = utf8Encode(msg); break; + case 'hex-bytes':msg = hexBytesToString(msg); break; // mostly for running tests + } + + // constants [§4.2.2] + const K = [ + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 ]; + + // initial hash value [§5.3.3] + const H = [ + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ]; + + // PREPROCESSING [§6.2.1] + + msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [§5.1.1] + + // convert string msg into 512-bit blocks (array of 16 32-bit integers) [§5.2.1] + const l = msg.length/4 + 2; // length (in 32-bit integers) of msg + ‘1’ + appended length + const N = Math.ceil(l/16); // number of 16-integer (512-bit) blocks required to hold 'l' ints + const M = new Array(N); // message M is N×16 array of 32-bit integers + + for (let i=0; i>> 32, but since JS converts + // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators + const lenHi = ((msg.length-1)*8) / Math.pow(2, 32); + const lenLo = ((msg.length-1)*8) >>> 0; + M[N-1][14] = Math.floor(lenHi); + M[N-1][15] = lenLo; + + + // HASH COMPUTATION [§6.2.2] + + for (let i=0; i>> 0; + } + + // 2 - initialise working variables a, b, c, d, e, f, g, h with previous hash value + let a = H[0], b = H[1], c = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7]; + + // 3 - main loop (note '>>> 0' for 'addition modulo 2^32') + for (let t=0; t<64; t++) { + const T1 = h + Sha256.Σ1(e) + Sha256.Ch(e, f, g) + K[t] + W[t]; + const T2 = Sha256.Σ0(a) + Sha256.Maj(a, b, c); + h = g; + g = f; + f = e; + e = (d + T1) >>> 0; + d = c; + c = b; + b = a; + a = (T1 + T2) >>> 0; + } + + // 4 - compute the new intermediate hash value (note '>>> 0' for 'addition modulo 2^32') + H[0] = (H[0]+a) >>> 0; + H[1] = (H[1]+b) >>> 0; + H[2] = (H[2]+c) >>> 0; + H[3] = (H[3]+d) >>> 0; + H[4] = (H[4]+e) >>> 0; + H[5] = (H[5]+f) >>> 0; + H[6] = (H[6]+g) >>> 0; + H[7] = (H[7]+h) >>> 0; + } + + // convert H0..H7 to hex strings (with leading zeros) + for (let h=0; h prev + String.fromCharCode(curr), ''); + } catch (e) { // no TextEncoder available? + return unescape(encodeURIComponent(str)); // monsur.hossa.in/2012/07/20/utf-8-in-javascript.html + } + } + + function hexBytesToString(hexStr) { // convert string of hex numbers to a string of chars (eg '616263' -> 'abc'). + const str = hexStr.replace(' ', ''); // allow space-separated groups + return str=='' ? '' : str.match(/.{2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join(''); + } + } + + + + /** + * Rotates right (circular right shift) value x by n positions [§3.2.4]. + * @private + */ + static ROTR(n, x) { + return (x >>> n) | (x << (32-n)); + } + + + /** + * Logical functions [§4.1.2]. + * @private + */ + static Σ0(x) { return Sha256.ROTR(2, x) ^ Sha256.ROTR(13, x) ^ Sha256.ROTR(22, x); } + static Σ1(x) { return Sha256.ROTR(6, x) ^ Sha256.ROTR(11, x) ^ Sha256.ROTR(25, x); } + static σ0(x) { return Sha256.ROTR(7, x) ^ Sha256.ROTR(18, x) ^ (x>>>3); } + static σ1(x) { return Sha256.ROTR(17, x) ^ Sha256.ROTR(19, x) ^ (x>>>10); } + static Ch(x, y, z) { return (x & y) ^ (~x & z); } // 'choice' + static Maj(x, y, z) { return (x & y) ^ (x & z) ^ (y & z); } // 'majority' + +} + + +/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + +export default Sha256; \ No newline at end of file From 33ce701612b68f0d3da03a9408b48985f9c10fa0 Mon Sep 17 00:00:00 2001 From: "jordi.munoz@adevinta.com" Date: Fri, 22 Mar 2024 18:04:27 +0100 Subject: [PATCH 074/103] docs(packages/sui-js): remove reference to old function --- packages/sui-js/src/hash/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/sui-js/src/hash/index.js b/packages/sui-js/src/hash/index.js index 6ece388b7..16209627b 100644 --- a/packages/sui-js/src/hash/index.js +++ b/packages/sui-js/src/hash/index.js @@ -1,4 +1,4 @@ -import Sha256 from "./sha256.js"; +import Sha256 from './sha256.js'; // extracted from https://github.com/jbt/tiny-hashes/blob/master/md5/md5.js const m = [ @@ -10,6 +10,7 @@ const m = [ 1126891415, -1416354905, -57434055, 1700485571, -1894986606, -1051523, -2054922799, 1873313359, -30611744, -1560198380, 1309151649, -145523070, -1120210379, 718787259, -343485551 ] // eslint-disable-line + const md5 = function (c) { let e let g @@ -41,7 +42,7 @@ const md5 = function (c) { return c } // eslint-disable-line -// extracted from https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string +// extracted from https://www.movable-type.co.uk/scripts/sha256.html const sha256 = async value => { return Sha256.hash(value) } From c0671da40352c4196d31fe2dd834f07f8dce4863 Mon Sep 17 00:00:00 2001 From: "jordi.munoz@adevinta.com" Date: Fri, 22 Mar 2024 18:13:41 +0100 Subject: [PATCH 075/103] refactor(packages/sui-js): make code linter friendly --- packages/sui-js/src/hash/index.js | 3 +- packages/sui-js/src/hash/sha256.js | 274 ++++++++++++++++------------- 2 files changed, 155 insertions(+), 122 deletions(-) diff --git a/packages/sui-js/src/hash/index.js b/packages/sui-js/src/hash/index.js index 16209627b..0aa8120f3 100644 --- a/packages/sui-js/src/hash/index.js +++ b/packages/sui-js/src/hash/index.js @@ -1,4 +1,4 @@ -import Sha256 from './sha256.js'; +import Sha256 from './sha256.js' // extracted from https://github.com/jbt/tiny-hashes/blob/master/md5/md5.js const m = [ @@ -18,6 +18,7 @@ const md5 = function (c) { let a const h = [] c = unescape(encodeURI(c)) + // eslint-disable-next-line no-var for (var b = c.length, k = [(e = 1732584193), (g = -271733879), ~e, ~g], d = 0; d <= b; ) h[d >> 2] |= (c.charCodeAt(d) || 128) << (8 * (d++ % 4)) h[(c = 16 * ((b + 8) >> 6) + 14)] = 8 * b diff --git a/packages/sui-js/src/hash/sha256.js b/packages/sui-js/src/hash/sha256.js index fae665281..e6dc15bc4 100644 --- a/packages/sui-js/src/hash/sha256.js +++ b/packages/sui-js/src/hash/sha256.js @@ -4,7 +4,6 @@ /* www.movable-type.co.uk/scripts/sha256.html */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ - /** * SHA-256 hash function reference implementation. * @@ -19,7 +18,6 @@ * csrc.nist.gov/groups/ST/toolkit/examples.html */ class Sha256 { - /** * Generates SHA-256 hash of string. * @@ -36,145 +34,179 @@ class Sha256 { * const hash = Sha256.hash('abc'); // 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' */ static hash(msg, options) { - const defaults = { msgFormat: 'string', outFormat: 'hex' }; - const opt = Object.assign(defaults, options); - - // note use throughout this routine of 'n >>> 0' to coerce Number 'n' to unsigned 32-bit integer - - switch (opt.msgFormat) { - default: // default is to convert string to UTF-8, as SHA only deals with byte-streams - case 'string': msg = utf8Encode(msg); break; - case 'hex-bytes':msg = hexBytesToString(msg); break; // mostly for running tests - } - - // constants [§4.2.2] - const K = [ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 ]; - - // initial hash value [§5.3.3] - const H = [ - 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ]; - - // PREPROCESSING [§6.2.1] - - msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [§5.1.1] - - // convert string msg into 512-bit blocks (array of 16 32-bit integers) [§5.2.1] - const l = msg.length/4 + 2; // length (in 32-bit integers) of msg + ‘1’ + appended length - const N = Math.ceil(l/16); // number of 16-integer (512-bit) blocks required to hold 'l' ints - const M = new Array(N); // message M is N×16 array of 32-bit integers - - for (let i=0; i>> 0' to coerce Number 'n' to unsigned 32-bit integer + + switch (opt.msgFormat) { + case 'hex-bytes': + msg = hexBytesToString(msg) + break // mostly for running tests + case 'string': + default: // default is to convert string to UTF-8, as SHA only deals with byte-streams + msg = utf8Encode(msg) + } + + // constants [§4.2.2] + const K = [ + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, + 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, + 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, + 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, + 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, + 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, + 0xc67178f2 + ] + + // initial hash value [§5.3.3] + const H = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19] + + // PREPROCESSING [§6.2.1] + + msg += String.fromCharCode(0x80) // add trailing '1' bit (+ 0's padding) to string [§5.1.1] + + // convert string msg into 512-bit blocks (array of 16 32-bit integers) [§5.2.1] + const l = msg.length / 4 + 2 // length (in 32-bit integers) of msg + ‘1’ + appended length + const N = Math.ceil(l / 16) // number of 16-integer (512-bit) blocks required to hold 'l' ints + const M = new Array(N) // message M is N×16 array of 32-bit integers + + for (let i = 0; i < N; i++) { + M[i] = new Array(16) + for (let j = 0; j < 16; j++) { + // encode 4 chars per integer (64 per block), big-endian encoding + M[i][j] = + (msg.charCodeAt(i * 64 + j * 4 + 0) << 24) | + (msg.charCodeAt(i * 64 + j * 4 + 1) << 16) | + (msg.charCodeAt(i * 64 + j * 4 + 2) << 8) | + (msg.charCodeAt(i * 64 + j * 4 + 3) << 0) + } // note running off the end of msg is ok 'cos bitwise ops on NaN return 0 + } + // add length (in bits) into final pair of 32-bit integers (big-endian) [§5.1.1] + // note: most significant word would be (len-1)*8 >>> 32, but since JS converts + // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators + const lenHi = ((msg.length - 1) * 8) / Math.pow(2, 32) + const lenLo = ((msg.length - 1) * 8) >>> 0 + M[N - 1][14] = Math.floor(lenHi) + M[N - 1][15] = lenLo + + // HASH COMPUTATION [§6.2.2] + + for (let i = 0; i < N; i++) { + const W = new Array(64) + + // 1 - prepare message schedule 'W' + for (let t = 0; t < 16; t++) W[t] = M[i][t] + for (let t = 16; t < 64; t++) { + W[t] = (Sha256.σ1(W[t - 2]) + W[t - 7] + Sha256.σ0(W[t - 15]) + W[t - 16]) >>> 0 } - // add length (in bits) into final pair of 32-bit integers (big-endian) [§5.1.1] - // note: most significant word would be (len-1)*8 >>> 32, but since JS converts - // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators - const lenHi = ((msg.length-1)*8) / Math.pow(2, 32); - const lenLo = ((msg.length-1)*8) >>> 0; - M[N-1][14] = Math.floor(lenHi); - M[N-1][15] = lenLo; - - - // HASH COMPUTATION [§6.2.2] - - for (let i=0; i>> 0; - } - - // 2 - initialise working variables a, b, c, d, e, f, g, h with previous hash value - let a = H[0], b = H[1], c = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7]; - - // 3 - main loop (note '>>> 0' for 'addition modulo 2^32') - for (let t=0; t<64; t++) { - const T1 = h + Sha256.Σ1(e) + Sha256.Ch(e, f, g) + K[t] + W[t]; - const T2 = Sha256.Σ0(a) + Sha256.Maj(a, b, c); - h = g; - g = f; - f = e; - e = (d + T1) >>> 0; - d = c; - c = b; - b = a; - a = (T1 + T2) >>> 0; - } - - // 4 - compute the new intermediate hash value (note '>>> 0' for 'addition modulo 2^32') - H[0] = (H[0]+a) >>> 0; - H[1] = (H[1]+b) >>> 0; - H[2] = (H[2]+c) >>> 0; - H[3] = (H[3]+d) >>> 0; - H[4] = (H[4]+e) >>> 0; - H[5] = (H[5]+f) >>> 0; - H[6] = (H[6]+g) >>> 0; - H[7] = (H[7]+h) >>> 0; - } - - // convert H0..H7 to hex strings (with leading zeros) - for (let h=0; h prev + String.fromCharCode(curr), ''); - } catch (e) { // no TextEncoder available? - return unescape(encodeURIComponent(str)); // monsur.hossa.in/2012/07/20/utf-8-in-javascript.html - } + // 2 - initialise working variables a, b, c, d, e, f, g, h with previous hash value + let a = H[0] + let b = H[1] + let c = H[2] + let d = H[3] + let e = H[4] + let f = H[5] + let g = H[6] + let h = H[7] + + // 3 - main loop (note '>>> 0' for 'addition modulo 2^32') + for (let t = 0; t < 64; t++) { + const T1 = h + Sha256.Σ1(e) + Sha256.Ch(e, f, g) + K[t] + W[t] + const T2 = Sha256.Σ0(a) + Sha256.Maj(a, b, c) + h = g + g = f + f = e + e = (d + T1) >>> 0 + d = c + c = b + b = a + a = (T1 + T2) >>> 0 } - function hexBytesToString(hexStr) { // convert string of hex numbers to a string of chars (eg '616263' -> 'abc'). - const str = hexStr.replace(' ', ''); // allow space-separated groups - return str=='' ? '' : str.match(/.{2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join(''); + // 4 - compute the new intermediate hash value (note '>>> 0' for 'addition modulo 2^32') + H[0] = (H[0] + a) >>> 0 + H[1] = (H[1] + b) >>> 0 + H[2] = (H[2] + c) >>> 0 + H[3] = (H[3] + d) >>> 0 + H[4] = (H[4] + e) >>> 0 + H[5] = (H[5] + f) >>> 0 + H[6] = (H[6] + g) >>> 0 + H[7] = (H[7] + h) >>> 0 + } + + // convert H0..H7 to hex strings (with leading zeros) + for (let h = 0; h < H.length; h++) H[h] = ('00000000' + H[h].toString(16)).slice(-8) + + // concatenate H0..H7, with separator if required + // eslint-disable-next-line eqeqeq + const separator = opt.outFormat == 'hex-w' ? ' ' : '' + + return H.join(separator) + + /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ + + function utf8Encode(str) { + try { + return new TextEncoder().encode(str, 'utf-8').reduce((prev, curr) => prev + String.fromCharCode(curr), '') + } catch (e) { + // no TextEncoder available? + return unescape(encodeURIComponent(str)) // monsur.hossa.in/2012/07/20/utf-8-in-javascript.html } + } + + function hexBytesToString(hexStr) { + // convert string of hex numbers to a string of chars (eg '616263' -> 'abc'). + const str = hexStr.replace(' ', '') // allow space-separated groups + // eslint-disable-next-line eqeqeq + return str == '' + ? '' + : str + .match(/.{2}/g) + .map(byte => String.fromCharCode(parseInt(byte, 16))) + .join('') + } } - - /** * Rotates right (circular right shift) value x by n positions [§3.2.4]. * @private */ static ROTR(n, x) { - return (x >>> n) | (x << (32-n)); + return (x >>> n) | (x << (32 - n)) } - /** * Logical functions [§4.1.2]. * @private */ - static Σ0(x) { return Sha256.ROTR(2, x) ^ Sha256.ROTR(13, x) ^ Sha256.ROTR(22, x); } - static Σ1(x) { return Sha256.ROTR(6, x) ^ Sha256.ROTR(11, x) ^ Sha256.ROTR(25, x); } - static σ0(x) { return Sha256.ROTR(7, x) ^ Sha256.ROTR(18, x) ^ (x>>>3); } - static σ1(x) { return Sha256.ROTR(17, x) ^ Sha256.ROTR(19, x) ^ (x>>>10); } - static Ch(x, y, z) { return (x & y) ^ (~x & z); } // 'choice' - static Maj(x, y, z) { return (x & y) ^ (x & z) ^ (y & z); } // 'majority' + static Σ0(x) { + return Sha256.ROTR(2, x) ^ Sha256.ROTR(13, x) ^ Sha256.ROTR(22, x) + } -} + static Σ1(x) { + return Sha256.ROTR(6, x) ^ Sha256.ROTR(11, x) ^ Sha256.ROTR(25, x) + } + static σ0(x) { + return Sha256.ROTR(7, x) ^ Sha256.ROTR(18, x) ^ (x >>> 3) + } + + static σ1(x) { + return Sha256.ROTR(17, x) ^ Sha256.ROTR(19, x) ^ (x >>> 10) + } + + static Ch(x, y, z) { + return (x & y) ^ (~x & z) + } // 'choice' + + static Maj(x, y, z) { + return (x & y) ^ (x & z) ^ (y & z) + } // 'majority' +} /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -export default Sha256; \ No newline at end of file +export default Sha256 From c1b65847a7adb466c24f20c922fdd17cd55b521e Mon Sep 17 00:00:00 2001 From: Kiko Ruiz Lloret Date: Fri, 22 Mar 2024 19:24:14 +0100 Subject: [PATCH 076/103] fix(packages/sui-react-context): fix json wrong character --- packages/sui-react-context/tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sui-react-context/tsconfig.json b/packages/sui-react-context/tsconfig.json index e64ed44cf..c69f8270e 100644 --- a/packages/sui-react-context/tsconfig.json +++ b/packages/sui-react-context/tsconfig.json @@ -2,6 +2,6 @@ "extends": "../../tsconfig.json", "compilerOptions": { "outDir": "./lib", - "rootDir": "./src", + "rootDir": "./src" } -} \ No newline at end of file +} From 04a9988ad5daa55bccf4c925d986bc46b348cb53 Mon Sep 17 00:00:00 2001 From: Kiko Ruiz Lloret Date: Fri, 22 Mar 2024 19:28:22 +0100 Subject: [PATCH 077/103] fix(packages/sui-react-context): fix compilation --- packages/sui-react-context/package.json | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/sui-react-context/package.json b/packages/sui-react-context/package.json index 4fab32f5d..3bcfcae32 100644 --- a/packages/sui-react-context/package.json +++ b/packages/sui-react-context/package.json @@ -3,14 +3,17 @@ "version": "1.8.0", "description": "", "main": "lib/index", - "types": "src/index.tsx", + "types": "lib/index.d.ts", "scripts": { - "lib": "rm -rf ./lib && tsc", + "lib": "sui-js-compiler", "prepublishOnly": "npm run lib" }, "keywords": [], "author": "", "license": "MIT", + "devDependencies": { + "@s-ui/js-compiler": "1" + }, "dependencies": { "hoist-non-react-statics": "3.3.2" } From b76fb6ca4f9eeab69a7f3cc7d19175bc1a03430b Mon Sep 17 00:00:00 2001 From: Kiko Ruiz Lloret Date: Fri, 22 Mar 2024 19:28:54 +0100 Subject: [PATCH 078/103] fix(packages/sui-react-context): fix linting problems --- packages/sui-react-context/src/index.tsx | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/packages/sui-react-context/src/index.tsx b/packages/sui-react-context/src/index.tsx index 1c1f0bbd0..0fea4a34b 100644 --- a/packages/sui-react-context/src/index.tsx +++ b/packages/sui-react-context/src/index.tsx @@ -2,8 +2,7 @@ import * as React from 'react' import hoistNonReactStatics from 'hoist-non-react-statics' -type SUIContextType = React.Context -& { +type SUIContextType = React.Context & { wrapper?: (Component: React.ComponentType, displayName: string) => React.ComponentType } @@ -11,19 +10,15 @@ const SUIContext: SUIContextType = React.createContext({}) SUIContext.wrapper = (Component, displayName): React.ComponentType => { const WrappedComponent = (props: any): JSX.Element => ( - - {context => } - + {context => } ) - WrappedComponent.displayName = Component.displayName !== undefined - ? Component.displayName - : displayName + WrappedComponent.displayName = Component.displayName !== undefined ? Component.displayName : displayName return hoistNonReactStatics(WrappedComponent, Component) } -export function useSuiContext (): React.Context { +export const useSuiContext = (): React.Context => { return React.useContext(SUIContext) } From c8049b77c45ca0cf28d3f9769e4b88ed7783b965 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Fri, 22 Mar 2024 18:41:16 +0000 Subject: [PATCH 079/103] release(packages/sui-react-context): v1.9.0 [skip ci] --- packages/sui-react-context/CHANGELOG.md | 16 ++++++++++++---- packages/sui-react-context/package.json | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/sui-react-context/CHANGELOG.md b/packages/sui-react-context/CHANGELOG.md index 8caad04ca..c994df2d6 100644 --- a/packages/sui-react-context/CHANGELOG.md +++ b/packages/sui-react-context/CHANGELOG.md @@ -1,5 +1,16 @@ # CHANGELOG +# 1.9.0 (2024-03-22) + + +### Bug Fixes + +* fix compilation ([04a9988](https://github.com/SUI-Components/sui/commit/04a9988ad5daa55bccf4c925d986bc46b348cb53)) +* fix json wrong character ([c1b6584](https://github.com/SUI-Components/sui/commit/c1b65847a7adb466c24f20c922fdd17cd55b521e)) +* fix linting problems ([b76fb6c](https://github.com/SUI-Components/sui/commit/b76fb6ca4f9eeab69a7f3cc7d19175bc1a03430b)) + + + # 1.8.0 (2022-08-12) @@ -67,7 +78,4 @@ * **sui-react-context:** bump Beta version ([0a31ada](https://github.com/SUI-Components/sui/commit/0a31ada0ae1933886d2891ad3408c391765d6b40)) * **sui-react-context:** create wrapper HoC ([b11bde9](https://github.com/SUI-Components/sui/commit/b11bde911a7726d6e5c929010ea92d229f96190e)) * **sui-react-context:** first commit ([c4524ef](https://github.com/SUI-Components/sui/commit/c4524ef0274e92ca97ad5637ded76bee7d0f282d)) -* **sui-react-context:** update deps ([2fa9a28](https://github.com/SUI-Components/sui/commit/2fa9a285225117117b81847e88513ec96d9ecc60)) - - - +* **sui-react-context:** update deps ([2fa9a28](https://github.com/SUI-Components/sui/commit/2fa9a285225117117b81847e88513ec96d9ecc60)) \ No newline at end of file diff --git a/packages/sui-react-context/package.json b/packages/sui-react-context/package.json index 3bcfcae32..93c13ab2d 100644 --- a/packages/sui-react-context/package.json +++ b/packages/sui-react-context/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/react-context", - "version": "1.8.0", + "version": "1.9.0", "description": "", "main": "lib/index", "types": "lib/index.d.ts", From 3cd74f3965dba469fceefc1ce8c2cb327c268161 Mon Sep 17 00:00:00 2001 From: "jordi.munoz@adevinta.com" Date: Sat, 23 Mar 2024 11:05:44 +0100 Subject: [PATCH 080/103] test(packages/sui-js): added tests for sha256 hashing function --- packages/sui-js/test/hashSpec.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 packages/sui-js/test/hashSpec.js diff --git a/packages/sui-js/test/hashSpec.js b/packages/sui-js/test/hashSpec.js new file mode 100644 index 000000000..b0892fc22 --- /dev/null +++ b/packages/sui-js/test/hashSpec.js @@ -0,0 +1,21 @@ +/* eslint-env mocha */ +import {expect} from 'chai' + +import {createAsyncSha256} from '../src/hash/index.js' + +describe('@s-ui/js', () => { + describe('string:sha256', () => { + it('should hash a specific string to something known', async () => { + const hashedString = await createAsyncSha256('abc') + expect(hashedString).to.equal('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') + }) + it('should hash a specific email string to something known', async () => { + const hashedString = await createAsyncSha256('firstname.lastname@example.com') + expect(hashedString).to.equal('4a0a303e33c11f496a9312a77309133325af1527a26d9d95cf74b81feba9c955') + }) + it('should hash a specific unicode string to something known', async () => { + const hashedString = await createAsyncSha256('Barça') + expect(hashedString).to.equal('d7bbc6d46128ea3a0fe95c744899f7724ce15600e3e6bf2d93bc319b1252cd9b') + }) + }) +}) From 1630cd92d124b746af02cad5f5b74f5d2d294488 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Sat, 23 Mar 2024 10:50:34 +0000 Subject: [PATCH 081/103] release(packages/sui-js): v2.31.0 [skip ci] --- packages/sui-js/CHANGELOG.md | 10 ++++++++++ packages/sui-js/package.json | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/sui-js/CHANGELOG.md b/packages/sui-js/CHANGELOG.md index 581c5c5b5..9e5a4b3e7 100644 --- a/packages/sui-js/CHANGELOG.md +++ b/packages/sui-js/CHANGELOG.md @@ -1,5 +1,15 @@ # CHANGELOG +# 2.31.0 (2024-03-23) + + +### Features + +* use a more solid hashing function ([760b854](https://github.com/SUI-Components/sui/commit/760b85427c5240c56dbd140b3cf42f46051143fd)) +* using a pure js function to make this utility hashing function isomorphic ([dd18e41](https://github.com/SUI-Components/sui/commit/dd18e41fc448a27d09cfe7e7c255efa7297880ac)) + + + # 2.30.0 (2024-03-07) diff --git a/packages/sui-js/package.json b/packages/sui-js/package.json index 5b958d3b5..dcb7e22ca 100644 --- a/packages/sui-js/package.json +++ b/packages/sui-js/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/js", - "version": "2.30.0", + "version": "2.31.0", "description": "Set of JS utilities", "type": "module", "scripts": { From 06c29f5b27a2a9bb240f558b602b471b77e24eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Leo=CC=81n?= Date: Tue, 26 Mar 2024 16:54:24 +0100 Subject: [PATCH 082/103] feat(packages/sui-react-web-vitals): upgrade web vitals package --- packages/sui-react-web-vitals/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sui-react-web-vitals/package.json b/packages/sui-react-web-vitals/package.json index 1395b868f..74c9ac8d1 100644 --- a/packages/sui-react-web-vitals/package.json +++ b/packages/sui-react-web-vitals/package.json @@ -17,7 +17,7 @@ "author": "", "license": "MIT", "dependencies": { - "web-vitals": "3.5.0", + "web-vitals": "3.5.2", "@s-ui/react-hooks": "1" }, "peerDependencies": { From ddde93fdebbde6595dd1e19a5654794610fc4bd6 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Tue, 26 Mar 2024 17:05:14 +0000 Subject: [PATCH 083/103] release(packages/sui-react-web-vitals): v2.4.0 [skip ci] --- packages/sui-react-web-vitals/CHANGELOG.md | 9 +++++++++ packages/sui-react-web-vitals/package.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/sui-react-web-vitals/CHANGELOG.md b/packages/sui-react-web-vitals/CHANGELOG.md index 235a5d750..3388a1cdd 100644 --- a/packages/sui-react-web-vitals/CHANGELOG.md +++ b/packages/sui-react-web-vitals/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 2.4.0 (2024-03-26) + + +### Features + +* upgrade web vitals package ([06c29f5](https://github.com/SUI-Components/sui/commit/06c29f5b27a2a9bb240f558b602b471b77e24eb6)) + + + # 2.3.0 (2024-02-12) diff --git a/packages/sui-react-web-vitals/package.json b/packages/sui-react-web-vitals/package.json index 74c9ac8d1..251d76083 100644 --- a/packages/sui-react-web-vitals/package.json +++ b/packages/sui-react-web-vitals/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/react-web-vitals", - "version": "2.3.0", + "version": "2.4.0", "description": "", "type": "module", "main": "lib/index.js", From f1776ac105a423d3dc774ffcd869b3bae41d8fbb Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Tue, 2 Apr 2024 15:23:42 +0200 Subject: [PATCH 084/103] feat(packages/lint-repository-sui): Create Repository Structure rule --- packages/lint-repository-sui/src/index.js | 4 +- .../src/rules/structure.js | 80 +++++++++++++++++++ .../test/server/node-versionSpec.js | 2 +- .../test/server/structureSpec.js | 26 ++++++ 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 packages/lint-repository-sui/src/rules/structure.js create mode 100644 packages/lint-repository-sui/test/server/structureSpec.js diff --git a/packages/lint-repository-sui/src/index.js b/packages/lint-repository-sui/src/index.js index d1f8a51d1..10bed5dc8 100644 --- a/packages/lint-repository-sui/src/index.js +++ b/packages/lint-repository-sui/src/index.js @@ -3,6 +3,7 @@ const ReactVersion = require('./rules/react-version.js') const PackageLock = require('./rules/package-lock.js') const GithubAction = require('./rules/github-action.js') const TypeScript = require('./rules/typescript.js') +const Structure = require('./rules/structure.js') // ------------------------------------------------------------------------------ // Plugin Definition @@ -15,6 +16,7 @@ module.exports = { 'react-version': ReactVersion, 'package-lock': PackageLock, 'github-action': GithubAction, - typescript: TypeScript + typescript: TypeScript, + structure: Structure } } diff --git a/packages/lint-repository-sui/src/rules/structure.js b/packages/lint-repository-sui/src/rules/structure.js new file mode 100644 index 000000000..729d7a70a --- /dev/null +++ b/packages/lint-repository-sui/src/rules/structure.js @@ -0,0 +1,80 @@ +const dedent = require('string-dedent') + +const FILES_AND_FOLDERS = [ + '.docker', + '.github', + 'Makefile', + 'package.json', + 'package-lock.json', + '.nvmrc', + '.dockerignore', + '.gitignore', + 'tsconfig.json', + 'README.md', + + 'app/src', + 'app/test', + 'app/Makefile', + 'app/package.json', + 'app/tsconfig.json', + 'app/CHANGELOG.md', + + 'app/src/pages', + 'app/src/app.(t|j)s(x)?', + 'app/src/contextFactory.(t|j)s(x)?', + 'app/src/hooks.(t|j)s(x)?', + 'app/src/index.html', + 'app/src/index.(s)?css', + 'app/src/routes.(t|j)s(x)?', + 'pacakges/**/src', + 'pacakges/**/test', + 'pacakges/**/Makefile', + 'pacakges/**/package.json', + 'pacakges/**/tsconfig.json', + 'pacakges/**/CHANGELOG.md', + + 'deploy/config-(pre|pro).yml', + 'deploy/*-(pre|pro)-paas.yml', + 'deploy/tags.yml', + + 'qa/e2e' +] + +module.exports = { + FILES_AND_FOLDERS, + meta: { + type: 'problem', + docs: { + description: 'Check that your repository follow the structure define', + 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: { + missingFileOrFolder: dedent` + The file or folder {{pattern}} is missing. + This repository should follow the Golden Path file Structure. + If you are not sure about how do it, please contact with Platform Web. + ` + } + }, + create: function (context) { + return { + ...Object.fromEntries(FILES_AND_FOLDERS.map(pattern => [pattern, () => context.monitoring(true)])), + + missmatch: key => { + context.monitoring(false) + context.report({ + messageId: 'missingFileOrFolder', + data: {pattern: key} + }) + } + } + }, + reduceMonitoring: function (monitorings) { + return monitorings.reduce((acc, signal) => { + return acc && signal.value + }, true) + } +} diff --git a/packages/lint-repository-sui/test/server/node-versionSpec.js b/packages/lint-repository-sui/test/server/node-versionSpec.js index daf20325d..db1c118b6 100644 --- a/packages/lint-repository-sui/test/server/node-versionSpec.js +++ b/packages/lint-repository-sui/test/server/node-versionSpec.js @@ -24,7 +24,7 @@ RuleTester.create('node-version', handler).run({ }, { '.nvmrc': [MatchStub.create({raw: '16.1.3'})], - name: 'Exits more than one nvmrc file', + name: 'nvmrc exists, but has an invalid Node version 🫠', report: dedent` Your current Node version is 16. Please be sure that your repository use the latest Node Version 20. diff --git a/packages/lint-repository-sui/test/server/structureSpec.js b/packages/lint-repository-sui/test/server/structureSpec.js new file mode 100644 index 000000000..78a5f8bbe --- /dev/null +++ b/packages/lint-repository-sui/test/server/structureSpec.js @@ -0,0 +1,26 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/structure.js' +import {RuleTester} from '../TestHelpers.js' + +RuleTester.create('structure', handler).run({ + valid: handler.FILES_AND_FOLDERS.map(pattern => { + return { + [pattern]: [], + name: `Testing ${pattern}`, + monitoring: true + } + }), + invalid: handler.FILES_AND_FOLDERS.map(pattern => { + return { + missmatch: pattern, + name: `Failed ${pattern}`, + report: dedent` + The file or folder ${pattern} is missing. + This repository should follow the Golden Path file Structure. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + } + }) +}) From 8dd7c14e4a63d36403e6c4c775a39d424d1c3b33 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Tue, 2 Apr 2024 15:24:14 +0200 Subject: [PATCH 085/103] feat(packages/sui-lint): enable repository structure rule --- packages/sui-lint/repository.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/sui-lint/repository.config.js b/packages/sui-lint/repository.config.js index bd8ef0308..13238617e 100644 --- a/packages/sui-lint/repository.config.js +++ b/packages/sui-lint/repository.config.js @@ -11,6 +11,7 @@ module.exports = { 'sui/react-version': RULES.WARNING, 'sui/package-lock': RULES.WARNING, 'sui/github-action': RULES.WARNING, - 'sui/typescript': RULES.WARNING + 'sui/typescript': RULES.WARNING, + 'sui/structure': RULES.WARNING } } From 7f6f0bf0a7d8ce70e0b936fd1afe524f15ba84a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Leo=CC=81n?= Date: Tue, 2 Apr 2024 17:04:42 +0200 Subject: [PATCH 086/103] feat(packages/lint-repository-sui): add sui-tools-version rule --- packages/lint-repository-sui/src/index.js | 4 +- .../src/rules/sui-tools-version.js | 86 +++++++++++++++++++ .../test/server/sui-tools-versionSepec.js | 51 +++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 packages/lint-repository-sui/src/rules/sui-tools-version.js create mode 100644 packages/lint-repository-sui/test/server/sui-tools-versionSepec.js diff --git a/packages/lint-repository-sui/src/index.js b/packages/lint-repository-sui/src/index.js index 10bed5dc8..8e2780b23 100644 --- a/packages/lint-repository-sui/src/index.js +++ b/packages/lint-repository-sui/src/index.js @@ -4,6 +4,7 @@ const PackageLock = require('./rules/package-lock.js') const GithubAction = require('./rules/github-action.js') const TypeScript = require('./rules/typescript.js') const Structure = require('./rules/structure.js') +const SuiToolsVersion = require('./rules/sui-tools-version.js') // ------------------------------------------------------------------------------ // Plugin Definition @@ -17,6 +18,7 @@ module.exports = { 'package-lock': PackageLock, 'github-action': GithubAction, typescript: TypeScript, - structure: Structure + structure: Structure, + 'sui-tools-version': SuiToolsVersion } } diff --git a/packages/lint-repository-sui/src/rules/sui-tools-version.js b/packages/lint-repository-sui/src/rules/sui-tools-version.js new file mode 100644 index 000000000..fbd24017d --- /dev/null +++ b/packages/lint-repository-sui/src/rules/sui-tools-version.js @@ -0,0 +1,86 @@ +const dedent = require('string-dedent') + +const SUI_TOOLS_VERSIONS = { + bundler: '9', + lint: '4', + studio: '11', + ssr: '8', + mono: '2', + 'test-e2e': '1', + pde: '2', + test: '8', + theme: '8', + domain: '2' +} + +module.exports = { + SUI_TOOLS_VERSIONS, + meta: { + type: 'problem', + docs: { + description: 'Check that your repository use the latest SUI Tools version', + recommended: true, + url: null + }, + fixable: null, + schema: [], + messages: { + badVersion: dedent` + Please be sure that your repository use the latest {{name}}. Version {{spectedVersion}}. + Your current version is {{version}}. + If you are not sure about how do it, please contact with Platform Web. + `, + missingDependencie: dedent` + Your project doesnt have installed {{name}}. + Please install at least the version {{spectedVersion}}. + If you are not sure about how do it, please contact with Platform Web. + `, + missingPackageLock: dedent` + To calculate the SUI Tool version first we need to have a package-lock.json in the root + If you are not sure about how do it, please contact with Platform Web. + ` + } + }, + create: function (context) { + return { + 'package-lock.json': matches => { + const [packageLock] = matches + + Object.entries(SUI_TOOLS_VERSIONS).forEach(([name, spectedVersion]) => { + let version = packageLock?.parsed?.packages?.[`node_modules/@s-ui/${name}`]?.version + + if (!version) { + context.report({ + messageId: 'missingDependencie', + data: {name, spectedVersion} + }) + return context.monitoring(false) + } + + version = version.split('.')[0] + + if (version !== spectedVersion) { + context.report({ + messageId: 'badVersion', + data: {name, version, spectedVersion} + }) + return context.monitoring(false) + } + return context.monitoring(true) + }) + }, + + missmatch: () => { + context.report({ + messageId: 'missingPackageLock' + }) + context.monitoring(0) + } + } + }, + reduceMonitoring: function (monitorings) { + return monitorings.reduce((acc, signal) => { + return acc && signal.value + }, true) + } +} diff --git a/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js b/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js new file mode 100644 index 000000000..a4945d821 --- /dev/null +++ b/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js @@ -0,0 +1,51 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/sui-tools-version.js' +import {MatchStub, RuleTester} from '../TestHelpers.js' + +const SUI_TOOLS_ENTRIES = Object.entries(handler.SUI_TOOLS_VERSIONS) + +// MatchStub.create({parsed: {packages: {[`node_modules/@s-ui/${name}`]: {version: spectedVersion}}}}) +RuleTester.create('sui-tools-version', handler).run({ + valid: [ + { + 'package-lock.json': SUI_TOOLS_ENTRIES.map(([name, spectedVersion]) => { + return MatchStub.create({parsed: {packages: {[`node_modules/@s-ui/${name}`]: {version: spectedVersion}}}}) + }), + name: 'Testing all SUI Tools packages', + monitoring: true + } + ], + invalid: [ + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {}}})], + name: `SUI Bundler not installed`, + report: dedent` + Your project doesnt have installed bundler. + Please install at least the version ${SUI_TOOLS_ENTRIES[0][1]}. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + }, + { + 'package-lock.json': SUI_TOOLS_ENTRIES.map(([name]) => { + return MatchStub.create({parsed: {packages: {[`node_modules/@s-ui/${name}`]: {version: '100'}}}}) + }), + name: `SUI Bundler wrong version`, + report: dedent` + Please be sure that your repository use the latest bundler. Version ${SUI_TOOLS_ENTRIES[0][1]}. + Your current version is 100. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + }, + { + missmatch: '', + report: dedent` + To calculate the SUI Tool version first we need to have a package-lock.json in the root + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + } + ] +}) From 1932c1dc795a450b88573ff381aa04a8d9fc5262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20Leo=CC=81n?= Date: Tue, 2 Apr 2024 17:05:50 +0200 Subject: [PATCH 087/103] feat(packages/sui-lint): add sui-tools-version rule --- packages/sui-lint/repository.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/sui-lint/repository.config.js b/packages/sui-lint/repository.config.js index 13238617e..679a88a49 100644 --- a/packages/sui-lint/repository.config.js +++ b/packages/sui-lint/repository.config.js @@ -12,6 +12,7 @@ module.exports = { 'sui/package-lock': RULES.WARNING, 'sui/github-action': RULES.WARNING, 'sui/typescript': RULES.WARNING, - 'sui/structure': RULES.WARNING + 'sui/structure': RULES.WARNING, + 'sui/sui-tools-version': RULES.WARNING } } From 48937153d2a043df94d362eb49b06bfa2bcd3448 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 3 Apr 2024 11:50:26 +0200 Subject: [PATCH 088/103] feat(packages/lint-repository-sui): Add check for ADV tools version --- packages/lint-repository-sui/src/index.js | 4 +- .../src/rules/adv-tools-version.js | 78 +++++++++++++++++++ .../test/server/adv-tools-versionSpec.js | 50 ++++++++++++ .../test/server/sui-tools-versionSepec.js | 1 - 4 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 packages/lint-repository-sui/src/rules/adv-tools-version.js create mode 100644 packages/lint-repository-sui/test/server/adv-tools-versionSpec.js diff --git a/packages/lint-repository-sui/src/index.js b/packages/lint-repository-sui/src/index.js index 8e2780b23..92feca32a 100644 --- a/packages/lint-repository-sui/src/index.js +++ b/packages/lint-repository-sui/src/index.js @@ -5,6 +5,7 @@ const GithubAction = require('./rules/github-action.js') const TypeScript = require('./rules/typescript.js') const Structure = require('./rules/structure.js') const SuiToolsVersion = require('./rules/sui-tools-version.js') +const ADVToolsVersion = require('./rules/adv-tools-version.js') // ------------------------------------------------------------------------------ // Plugin Definition @@ -19,6 +20,7 @@ module.exports = { 'github-action': GithubAction, typescript: TypeScript, structure: Structure, - 'sui-tools-version': SuiToolsVersion + 'sui-tools-version': SuiToolsVersion, + 'adv-tools-version': ADVToolsVersion } } diff --git a/packages/lint-repository-sui/src/rules/adv-tools-version.js b/packages/lint-repository-sui/src/rules/adv-tools-version.js new file mode 100644 index 000000000..d81f3d7a5 --- /dev/null +++ b/packages/lint-repository-sui/src/rules/adv-tools-version.js @@ -0,0 +1,78 @@ +const dedent = require('string-dedent') + +const ADV_TOOLS_VERSIONS = { + logger: '2', + 'lint-reporters': '1' +} + +module.exports = { + ADV_TOOLS_VERSIONS, + meta: { + type: 'problem', + docs: { + description: 'Check that your repository use the latest ADV Tools version', + recommended: true, + url: null + }, + fixable: null, + schema: [], + messages: { + badVersion: dedent` + Please be sure that your repository use the latest {{name}}. Version {{spectedVersion}}. + Your current version is {{version}}. + If you are not sure about how do it, please contact with Platform Web. + `, + missingDependencie: dedent` + Your project doesnt have installed {{name}}. + Please install at least the version {{spectedVersion}}. + If you are not sure about how do it, please contact with Platform Web. + `, + missingPackageLock: dedent` + To calculate the ADV Tool version first we need to have a package-lock.json in the root + If you are not sure about how do it, please contact with Platform Web. + ` + } + }, + create: function (context) { + return { + 'package-lock.json': matches => { + const [packageLock] = matches + + Object.entries(ADV_TOOLS_VERSIONS).forEach(([name, spectedVersion]) => { + let version = packageLock?.parsed?.packages?.[`node_modules/@adv-ui/${name}`]?.version + + if (!version) { + context.report({ + messageId: 'missingDependencie', + data: {name, spectedVersion} + }) + return context.monitoring(false) + } + + version = version.split('.')[0] + + if (version !== spectedVersion) { + context.report({ + messageId: 'badVersion', + data: {name, version, spectedVersion} + }) + return context.monitoring(false) + } + return context.monitoring(true) + }) + }, + + missmatch: () => { + context.report({ + messageId: 'missingPackageLock' + }) + context.monitoring(0) + } + } + }, + reduceMonitoring: function (monitorings) { + return monitorings.reduce((acc, signal) => { + return acc && signal.value + }, true) + } +} diff --git a/packages/lint-repository-sui/test/server/adv-tools-versionSpec.js b/packages/lint-repository-sui/test/server/adv-tools-versionSpec.js new file mode 100644 index 000000000..8d61e3596 --- /dev/null +++ b/packages/lint-repository-sui/test/server/adv-tools-versionSpec.js @@ -0,0 +1,50 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/adv-tools-version.js' +import {MatchStub, RuleTester} from '../TestHelpers.js' + +const ADV_TOOLS_ENTRIES = Object.entries(handler.ADV_TOOLS_VERSIONS) + +RuleTester.create('adv-tools-version', handler).run({ + valid: [ + { + 'package-lock.json': ADV_TOOLS_ENTRIES.map(([name, spectedVersion]) => { + return MatchStub.create({parsed: {packages: {[`node_modules/@adv-ui/${name}`]: {version: spectedVersion}}}}) + }), + name: 'Testing all ADV Tools packages', + monitoring: true + } + ], + invalid: [ + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {}}})], + name: `ADV logger not installed`, + report: dedent` + Your project doesnt have installed logger. + Please install at least the version ${ADV_TOOLS_ENTRIES[0][1]}. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + }, + { + 'package-lock.json': ADV_TOOLS_ENTRIES.map(([name]) => { + return MatchStub.create({parsed: {packages: {[`node_modules/@adv-ui/${name}`]: {version: '100'}}}}) + }), + name: `ADV logger wrong version`, + report: dedent` + Please be sure that your repository use the latest logger. Version ${ADV_TOOLS_ENTRIES[0][1]}. + Your current version is 100. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + }, + { + missmatch: '', + report: dedent` + To calculate the ADV Tool version first we need to have a package-lock.json in the root + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + } + ] +}) diff --git a/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js b/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js index a4945d821..b283d8094 100644 --- a/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js +++ b/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js @@ -5,7 +5,6 @@ import {MatchStub, RuleTester} from '../TestHelpers.js' const SUI_TOOLS_ENTRIES = Object.entries(handler.SUI_TOOLS_VERSIONS) -// MatchStub.create({parsed: {packages: {[`node_modules/@s-ui/${name}`]: {version: spectedVersion}}}}) RuleTester.create('sui-tools-version', handler).run({ valid: [ { From 6394545857204b80f8e6f80d5abaa77899f63400 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 3 Apr 2024 11:51:03 +0200 Subject: [PATCH 089/103] feat(packages/sui-lint): enable test for ADV packages version --- packages/sui-lint/repository.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/sui-lint/repository.config.js b/packages/sui-lint/repository.config.js index 679a88a49..0f4c0fde3 100644 --- a/packages/sui-lint/repository.config.js +++ b/packages/sui-lint/repository.config.js @@ -13,6 +13,7 @@ module.exports = { 'sui/github-action': RULES.WARNING, 'sui/typescript': RULES.WARNING, 'sui/structure': RULES.WARNING, - 'sui/sui-tools-version': RULES.WARNING + 'sui/sui-tools-version': RULES.WARNING, + 'sui/adv-tools-version': RULES.WARNING } } From a4bbed90ded5d414ce52e02b677e2a79fb0fc4f8 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 3 Apr 2024 12:03:54 +0200 Subject: [PATCH 090/103] feat(packages/sui-lint): enable check for cypress version --- packages/sui-lint/repository.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/sui-lint/repository.config.js b/packages/sui-lint/repository.config.js index 0f4c0fde3..f7b2b740b 100644 --- a/packages/sui-lint/repository.config.js +++ b/packages/sui-lint/repository.config.js @@ -9,6 +9,7 @@ module.exports = { rules: { 'sui/node-version': RULES.WARNING, 'sui/react-version': RULES.WARNING, + 'sui/cypress-version': RULES.WARNING, 'sui/package-lock': RULES.WARNING, 'sui/github-action': RULES.WARNING, 'sui/typescript': RULES.WARNING, From 852df2e4c83039a1acae7a39433f1f8b90afa206 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 3 Apr 2024 12:04:58 +0200 Subject: [PATCH 091/103] feat(packages/lint-repository-sui): Create rule to check the Cypress version --- packages/lint-repository-sui/src/index.js | 2 + .../src/rules/adv-tools-version.js | 3 +- .../src/rules/cypress-version.js | 64 +++++++++++++++++++ .../src/rules/sui-tools-version.js | 4 +- .../test/server/cypress-versionSpec.js | 44 +++++++++++++ 5 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 packages/lint-repository-sui/src/rules/cypress-version.js create mode 100644 packages/lint-repository-sui/test/server/cypress-versionSpec.js diff --git a/packages/lint-repository-sui/src/index.js b/packages/lint-repository-sui/src/index.js index 92feca32a..2f6411552 100644 --- a/packages/lint-repository-sui/src/index.js +++ b/packages/lint-repository-sui/src/index.js @@ -1,5 +1,6 @@ const NodeVersion = require('./rules/node-version.js') const ReactVersion = require('./rules/react-version.js') +const CypressVersion = require('./rules/cypress-version.js') const PackageLock = require('./rules/package-lock.js') const GithubAction = require('./rules/github-action.js') const TypeScript = require('./rules/typescript.js') @@ -16,6 +17,7 @@ module.exports = { rules: { 'node-version': NodeVersion, 'react-version': ReactVersion, + 'cypress-version': CypressVersion, 'package-lock': PackageLock, 'github-action': GithubAction, typescript: TypeScript, diff --git a/packages/lint-repository-sui/src/rules/adv-tools-version.js b/packages/lint-repository-sui/src/rules/adv-tools-version.js index d81f3d7a5..ff7f96fd3 100644 --- a/packages/lint-repository-sui/src/rules/adv-tools-version.js +++ b/packages/lint-repository-sui/src/rules/adv-tools-version.js @@ -2,7 +2,8 @@ const dedent = require('string-dedent') const ADV_TOOLS_VERSIONS = { logger: '2', - 'lint-reporters': '1' + 'lint-reporters': '1', + 'vendor-by-consents-loader': '1' } module.exports = { diff --git a/packages/lint-repository-sui/src/rules/cypress-version.js b/packages/lint-repository-sui/src/rules/cypress-version.js new file mode 100644 index 000000000..e01a7b3ec --- /dev/null +++ b/packages/lint-repository-sui/src/rules/cypress-version.js @@ -0,0 +1,64 @@ +const dedent = require('string-dedent') + +const CYPRESS_VERSION = '10' + +module.exports = { + meta: { + type: 'problem', + docs: { + description: 'Check that your repository use the latest Cypress version', + recommended: true, + url: null + }, + fixable: null, + schema: [], + messages: { + badCypressVersion: dedent` + Please be sure that your repository use the latest Cypress Version ${CYPRESS_VERSION}. + Your current version is {{version}}. + If you are not sure about how do it, please contact with Platform Web. + `, + missingCypressDependencie: dedent` + Your project doesnt have installed Cypress. + Please install at least the version ${CYPRESS_VERSION}. + If you are not sure about how do it, please contact with Platform Web. + `, + missingPackageLock: dedent` + To calculate the cypress version first we need to have a package-lock.json in the root + If you are not sure about how do it, please contact with Platform Web. + ` + } + }, + create: function (context) { + return { + 'package-lock.json': matches => { + const [packageLock] = matches + let version = packageLock?.parsed?.packages?.['node_modules/cypress']?.version + + if (!version) { + context.report({ + messageId: 'missingCypressDependencie' + }) + return context.monitoring(0) + } + + version = version.split('.')[0] + + if (version !== CYPRESS_VERSION) { + context.report({ + messageId: 'badCypressVersion', + data: {version} + }) + } + return context.monitoring(version) + }, + + missmatch: key => { + context.report({ + messageId: 'missingPackageLock' + }) + context.monitoring(0) + } + } + } +} diff --git a/packages/lint-repository-sui/src/rules/sui-tools-version.js b/packages/lint-repository-sui/src/rules/sui-tools-version.js index fbd24017d..8473d95a9 100644 --- a/packages/lint-repository-sui/src/rules/sui-tools-version.js +++ b/packages/lint-repository-sui/src/rules/sui-tools-version.js @@ -10,7 +10,9 @@ const SUI_TOOLS_VERSIONS = { pde: '2', test: '8', theme: '8', - domain: '2' + domain: '2', + 'react-web-vitals': '2', + 'segment-wrapper': '3' } module.exports = { diff --git a/packages/lint-repository-sui/test/server/cypress-versionSpec.js b/packages/lint-repository-sui/test/server/cypress-versionSpec.js new file mode 100644 index 000000000..1b2a7356b --- /dev/null +++ b/packages/lint-repository-sui/test/server/cypress-versionSpec.js @@ -0,0 +1,44 @@ +import dedent from 'dedent' + +import handler from '../../src/rules/cypress-version.js' +import {MatchStub, RuleTester} from '../TestHelpers.js' + +RuleTester.create('cypress-version', handler).run({ + valid: [ + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/cypress': {version: '10.0.0'}}}})], + name: 'Cypress 10 installed', + monitoring: '10' + } + ], + invalid: [ + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {}}})], + name: 'Cypress not installed', + report: dedent` + Your project doesnt have installed Cypress. + Please install at least the version 10. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + }, + { + 'package-lock.json': [MatchStub.create({parsed: {packages: {'node_modules/cypress': {version: '17.0.0'}}}})], + name: 'Cypress wrong version', + report: dedent` + Please be sure that your repository use the latest Cypress Version 10. + Your current version is 17. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: '17' + }, + { + missmatch: '', + report: dedent` + To calculate the cypress version first we need to have a package-lock.json in the root + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: 0 + } + ] +}) From e06acb9ca221abc0619cd2f09d1c0468a9aeeb68 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 3 Apr 2024 17:03:09 +0200 Subject: [PATCH 092/103] Update packages/lint-repository-sui/src/rules/sui-tools-version.js Co-authored-by: Jordi --- packages/lint-repository-sui/src/rules/sui-tools-version.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lint-repository-sui/src/rules/sui-tools-version.js b/packages/lint-repository-sui/src/rules/sui-tools-version.js index 8473d95a9..843a7eb56 100644 --- a/packages/lint-repository-sui/src/rules/sui-tools-version.js +++ b/packages/lint-repository-sui/src/rules/sui-tools-version.js @@ -32,7 +32,7 @@ module.exports = { Your current version is {{version}}. If you are not sure about how do it, please contact with Platform Web. `, - missingDependencie: dedent` + missingDependency: dedent` Your project doesnt have installed {{name}}. Please install at least the version {{spectedVersion}}. If you are not sure about how do it, please contact with Platform Web. From c543ae63adfa79e95cc5bdd6245a1741c131e547 Mon Sep 17 00:00:00 2001 From: Carlos Villuendas Zambrana Date: Wed, 3 Apr 2024 17:03:14 +0200 Subject: [PATCH 093/103] Update packages/lint-repository-sui/src/rules/sui-tools-version.js Co-authored-by: Jordi --- packages/lint-repository-sui/src/rules/sui-tools-version.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lint-repository-sui/src/rules/sui-tools-version.js b/packages/lint-repository-sui/src/rules/sui-tools-version.js index 843a7eb56..4c50cb070 100644 --- a/packages/lint-repository-sui/src/rules/sui-tools-version.js +++ b/packages/lint-repository-sui/src/rules/sui-tools-version.js @@ -33,7 +33,7 @@ module.exports = { If you are not sure about how do it, please contact with Platform Web. `, missingDependency: dedent` - Your project doesnt have installed {{name}}. + Your project does not have installed {{name}}. Please install at least the version {{spectedVersion}}. If you are not sure about how do it, please contact with Platform Web. `, From bec81a76ee1438c521687c84ec1ed75ba0c5d807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alvarez?= Date: Thu, 4 Apr 2024 11:02:06 +0200 Subject: [PATCH 094/103] feat(packages/lint-repository-sui): add dependabot rules --- .../src/rules/adv-tools-version.js | 4 +- .../src/rules/structure.js | 32 ++++++++--- .../src/rules/sui-tools-version.js | 2 +- .../test/server/structureSpec.js | 53 ++++++++++++++----- .../test/server/sui-tools-versionSepec.js | 2 +- 5 files changed, 71 insertions(+), 22 deletions(-) diff --git a/packages/lint-repository-sui/src/rules/adv-tools-version.js b/packages/lint-repository-sui/src/rules/adv-tools-version.js index ff7f96fd3..fd573d67c 100644 --- a/packages/lint-repository-sui/src/rules/adv-tools-version.js +++ b/packages/lint-repository-sui/src/rules/adv-tools-version.js @@ -23,7 +23,7 @@ module.exports = { Your current version is {{version}}. If you are not sure about how do it, please contact with Platform Web. `, - missingDependencie: dedent` + missingDependency: dedent` Your project doesnt have installed {{name}}. Please install at least the version {{spectedVersion}}. If you are not sure about how do it, please contact with Platform Web. @@ -44,7 +44,7 @@ module.exports = { if (!version) { context.report({ - messageId: 'missingDependencie', + messageId: 'missingDependency', data: {name, spectedVersion} }) return context.monitoring(false) diff --git a/packages/lint-repository-sui/src/rules/structure.js b/packages/lint-repository-sui/src/rules/structure.js index 729d7a70a..6609b2ab6 100644 --- a/packages/lint-repository-sui/src/rules/structure.js +++ b/packages/lint-repository-sui/src/rules/structure.js @@ -26,12 +26,12 @@ const FILES_AND_FOLDERS = [ 'app/src/index.html', 'app/src/index.(s)?css', 'app/src/routes.(t|j)s(x)?', - 'pacakges/**/src', - 'pacakges/**/test', - 'pacakges/**/Makefile', - 'pacakges/**/package.json', - 'pacakges/**/tsconfig.json', - 'pacakges/**/CHANGELOG.md', + 'packages/**/src', + 'packages/**/test', + 'packages/**/Makefile', + 'packages/**/package.json', + 'packages/**/tsconfig.json', + 'packages/**/CHANGELOG.md', 'deploy/config-(pre|pro).yml', 'deploy/*-(pre|pro)-paas.yml', @@ -56,6 +56,9 @@ module.exports = { The file or folder {{pattern}} is missing. This repository should follow the Golden Path file Structure. If you are not sure about how do it, please contact with Platform Web. + `, + automergeDisabled: dedent` + The dependabot automerge feature is disabled. ` } }, @@ -63,6 +66,23 @@ module.exports = { return { ...Object.fromEntries(FILES_AND_FOLDERS.map(pattern => [pattern, () => context.monitoring(true)])), + '.github/dependabot.yml': matches => { + const [match] = matches + const json = match.parsed + const labels = json?.updates[0]?.labels || [] + const isAutomergeEnabled = labels.find(key => key === 'skynet:merge') + + if (!isAutomergeEnabled) { + context.monitoring(false) + context.report({ + messageId: 'automergeDisabled' + }) + return + } + + context.monitoring(true) + }, + missmatch: key => { context.monitoring(false) context.report({ diff --git a/packages/lint-repository-sui/src/rules/sui-tools-version.js b/packages/lint-repository-sui/src/rules/sui-tools-version.js index 4c50cb070..ac16370b9 100644 --- a/packages/lint-repository-sui/src/rules/sui-tools-version.js +++ b/packages/lint-repository-sui/src/rules/sui-tools-version.js @@ -53,7 +53,7 @@ module.exports = { if (!version) { context.report({ - messageId: 'missingDependencie', + messageId: 'missingDependency', data: {name, spectedVersion} }) return context.monitoring(false) diff --git a/packages/lint-repository-sui/test/server/structureSpec.js b/packages/lint-repository-sui/test/server/structureSpec.js index 78a5f8bbe..b06b76a92 100644 --- a/packages/lint-repository-sui/test/server/structureSpec.js +++ b/packages/lint-repository-sui/test/server/structureSpec.js @@ -1,26 +1,55 @@ import dedent from 'dedent' import handler from '../../src/rules/structure.js' -import {RuleTester} from '../TestHelpers.js' +import {MatchStub, RuleTester} from '../TestHelpers.js' RuleTester.create('structure', handler).run({ - valid: handler.FILES_AND_FOLDERS.map(pattern => { - return { - [pattern]: [], - name: `Testing ${pattern}`, + valid: [ + ...handler.FILES_AND_FOLDERS.map(pattern => { + return { + [pattern]: [], + name: `Testing ${pattern}`, + monitoring: true + } + }), + { + '.github/dependabot.yml': [ + MatchStub.create({parsed: {updates: [{labels: ['skynet:update:rebase', 'skynet:merge']}]}}) + ], + name: 'Dependabot automerge enabled', monitoring: true } - }), - invalid: handler.FILES_AND_FOLDERS.map(pattern => { - return { - missmatch: pattern, - name: `Failed ${pattern}`, - report: dedent` + ], + invalid: [ + ...handler.FILES_AND_FOLDERS.map(pattern => { + return { + missmatch: pattern, + name: `Failed ${pattern}`, + report: dedent` The file or folder ${pattern} is missing. This repository should follow the Golden Path file Structure. If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + } + }), + { + missmatch: '.github/dependabot.yml', + name: 'Dependabot file does not exist', + report: dedent` + The file or folder .github/dependabot.yml is missing. + This repository should follow the Golden Path file Structure. + If you are not sure about how do it, please contact with Platform Web. + `, + monitoring: false + }, + { + '.github/dependabot.yml': [MatchStub.create({parsed: {updates: [{labels: []}]}})], + name: 'Dependabot automerge disabled', + report: dedent` + The dependabot automerge feature is disabled. `, monitoring: false } - }) + ] }) diff --git a/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js b/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js index b283d8094..6e57a0480 100644 --- a/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js +++ b/packages/lint-repository-sui/test/server/sui-tools-versionSepec.js @@ -20,7 +20,7 @@ RuleTester.create('sui-tools-version', handler).run({ 'package-lock.json': [MatchStub.create({parsed: {packages: {}}})], name: `SUI Bundler not installed`, report: dedent` - Your project doesnt have installed bundler. + Your project does not have installed bundler. Please install at least the version ${SUI_TOOLS_ENTRIES[0][1]}. If you are not sure about how do it, please contact with Platform Web. `, From 8081d54fe5f159ed2af348f6ec8e1fbe36bab2b5 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Fri, 5 Apr 2024 08:54:17 +0000 Subject: [PATCH 095/103] release(packages/lint-repository-sui): v1.4.0 [skip ci] --- packages/lint-repository-sui/CHANGELOG.md | 13 +++++++++++++ packages/lint-repository-sui/package.json | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/lint-repository-sui/CHANGELOG.md b/packages/lint-repository-sui/CHANGELOG.md index 1db18171d..437387780 100644 --- a/packages/lint-repository-sui/CHANGELOG.md +++ b/packages/lint-repository-sui/CHANGELOG.md @@ -1,5 +1,18 @@ # CHANGELOG +# 1.4.0 (2024-04-05) + + +### Features + +* Add check for ADV tools version ([4893715](https://github.com/SUI-Components/sui/commit/48937153d2a043df94d362eb49b06bfa2bcd3448)) +* add dependabot rules ([bec81a7](https://github.com/SUI-Components/sui/commit/bec81a76ee1438c521687c84ec1ed75ba0c5d807)) +* add sui-tools-version rule ([7f6f0bf](https://github.com/SUI-Components/sui/commit/7f6f0bf0a7d8ce70e0b936fd1afe524f15ba84a6)) +* Create Repository Structure rule ([f1776ac](https://github.com/SUI-Components/sui/commit/f1776ac105a423d3dc774ffcd869b3bae41d8fbb)) +* Create rule to check the Cypress version ([852df2e](https://github.com/SUI-Components/sui/commit/852df2e4c83039a1acae7a39433f1f8b90afa206)) + + + # 1.3.0 (2024-03-21) diff --git a/packages/lint-repository-sui/package.json b/packages/lint-repository-sui/package.json index 661cf9603..5f5f1835b 100644 --- a/packages/lint-repository-sui/package.json +++ b/packages/lint-repository-sui/package.json @@ -1,7 +1,7 @@ { "name": "lint-repository-sui", "access": "public", - "version": "1.3.0", + "version": "1.4.0", "description": "Set of sui lint rules to lint a repository", "keywords": [], "author": "Sui", From cd2a08e42717c52b2e7ed0cb69aa4e2250602652 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Fri, 5 Apr 2024 08:54:21 +0000 Subject: [PATCH 096/103] release(packages/sui-lint): v4.41.0 [skip ci] --- packages/sui-lint/CHANGELOG.md | 12 ++++++++++++ packages/sui-lint/package.json | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/sui-lint/CHANGELOG.md b/packages/sui-lint/CHANGELOG.md index 94eefb9aa..5b813e887 100644 --- a/packages/sui-lint/CHANGELOG.md +++ b/packages/sui-lint/CHANGELOG.md @@ -1,5 +1,17 @@ # CHANGELOG +# 4.41.0 (2024-04-05) + + +### Features + +* add sui-tools-version rule ([1932c1d](https://github.com/SUI-Components/sui/commit/1932c1dc795a450b88573ff381aa04a8d9fc5262)) +* enable check for cypress version ([a4bbed9](https://github.com/SUI-Components/sui/commit/a4bbed90ded5d414ce52e02b677e2a79fb0fc4f8)) +* enable repository structure rule ([8dd7c14](https://github.com/SUI-Components/sui/commit/8dd7c14e4a63d36403e6c4c775a39d424d1c3b33)) +* enable test for ADV packages version ([6394545](https://github.com/SUI-Components/sui/commit/6394545857204b80f8e6f80d5abaa77899f63400)) + + + # 4.40.0 (2024-03-21) diff --git a/packages/sui-lint/package.json b/packages/sui-lint/package.json index c5cbff008..053c305ff 100644 --- a/packages/sui-lint/package.json +++ b/packages/sui-lint/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/lint", - "version": "4.40.0", + "version": "4.41.0", "description": "Linting CLI for sui packages", "main": "./bin/sui-lint.js", "bin": { From c7e252499ada037c4c2d887863378cde192e1c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alvarez?= Date: Fri, 5 Apr 2024 11:43:44 +0200 Subject: [PATCH 097/103] fix(packages/sui-ssr): internal error --- packages/sui-ssr/server/hooksFactory/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sui-ssr/server/hooksFactory/index.js b/packages/sui-ssr/server/hooksFactory/index.js index 6910b9883..aaa8da2e5 100644 --- a/packages/sui-ssr/server/hooksFactory/index.js +++ b/packages/sui-ssr/server/hooksFactory/index.js @@ -133,7 +133,7 @@ export const hooksFactory = async () => { [TYPES.NOT_FOUND]: async (req, res) => { res.status(NOT_FOUND_CODE).send(await getStaticErrorPageContent(NOT_FOUND_CODE, req)) }, - [TYPES.INTERNAL_ERROR]: async (err, req, res) => { + [TYPES.INTERNAL_ERROR]: async (err, req, res, next) => { // getInitialProps could throw a 404 error or any other error req.log && req.log.error && req.log.error((' ' + err.message).slice(1)) const status = @@ -144,7 +144,7 @@ export const hooksFactory = async () => { res.status(status) } - res.end(await getStaticErrorPageContent(status, req)) + res.send(await getStaticErrorPageContent(status, req)) }, ..._userHooks } From 27ae05b0a335f2ca146bc5d46d2f39a394d54234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alvarez?= Date: Fri, 5 Apr 2024 11:59:20 +0200 Subject: [PATCH 098/103] fix(packages/sui-ssr): copy ssr dev 404 and 500 html --- packages/sui-ssr/bin/sui-ssr-dev.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/sui-ssr/bin/sui-ssr-dev.js b/packages/sui-ssr/bin/sui-ssr-dev.js index 5650c4da1..2b2cc6e9b 100644 --- a/packages/sui-ssr/bin/sui-ssr-dev.js +++ b/packages/sui-ssr/bin/sui-ssr-dev.js @@ -3,6 +3,7 @@ const program = require('commander') const {exec} = require('child_process') +const {copyFile} = require('fs/promises') const path = require('path') const fs = require('fs') const express = require('express') @@ -19,6 +20,8 @@ const log = require('@s-ui/bundler/shared/log.js') const serverConfigFactory = require('../compiler/server.js') const TMP_PATH = '.sui' +const SRC_PATH = path.join(process.cwd(), 'src') +const PUBLIC_OUTPUT_PATH = path.join(process.cwd(), `${TMP_PATH}/server`) const SERVER_OUTPUT_PATH = path.join(process.cwd(), `${TMP_PATH}/server`) const STATICS_PATH = path.join(process.cwd(), './statics') const STATICS_OUTPUT_PATH = path.join(process.cwd(), `${TMP_PATH}/statics`) @@ -63,6 +66,13 @@ const linkStatics = () => { ) } +const copyStatics = () => { + return Promise.allSettled([ + copyFile(path.join(SRC_PATH, '404.html'), path.join(PUBLIC_OUTPUT_PATH, '404.html')), + copyFile(path.join(SRC_PATH, '500.html'), path.join(PUBLIC_OUTPUT_PATH, '500.html')) + ]) +} + const initMSW = () => { return exec(`npx msw init ${STATICS_PATH}`) } @@ -153,7 +163,13 @@ const start = async ({packagesToLink, linkAll}) => { fs.mkdirSync(TMP_PATH) } - Promise.all([linkStatics(), initMSW(), compile('client', clientCompiler), compile('server', serverCompiler)]) + Promise.all([ + linkStatics(), + initMSW(), + copyStatics(), + compile('client', clientCompiler), + compile('server', serverCompiler) + ]) .then(() => { const script = nodemon({ script: `${SERVER_OUTPUT_PATH}/index.js`, From b692c3ead33bb81198e15bd69ff01b3a6a5a4fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alvarez?= Date: Fri, 5 Apr 2024 12:08:29 +0200 Subject: [PATCH 099/103] fix(packages/sui-ssr): copy files --- packages/sui-ssr/bin/sui-ssr-dev.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/sui-ssr/bin/sui-ssr-dev.js b/packages/sui-ssr/bin/sui-ssr-dev.js index 2b2cc6e9b..305a92699 100644 --- a/packages/sui-ssr/bin/sui-ssr-dev.js +++ b/packages/sui-ssr/bin/sui-ssr-dev.js @@ -3,7 +3,7 @@ const program = require('commander') const {exec} = require('child_process') -const {copyFile} = require('fs/promises') +const {cp} = require('fs/promises') const path = require('path') const fs = require('fs') const express = require('express') @@ -21,7 +21,7 @@ const serverConfigFactory = require('../compiler/server.js') const TMP_PATH = '.sui' const SRC_PATH = path.join(process.cwd(), 'src') -const PUBLIC_OUTPUT_PATH = path.join(process.cwd(), `${TMP_PATH}/server`) +const PUBLIC_OUTPUT_PATH = path.join(process.cwd(), `${TMP_PATH}/public`) const SERVER_OUTPUT_PATH = path.join(process.cwd(), `${TMP_PATH}/server`) const STATICS_PATH = path.join(process.cwd(), './statics') const STATICS_OUTPUT_PATH = path.join(process.cwd(), `${TMP_PATH}/statics`) @@ -68,8 +68,8 @@ const linkStatics = () => { const copyStatics = () => { return Promise.allSettled([ - copyFile(path.join(SRC_PATH, '404.html'), path.join(PUBLIC_OUTPUT_PATH, '404.html')), - copyFile(path.join(SRC_PATH, '500.html'), path.join(PUBLIC_OUTPUT_PATH, '500.html')) + cp(path.join(SRC_PATH, '404.html'), path.join(PUBLIC_OUTPUT_PATH, '404.html'), {recursive: true}), + cp(path.join(SRC_PATH, '500.html'), path.join(PUBLIC_OUTPUT_PATH, '500.html'), {recursive: true}) ]) } From 5ceed50f0b30aa75d9be4b35008965aab0bed8a4 Mon Sep 17 00:00:00 2001 From: sui-bot Date: Fri, 5 Apr 2024 10:17:43 +0000 Subject: [PATCH 100/103] release(packages/sui-ssr): v8.26.0 [skip ci] --- packages/sui-ssr/CHANGELOG.md | 14 ++++++++++---- packages/sui-ssr/package.json | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/sui-ssr/CHANGELOG.md b/packages/sui-ssr/CHANGELOG.md index cabb5cf97..b819a8032 100644 --- a/packages/sui-ssr/CHANGELOG.md +++ b/packages/sui-ssr/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 8.26.0 (2024-04-05) + + +### Bug Fixes + +* internal error ([c7e2524](https://github.com/SUI-Components/sui/commit/c7e252499ada037c4c2d887863378cde192e1c24)) + + + # 8.25.0 (2024-01-11) @@ -1534,7 +1543,4 @@ ### Features -* **sui-ssr:** add package.json ([d1495ce](https://github.com/SUI-Components/sui/commit/d1495ce76b02d906ca512f59518737411a67acc1)), closes [#67](https://github.com/SUI-Components/sui/issues/67) - - - +* **sui-ssr:** add package.json ([d1495ce](https://github.com/SUI-Components/sui/commit/d1495ce76b02d906ca512f59518737411a67acc1)), closes [#67](https://github.com/SUI-Components/sui/issues/67) \ No newline at end of file diff --git a/packages/sui-ssr/package.json b/packages/sui-ssr/package.json index a70c4fb6f..77c4cc08f 100644 --- a/packages/sui-ssr/package.json +++ b/packages/sui-ssr/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/ssr", - "version": "8.25.0", + "version": "8.26.0", "description": "> Plug SSR to you SUI SPA.", "main": "index.js", "bin": { From eb6082382ccd51825f2c0f0aca3118df1f2800df Mon Sep 17 00:00:00 2001 From: sui-bot Date: Fri, 5 Apr 2024 10:23:22 +0000 Subject: [PATCH 101/103] release(packages/sui-ssr): v8.27.0 [skip ci] --- packages/sui-ssr/CHANGELOG.md | 10 ++++++++++ packages/sui-ssr/package.json | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/sui-ssr/CHANGELOG.md b/packages/sui-ssr/CHANGELOG.md index b819a8032..16fa12f6b 100644 --- a/packages/sui-ssr/CHANGELOG.md +++ b/packages/sui-ssr/CHANGELOG.md @@ -1,5 +1,15 @@ # CHANGELOG +# 8.27.0 (2024-04-05) + + +### Bug Fixes + +* copy files ([b692c3e](https://github.com/SUI-Components/sui/commit/b692c3ead33bb81198e15bd69ff01b3a6a5a4fcb)) +* copy ssr dev 404 and 500 html ([27ae05b](https://github.com/SUI-Components/sui/commit/27ae05b0a335f2ca146bc5d46d2f39a394d54234)) + + + # 8.26.0 (2024-04-05) diff --git a/packages/sui-ssr/package.json b/packages/sui-ssr/package.json index 77c4cc08f..06a335303 100644 --- a/packages/sui-ssr/package.json +++ b/packages/sui-ssr/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/ssr", - "version": "8.26.0", + "version": "8.27.0", "description": "> Plug SSR to you SUI SPA.", "main": "index.js", "bin": { From 566fea3824726a3ce1ec0db9d5a612ea9da7d6e8 Mon Sep 17 00:00:00 2001 From: Giorgio de la Barrera Date: Fri, 19 Apr 2024 11:26:47 +0200 Subject: [PATCH 102/103] feat(packages/sui-studio): add __tests__ to npmignore --- packages/sui-studio/bin/sui-studio-generate.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/sui-studio/bin/sui-studio-generate.js b/packages/sui-studio/bin/sui-studio-generate.js index 186aabac7..53d99dc90 100755 --- a/packages/sui-studio/bin/sui-studio-generate.js +++ b/packages/sui-studio/bin/sui-studio-generate.js @@ -168,6 +168,7 @@ node_modules` demo src test +__tests__ ` ), From b8092855ade35401b2c0cc58dd93f6b7e32a27ff Mon Sep 17 00:00:00 2001 From: sui-bot Date: Fri, 19 Apr 2024 09:52:02 +0000 Subject: [PATCH 103/103] release(packages/sui-studio): v11.40.0 [skip ci] --- packages/sui-studio/CHANGELOG.md | 14 ++++++++++---- packages/sui-studio/package.json | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/sui-studio/CHANGELOG.md b/packages/sui-studio/CHANGELOG.md index 20df622b1..aff8d0526 100644 --- a/packages/sui-studio/CHANGELOG.md +++ b/packages/sui-studio/CHANGELOG.md @@ -1,5 +1,14 @@ # CHANGELOG +# 11.40.0 (2024-04-19) + + +### Features + +* add __tests__ to npmignore ([566fea3](https://github.com/SUI-Components/sui/commit/566fea3824726a3ce1ec0db9d5a612ea9da7d6e8)) + + + # 11.39.0 (2024-01-15) @@ -2628,7 +2637,4 @@ ### BREAKING CHANGES -* **sui-studio:** API is now named sui-studio instead of suistudio - - - +* **sui-studio:** API is now named sui-studio instead of suistudio \ No newline at end of file diff --git a/packages/sui-studio/package.json b/packages/sui-studio/package.json index 4e597cfb4..99e9edc45 100644 --- a/packages/sui-studio/package.json +++ b/packages/sui-studio/package.json @@ -1,6 +1,6 @@ { "name": "@s-ui/studio", - "version": "11.39.0", + "version": "11.40.0", "description": "Develop, maintain and publish your SUI components.", "main": "index.js", "bin": {