Skip to content

Commit

Permalink
fix: browser compatibility checks (hyperlane-xyz#5288)
Browse files Browse the repository at this point in the history
### Description

This PR enhances the ESLint configuration to restrict the usage of
Node.js built-in modules across the codebase. This is particularly
important for the SDK package which needs to maintain browser
compatibility for frontend integrations.

Key changes:
- Added comprehensive ESLint rules to prevent usage of Node.js specific
modules (fs, path, crypto, etc.)
- Updated error messages to guide developers towards
environment-agnostic alternatives. (alternatives can be mentioned later
on)
- Created a separate ESLint config for the Solidity package
- Excluded test files and AWS-related code from these restrictions

### Drive-by changes

- Added ESLint dependency to solidity/package.json
- Removed redundant eslint-disable comments from test files
- Updated lint script in solidity/package.json to use the new config

### Related issues

hyperlane-xyz#5277

### Backward compatibility

Yes. These changes only affect development-time linting and do not
impact runtime behavior.

### Testing

Manual testing:
- Verified ESLint rules correctly flag Node.js built-in module imports
- Confirmed test files in excluded directories are not affected
- Validated lint commands work as expected in both root and solidity
packages

---------

Co-authored-by: Morteza Shojaei <[email protected]>
  • Loading branch information
mshojaei-txfusion and mortezashojaei authored Jan 31, 2025
1 parent bed81f7 commit ba50e62
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/red-games-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperlane-xyz/sdk': patch
'@hyperlane-xyz/core': patch
---

Added ESLint configuration and dependency to enforce Node.js module restrictions
98 changes: 94 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ export default [
name: 'console',
message: 'Please use a logger and/or the utils package assert',
},
{
name: 'fs',
message: 'Avoid use of node-specific libraries',
},
],

'@typescript-eslint/ban-ts-comment': ['off'],
Expand All @@ -112,4 +108,98 @@ export default [
'jest/valid-expect': 'error',
},
},

{
files: ['**/*.ts', '**/*.js', '**/*.mjs'],
ignores: ['**/aws/**/*', '**/test/**/*', '**/*.test.ts'],
rules: {
'no-restricted-imports': [
'error',
{
paths: [
{
name: 'fs',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'path',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'child_process',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'os',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'process',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'http',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'https',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'net',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'dgram',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'dns',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'crypto',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'tls',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'cluster',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'stream',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'vm',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
{
name: 'readline',
message:
'Avoid Node.js built-in modules in cross-platform code. Use environment-agnostic alternatives.',
},
],
},
],
},
},
];
12 changes: 12 additions & 0 deletions solidity/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import MonorepoDefaults from '../eslint.config.mjs';

export default [
...MonorepoDefaults,
{
ignores: [
'./test/**/*',
'./dist/**/*',
'.solcover.js',
],
},
];
1 change: 1 addition & 0 deletions solidity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@typechain/hardhat": "^9.1.0",
"@types/node": "^18.14.5",
"chai": "^4.5.0",
"eslint": "^9.15.0",
"ethereum-waffle": "^4.0.10",
"ethers": "^5.7.2",
"hardhat": "^2.22.2",
Expand Down
1 change: 0 additions & 1 deletion typescript/sdk/src/ism/metadata/aggregation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-restricted-imports */
import { expect } from 'chai';
import { ethers } from 'ethers';
import { existsSync, readFileSync, readdirSync } from 'fs';
Expand Down
1 change: 0 additions & 1 deletion typescript/sdk/src/ism/metadata/multisig.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-restricted-imports */
import { expect } from 'chai';
import { existsSync, readFileSync, readdirSync } from 'fs';

Expand Down
1 change: 0 additions & 1 deletion typescript/sdk/src/warp/WarpCore.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-restricted-imports */
import { expect } from 'chai';
import fs from 'fs';
import sinon from 'sinon';
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7379,6 +7379,7 @@ __metadata:
"@typechain/hardhat": "npm:^9.1.0"
"@types/node": "npm:^18.14.5"
chai: "npm:^4.5.0"
eslint: "npm:^9.15.0"
ethereum-waffle: "npm:^4.0.10"
ethers: "npm:^5.7.2"
fx-portal: "npm:^1.0.3"
Expand Down

0 comments on commit ba50e62

Please sign in to comment.