-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(packages/eslint-plugin-sui): Add rule to mark any uso off common…
…js syntax in our code
- Loading branch information
1 parent
eb60823
commit f210c46
Showing
4 changed files
with
254 additions
and
47 deletions.
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
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' | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,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. | ||
` | ||
} | ||
] | ||
} | ||
] | ||
}) |