Skip to content

Commit

Permalink
feat(packages/eslint-plugin-sui): Add rule to mark any uso off common…
Browse files Browse the repository at this point in the history
…js syntax in our code
  • Loading branch information
carlosvillu committed Apr 23, 2024
1 parent eb60823 commit f210c46
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 47 deletions.
4 changes: 3 additions & 1 deletion packages/eslint-plugin-sui/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const FactoryPattern = require('./rules/factory-pattern.js')
const SerializeDeserialize = require('./rules/serialize-deserialize.js')
const CommonJS = require('./rules/commonjs.js')

// ------------------------------------------------------------------------------
// Plugin Definition
Expand All @@ -9,6 +10,7 @@ const SerializeDeserialize = require('./rules/serialize-deserialize.js')
module.exports = {
rules: {
'factory-pattern': FactoryPattern,
'serialize-deserialize': SerializeDeserialize
'serialize-deserialize': SerializeDeserialize,
commonjs: CommonJS
}
}
110 changes: 110 additions & 0 deletions packages/eslint-plugin-sui/src/rules/commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @fileoverview Ensure your code is not using CommonJS signatures like module.exports or moduel.exports.foo or require() or require.resolve()
*/
'use strict'

const dedent = require('string-dedent')

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

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Ensure that your code is using ems over commonjs modules',
recommended: true,
url: 'https://github.mpi-internal.com/scmspain/es-td-agreements/blob/master/30-Frontend/00-agreements'
},
fixable: null,
schema: [],
messages: {
forbiddenExports: dedent`
Use module.* should be avoid.
`,
forbiddenRequires: dedent`
Use require function should be avoid.
`,
forbiddenModuleRequire: dedent`
Use module.require function should be avoid.
`,
forbiddenRequiresObjects: dedent`
Use require.cache or require.extensions or require.main should be avoid.
`,
forbiddenRequireResolve: dedent`
Use require.resolve function should be avoid.
`,
forbidden__filename: dedent`
__filename should be avoid
`,
forbidden__dirname: dedent`
__dirname should be avoid
`
}
},
create: function (context) {
return {
CallExpression(node) {
const isRequire = node.callee?.name === 'require'
const isResolve = node.callee?.object?.name === 'require' && node.callee?.property?.name === 'resolve'
const isModule = node.callee?.object?.name === 'module' && node.callee?.property?.name === 'require'

isRequire &&
context.report({
node,
messageId: 'forbiddenRequires'
})

isResolve &&
context.report({
node,
messageId: 'forbiddenRequireResolve'
})

isModule &&
context.report({
node,
messageId: 'forbiddenModuleRequire'
})
},
MemberExpression(node) {
const isModule =
node.object?.name === 'module' &&
['children', 'exports', 'filename', 'id', 'isPreloading', 'loaded', 'parent', 'path', 'paths'].some(
property => node.property?.name === property
)

const isRequire =
node.object?.name === 'require' &&
['cache', 'extensions', 'main'].some(property => node.property?.name === property)

isModule &&
context.report({
node,
messageId: 'forbiddenExports'
})

isRequire &&
context.report({
node,
messageId: 'forbiddenRequiresObjects'
})
},
Identifier(node) {
node.name === '__filename' &&
context.report({
node,
messageId: 'forbidden__filename'
})

node.name === '__dirname' &&
context.report({
node,
messageId: 'forbidden__dirname'
})
}
}
}
}
46 changes: 0 additions & 46 deletions packages/eslint-plugin-sui/src/rules/forbidden-require.js

This file was deleted.

141 changes: 141 additions & 0 deletions packages/eslint-plugin-sui/test/server/commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import dedent from 'dedent'
import {RuleTester} from 'eslint'

import rule from '../../src/rules/commonjs.js'

// ------------------------------------------------------------------------------
// Tests
// more info: https://eslint.org/docs/latest/integrate/nodejs-api#ruletester
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 2018, sourceType: 'module'}})
ruleTester.run('esm', rule, {
valid: [
{
code: dedent`
class User {
static create() { return new User() }
}
`
}
],

invalid: [
{
code: dedent`
class Model {
constructor() { this.name = 'John Doe' }
}
module.exports = Model
`,
errors: [
{
message: dedent`
Use module.* should be avoid.
`
}
]
},
{
code: dedent`
const deps = require('my-dep')
class Config {
static create() {
return {API_URL: 'google.com'}
}
}
`,
errors: [
{
message: dedent`
Use require function should be avoid.
`
}
]
},
{
code: dedent`
const path = require.resolve('my-dep')
`,
errors: [
{
message: dedent`
Use require.resolve function should be avoid.
`
}
]
},
{
code: dedent`
console.log(require.main)
`,
errors: [
{
message: dedent`
Use require.cache or require.extensions or require.main should be avoid.
`
}
]
},
{
code: dedent`
console.log(require.cache)
`,
errors: [
{
message: dedent`
Use require.cache or require.extensions or require.main should be avoid.
`
}
]
},
{
code: dedent`
console.log(require.extensions)
`,
errors: [
{
message: dedent`
Use require.cache or require.extensions or require.main should be avoid.
`
}
]
},
{
code: dedent`
console.log(__dirname)
`,
errors: [
{
message: dedent`
__dirname should be avoid
`
}
]
},
{
code: dedent`
console.log(__filename)
`,
errors: [
{
message: dedent`
__filename should be avoid
`
}
]
},
{
code: dedent`
module.require(id)
`,
errors: [
{
message: dedent`
Use module.require function should be avoid.
`
}
]
}
]
})

0 comments on commit f210c46

Please sign in to comment.