Skip to content

Commit

Permalink
feat(packages/eslint-plugin-sui): Allow require from ESM module
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvillu committed Apr 25, 2024
1 parent 86ef4a2 commit ea666e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/eslint-plugin-sui/src/rules/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ module.exports = {
const isResolve = node.callee?.object?.name === 'require' && node.callee?.property?.name === 'resolve'
const isModule = node.callee?.object?.name === 'module' && node.callee?.property?.name === 'require'

const isRequireFormCreateRequire = node.parent?.parent?.body
?.filter(node => node.type === 'ImportDeclaration')
?.some(
node =>
node.source?.value === 'module' && node.specifiers?.some(spec => spec.imported?.name === 'createRequire')
)

isRequire &&
!isRequireFormCreateRequire &&
context.report({
node,
messageId: 'forbiddenRequires'
Expand Down
17 changes: 16 additions & 1 deletion packages/eslint-plugin-sui/test/server/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@ import {RuleTester} from 'eslint'

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

const resolvedBabelPresetSui = require.resolve('babel-preset-sui')
const parser = require.resolve('@babel/eslint-parser')

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

const ruleTester = new RuleTester({parserOptions: {ecmaVersion: 2018, sourceType: 'module'}})
const ruleTester = new RuleTester({parser, parserOptions: {babelOptions: {configFile: resolvedBabelPresetSui}}})
ruleTester.run('esm', rule, {
valid: [
{
code: dedent`
class User {
static create() { return new User() }
}
`
},
{
code: dedent`
import { createRequire } from "module"
const require = createRequire(import.meta.url)
require('whatever')
class User {
static create() { return new User() }
}
Expand Down

0 comments on commit ea666e0

Please sign in to comment.