apigee / apigeelint

Static code analysis for Apigee proxy bundles to encourage API developers to use best practices and avoid anti-patterns.
Apache License 2.0
92 stars 71 forks source link

JavaScript | ESLint report warning for call a function as a include resource #367

Closed emoralescaamano closed 1 year ago

emoralescaamano commented 1 year ago

Hi,

I get the following warning when i call a function that is included in JavaScript policy definition:

image

In the policy i do the follow (i'm following the definition of Apigee X documentation):

image

The IncludeURL attribute works like "import" sentence in JavaScript so, ¿Why Apigeelint marks this warning?, ¿Exists any alternative to remove this warning without use "import" sentence in script?

Thanks for help.

ssvaidyanathan commented 1 year ago

@emoralescaamano - would it be possible to share your eslint configuration and also the JS files so that I can reproduce?

emoralescaamano commented 1 year ago

@ssvaidyanathan sure, the configuration for eslint is the same of the master branch:

{
    "env": {
        "browser": false,
        "commonjs": false,
        "strict": true,
        "es6": true,
        "node": false
    },
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "sourceType": "module"
    },
    "rules": {
        "no-const-assign": "warn",
        "no-this-before-super": "warn",
        "no-undef": "warn",
        "no-unreachable": "warn",
        "no-unused-vars": "warn",
        "constructor-super": "warn",
        "valid-typeof": "warn"
    }
} 

The JS's files are follow:

JS-SearchActivitiesModels.js

/* jshint esversion:6, node:false, strict:implied */
/* global properties, context, crypto, httpClient*/

var responsePayload = JSON.parse(context.getVariable('response.content'));

const responseMapping = {
                        "mapping1": "mapp1",
                        "mapping2": "mapp2",
                        "mapping3": "mapp3",
                        "mapping4": "mapp4",
                    };

if (context.flow == "PROXY_RESP_FLOW") {
    var newResponsePayload = [];

    if (responsePayload !== null) {
        try {
            responsePayload.forEach((obj) =>  {
                var newObj = {};
                for (var propKey in obj) {
                    if (obj[propKey]) {
                        if(responseMapping.hasOwnProperty(propKey)) {
                            var newMapKeyProp = responseMapping[propKey];

                            if (newMapKeyProp) {
                                if (propKey === 'mapping4') {
                                    var dateTimeFormated = setDateFormat1(obj[propKey]);
                                    newObj[newMapKeyProp] = dateTimeFormated;

                                } else {
                                    newObj[newMapKeyProp] = obj[propKey];
                                }
                            }
                        }
                    }
                }
                newResponsePayload.push(newObj);
            });
        } catch (err) {
            var errorMessage = 'Error mapping response ' + err;
            context.setVariable('apigee.triggerError', 'true');
            context.setVariable('apigee.errorMessage', errorMessage);
        }
    }
    context.setVariable('response.content', JSON.stringify(newResponsePayload));
}

JS-GeneralFunctions.js

/* jshint esversion:6, node:false, strict:implied */
/* global properties, context, crypto, httpClient*/
function setDateFormat1(dateSource) {
    const date = new Date(dateSource);

    if(date instanceof Date && !isNaN(date)) {
        var day = date.getDate().toString(); 
        var month = (date.getMonth() + 1).toString();
        var year = date.getFullYear();

        if (month.length < 2) {
            month = '0' + month;
        }

        if (day.length < 2) {
            day = '0' + day;
        }

        return [month, day, year].join('/');
    } else {
        throw 'RangeError: Date is invalid';
    }
}

And this is the JavaScript policy configuration:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript continueOnError="false" enabled="true" timeLimit="200" name="JS-CreateSearchActivitiesModels">
  <DisplayName>JS-CreateSearchActivitiesModels</DisplayName>
  <ResourceURL>jsc://JS-CreateSearchActivitiesModels.js</ResourceURL>
  <IncludeURL>jsc://JS-GeneralFunctions.js</IncludeURL>
</Javascript>

Thanks!

ssvaidyanathan commented 1 year ago

@emoralescaamano - Can you add setDateFormat1 to the jshint globals?? That should do the trick. This is not apigeelint plugin issue. Its just returning the error returned by jshint. If you paste your JavaScript code to jshint.com. You will see the error but when you add setDateFormat1 to the global, its gone.

/* jshint esversion:6, node:false, strict:implied */
/* global properties, crypto, httpClient, setDateFormat1 */
emoralescaamano commented 1 year ago

@ssvaidyanathan great, the warning gone, thank you so much!