@quintaaa/eslint-plugin-starlims
Version:
Eslint plugin to parse and lint starlims form code successfully
113 lines (111 loc) • 4.47 kB
JavaScript
const RuleTester = require('eslint').RuleTester;
const rule = require('../../src/rules/requests-in-loop');
const ruleTester = new RuleTester({
languageOptions: {
ecmaVersion: 2022,
},
});
ruleTester.run('requests-in-loop', rule, {
valid: [
`for(let i = 0; i < 10; i++) { lims.CallServerAsync("Categ.Name") }`,
`lims.CallServer("Categ.Name")`,
],
invalid: [
{
name: 'Callserver in for loop',
code: `
for (var i = 0; i < 10; i++) {
lims.CallServer("SomeCategory.SomeScript");
}
`,
errors: [
{
message:
'Avoid requests in loops (CallServer). You should make a server script or datasource that allows you to retrieve the data in one request. Or call the asynchronous version of the function and handle the result afterwards.',
type: 'CallExpression',
},
],
},
{
name: 'Callserver in for in loop',
code: `
for(var i in [1,2,3]) {
lims.CallServer("SomeCategory.SomeScript");
}
`,
errors: [
{
message:
'Avoid requests in loops (CallServer). You should make a server script or datasource that allows you to retrieve the data in one request. Or call the asynchronous version of the function and handle the result afterwards.',
type: 'CallExpression',
},
],
},
{
name: 'GetDataSet in while loop',
code: `
let i = 0;
while(i < 10) {
lims.GetDataSet("SomeCategory.SomeDS");
i++;
}
`,
errors: [
{
message:
'Avoid requests in loops (GetDataSet). You should make a server script or datasource that allows you to retrieve the data in one request. Or call the asynchronous version of the function and handle the result afterwards.',
type: 'CallExpression',
},
],
},
{
name: 'GetData in do while loop',
code: `
let i = 0;
do {
lims.GetData("SomeCategory.SomeDS");
i++;
} while(i < 10);
`,
errors: [
{
message:
'Avoid requests in loops (GetData). You should make a server script or datasource that allows you to retrieve the data in one request. Or call the asynchronous version of the function and handle the result afterwards.',
type: 'CallExpression',
},
],
},
{
name: 'GetDataSource in array map',
code: `
[].map(() => {
dgd.Data = lims.GetDataSource("SomeCategory.SomeDS");
});
`,
errors: [
{
message:
'Avoid requests in loops (GetDataSource). You should make a server script or datasource that allows you to retrieve the data in one request. Or call the asynchronous version of the function and handle the result afterwards.',
type: 'CallExpression',
},
],
},
{
name: 'await CallServerAsync in for',
code: `
async function someAsyncFunction() {
for (var i = 0; i < 10; i++) {
await lims.CallServerAsync("SomeCategory.SomeScript");
}
}
`,
errors: [
{
message:
'Avoid awaiting requests in loops (CallServerAsync). You should either make a server script or datasource that allows you to retrieve the data in one request or do not await each call (Using Promise.all if possible or handling the response in a callback).',
type: 'CallExpression',
},
],
},
],
});