-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(packages/eslint-plugin-sui): Add rule to check relative imports #1760
Merged
carlosvillu
merged 8 commits into
master
from
sui-lint-add-fitness-board-rules-part-iii
May 10, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
07cd08b
feat(packages/eslint-plugin-sui): Add rule to check relative imports
carlosvillu 5906840
feat(packages/eslint-plugin-sui): several fixes
carlosvillu 7a7df02
feat(packages/lint-repository-sui): Create a metric about how many ts…
carlosvillu a0d017f
feat(packages/lint-repository-sui): remove noise
carlosvillu 1b21293
feat(packages/lint-repository-sui): Add rules to check sass and spark…
carlosvillu be93c08
feat(packages/lint-repository-sui): Add rule to detect how many compo…
carlosvillu 07062f2
refactor(packages/lint-repository-sui): remove debugger
carlosvillu 1d7757d
Update packages/eslint-plugin-sui/package.json
carlosvillu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/eslint-plugin-sui/src/rules/layers-architecture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @fileoverview Make sure to avoid direct file imports from other packages within your monorepo. | ||
* */ | ||
'use strict' | ||
|
||
const dedent = require('string-dedent') | ||
const {Monorepo} = require('../utils/monorepo.js') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Make sure to avoid direct file imports from other packages within your monorepo', | ||
recommended: true, | ||
url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
forbiddenRelativeImports: dedent` | ||
When using a package from your monorepo, import it directly instead of using a relative path. | ||
` | ||
} | ||
}, | ||
create: function (context) { | ||
const monorepo = Monorepo.create(context.cwd) | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
const isForbidden = monorepo.isPackage(context.filename, node.source.value) | ||
|
||
isForbidden && | ||
context.report({ | ||
node, | ||
messageId: 'forbiddenRelativeImports' | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const path = require('node:path') | ||
const fg = require('fast-glob') | ||
|
||
let instance | ||
|
||
class MonoRepo { | ||
static create(root) { | ||
if (instance) return instance | ||
|
||
instance = new MonoRepo(root) | ||
return instance | ||
} | ||
|
||
constructor(root) { | ||
const rootPackageJSON = require(path.join(root, 'package.json')) | ||
const innerPatternPackagesJSON = rootPackageJSON.workspaces?.map(workspace => path.join(workspace, 'package.json')) | ||
this._packages = innerPatternPackagesJSON ? fg.sync(innerPatternPackagesJSON, {deep: 3}) : [] | ||
this._root = root | ||
} | ||
|
||
get packages() { | ||
return this._packages | ||
} | ||
|
||
get root() { | ||
return this._root | ||
} | ||
|
||
belongSamePackage(filePath, relativeImport) { | ||
return ( | ||
path.normalize(filePath)?.replace(this.root, '')?.split('/')?.at(1) === | ||
path | ||
.normalize(path.dirname(filePath) + '/' + relativeImport) | ||
?.replace(this.root, '') | ||
?.split('/') | ||
?.at(1) | ||
) | ||
} | ||
|
||
isPackage(filePath, relativeImport) { | ||
if (!relativeImport.startsWith('../')) return false | ||
if (this.belongSamePackage(filePath, relativeImport)) return false | ||
|
||
const pkgName = path | ||
.normalize(path.dirname(filePath) + '/' + relativeImport) | ||
?.replace(this.root, '') | ||
?.replace(/(lib|src)\/.*/, 'package.json') | ||
?.replace('/', '') | ||
|
||
return this.packages.includes(pkgName) | ||
} | ||
} | ||
|
||
module.exports.Monorepo = MonoRepo |
67 changes: 67 additions & 0 deletions
67
packages/eslint-plugin-sui/test/server/layers-architecture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import dedent from 'dedent' | ||
import {RuleTester} from 'eslint' | ||
import sinon from 'sinon' | ||
|
||
import rule from '../../src/rules/layers-architecture.js' | ||
import {Monorepo} from '../../src/utils/monorepo.js' | ||
|
||
const resolvedBabelPresetSui = require.resolve('babel-preset-sui') | ||
const parser = require.resolve('@babel/eslint-parser') | ||
|
||
const CWD = '/Users/carlosvillu/Developer/frontend-mt--web-app' | ||
|
||
const ruleTester = new RuleTester({parser, parserOptions: {babelOptions: {configFile: resolvedBabelPresetSui}}}) | ||
describe('layersArch valid', function () { | ||
beforeEach(function () { | ||
this.getterPackagesStub = sinon.stub(Monorepo.prototype, 'packages').get(() => ['domain/package.json']) | ||
this.getterRootStub = sinon.stub(Monorepo.prototype, 'root').get(() => CWD) | ||
}) | ||
afterEach(function () { | ||
this.getterPackagesStub.restore() | ||
this.getterRootStub.restore() | ||
}) | ||
|
||
ruleTester.run('layersArch', rule, { | ||
valid: [ | ||
{ | ||
filename: CWD + '/components/animation/fadeOut/demo/context.js', | ||
code: dedent` | ||
import DomainBuilder from 'studio-utils/DomainBuilder' | ||
|
||
class User { | ||
static create() { return new User() } | ||
} | ||
` | ||
}, | ||
{ | ||
filename: CWD + '/components/animation/fadeOut/demo/context.js', | ||
code: dedent` | ||
import { createRequire } from "module" | ||
|
||
class User { | ||
static create() { return new User() } | ||
} | ||
` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: CWD + '/components/animation/fadeOut/demo/context.js', | ||
code: dedent` | ||
import {Coches as Domain} from '../../../../domain/lib/index.js' | ||
|
||
class Model { | ||
constructor() { this.name = 'John Doe' } | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
message: dedent` | ||
When using a package from your monorepo, import it directly instead of using a relative path. | ||
` | ||
} | ||
] | ||
} | ||
] | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/lint-repository-sui/src/rules/components-location.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const dedent = require('string-dedent') | ||
|
||
const handler = (context, matches) => { | ||
const badComponents = matches | ||
.filter(match => match.path.match(/src\/pages\/.*/)) | ||
.filter(match => !match.path.match(/src\/pages\/\w+\/index\.(j|t)s(x)?/)) | ||
.filter(match => match.raw.match(/(?<tag><\w+\s*.*>)|(?<fragment><>)|(?<react>react)/)).length | ||
|
||
context.report({ | ||
messageId: 'number', | ||
data: {number: badComponents} | ||
}) | ||
return context.monitoring(badComponents) | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'This metric reports how many component live outside of our Studios.', | ||
recommended: true, | ||
url: null | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
number: dedent` | ||
Currently, your project has {{number}} component living outside of your SUI-Studio. | ||
Try to move all those component to your \`packages/ui/components\` folder. | ||
` | ||
} | ||
}, | ||
create: function (context) { | ||
return { | ||
'app/src/pages/**/*.(j|t)s(x)?': handler.bind(undefined, context), | ||
'src/**/*.(j|t)s(x)?': handler.bind(undefined, context) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const dedent = require('string-dedent') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'This metric reports the number of sass files in your repository', | ||
recommended: true, | ||
url: null | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
percentage: dedent` | ||
Currently, your project has {{number}} sass files. | ||
We should remove as many sass files as we can | ||
` | ||
} | ||
}, | ||
create: function (context) { | ||
return { | ||
'**/*.scss': matches => { | ||
context.report({ | ||
messageId: 'percentage', | ||
data: {number: matches.length} | ||
}) | ||
return context.monitoring(matches.length) | ||
}, | ||
|
||
missmatch: key => { | ||
context.report({ | ||
messageId: 'percentage', | ||
data: {percentage: 0} | ||
}) | ||
context.monitoring(0) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the new
/foo/bar
? 😉