Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eslint : no-restricted-syntax for specific require function calls

Current platform which we are using just moved from proprietary ECMAScript based language to Javascript.

In the proprietary language there was Arraylist class which we could use apart from native JS array. As now we have completely moved to native JS I would like to generate a ESLINT warning whenever someone tried to use Arraylist.

I read about "no-restricted-syntax" rule in Eslint and tried creating a rule for the calls like below in my code

require( 'dw/util/ArrayList' )

I came up with below syntax but this doesn't work. I even tried arguments[0].value instead of arguments.value

"no-restricted-syntax": [
    "warn",
    {
        "selector": "CallExpression[callee.name='require'][arguments.value='dw/util/ArrayList']",
        "message": "Please use native JS Array"
    }
]

I used ESLint Parser Demo to check the syntax tree which looks like below

    {
  "type": "Program",
  "start": 0,
  "end": 30,
  "body": [
    {
      "type": "ExpressionStatement",
      "start": 0,
      "end": 30,
      "expression": {
        "type": "CallExpression",
        "start": 0,
        "end": 30,
        "callee": {
          "type": "Identifier",
          "start": 0,
          "end": 7,
          "name": "require"
        },
        "arguments": [
          {
            "type": "Literal",
            "start": 9,
            "end": 28,
            "value": "dw/util/ArrayList",
            "raw": "'dw/util/ArrayList'"
          }
        ]
      }
    }
  ],
  "sourceType": "module"
}

Can someone help me with the correct syntax.

like image 924
RanRag Avatar asked Nov 18 '25 01:11

RanRag


1 Answers

The correct syntax shall be:

[arguments.0.value='dw/util/ArrayList']
like image 185
Radi Mortada Avatar answered Nov 20 '25 14:11

Radi Mortada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!