RanRag
RanRag

Reputation: 49547

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.

Upvotes: 3

Views: 2012

Answers (2)

jornathan
jornathan

Reputation: 856

This is work for me,

enter image description here

"no-restricted-syntax": [
  "warn",          
  {
    "selector": "Literal[value='dw/util/ArrayList']",
    "message": "Please use native JS Array"
  }
],

Upvotes: 0

Radi Mortada
Radi Mortada

Reputation: 164

The correct syntax shall be:

[arguments.0.value='dw/util/ArrayList']

Upvotes: 2

Related Questions