简体   繁体   中英

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.

正确的语法应该是:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM