This plugin contains rules that help enforce ServiceNow best practices. It also guards against using unsupported JavaScript features, such as Promises, async/await, and BigInt (see: JavaScript engine feature support).
You'll first need to install ESLint:
npm i eslint --save-dev
Next, install eslint-plugin-servicenow
:
npm install eslint-plugin-servicenow --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must also install eslint-plugin-servicenow
globally.
Add servicenow
to the plugins section of your .eslintrc
configuration file. You can omit the eslint-plugin-
prefix:
{
"plugins": [
"servicenow"
]
}
To enable the recommended rules, extend your .eslintrc:
{
"extends": ["plugin:servicenow/recommended"]
}
Specify individual rules under the rules section:
{
"rules": {
"servicenow/no-hardcoded-sysids": "warn",
"servicenow/no-at-method": "warn",
"servicenow/no-promise": "warn",
"servicenow/no-weak-references": "warn",
...
}
}
Warns against hardcoded sys_ids.
var id = "0329a956a9bb0000b0a7619be1050e41";
var id = getSysID();
Warns against using the .at()
method as it's not fully supported by ServiceNow.
arr.at(1);
typedArr.at(1);
'hello'.at(1);
'hello'.charAt(1);
arr[1];
arr.slice(1,2);
Warns against using Promises.
new Promise((resolve, reject) => {});
Warns against weak references.
new WeakMap();
new Map();
Warns against using async/await syntax.
async function myFunc() {
await someFunction();
}
Warns against using async iterators.
****#### Invalid:
for await (let item of asyncIterable) {}
for (let item of iterable) {}
Warns against using BigInt.
const bigInt = 123n;
const number = 123;
Warns against using Date.prototype.toJSON()
.
const date = new Date();
const jsonDate = date.toJSON();
const date = new Date();
const stringDate = date.toString();
Disallows certain package calls.
const result = Packages.com.glide.Glide.someMethod();
const result = Glide.someMethod();
Disallows the use of private class methods.
class MyClass {
#myPrivateMethod() {}
}
class MyClass {
myPublicMethod() {}
}
Warns against internal proxy calls.
const p = new Proxy(target, handler);
const obj = { prop: value };
Warns against using RegExp lookbehind assertions.
const regex = /(?<=@)\\w+/;
const regex = /@\\w+/;
Disallows the use of Object.setPrototypeOf
.
Object.setPrototypeOf(obj, prototype);
const newObj = Object.create(prototype);
Disallows the use of shared memory and atomics.
Atomics.add(sharedArray, index, value);
array[index] = value;
Disallows the use of Typed Arrays and DataView methods for typed arrays.
const int8 = new Int8Array();
const data = new DataView(buffer);
data.getInt8(0);
const regularArray = [1, 2, 3];
const obj = { byte: 8 };