JSON File Error - Expected an assignment or function call and instead saw an expression

  • ALM used - GitHub
  • CI system - AWS CodePipeline
  • Languages of the repository - Typescript
  • Error observed Expected an assignment or function call and instead saw an expression
  • Steps to reproduce - We are getting the above error on the below JSON file
  • Potential workaround - None that we can see

JSON file this error is popping up on -

[
  {
    "hs": "hs_object_id",
    "v3": "hubspotDetails.agencyCustomObjectId"
  },
  {
    "hs": "first_name",
    "v2": "first_name",
    "v3": "agencyDetails.primaryContact.firstName"
  },
  {
    "hs": "last_name",
    "v2": "last_name",
    "v3": "agencyDetails.primaryContact.lastName"
  },
  {
    "hs": "company_email_id",
    "v2": "company_email_id",
    "v3": "agencyDetails.primaryContact.email"
  },
  {
    "hs": "status",
    "v2": "status",
    "v3": "statusField"
  },
  {
    "hs": "company_name",
    "v2": "company_name",
    "v3": "agencyDetails.basicDetails.agencyName"
  },
  {
    "hs": "company_address",
    "v2": "company_address",
    "v3": "agencyDetails.businessAddress.streetAddress"
  },
  {
    "hs": "zip",
    "v2": "zip",
    "v3": "agencyDetails.businessAddress.zipCode"
  },
  {
    "hs": "city",
    "v2": "city",
    "v3": "agencyDetails.businessAddress.city"
  },
  {
    "hs": "suite",
    "v2": "suite",
    "v3": "agencyDetails.businessAddress.suite"
  },
  {
    "hs": "company_website",
    "v2": "company_website",
    "v3": "agencyDetails.basicDetails.businessWebsite"
  },
  {
    "hs": "company_phone_number",
    "v2": "company_phone_number",
    "v3": "agencyDetails.basicDetails.phoneNumber"
  },
  {
    "hs": "state",
    "v2": "state",
    "v3": "agencyDetails.businessAddress.state"
  }
]

Hi,

Thanks for this report. Can you share the rule ID this issue is coming from, please?

 
Thx,
Ann

Does this help?

1 Like

Hi,

Yes, that helps.

I’ve flagged this for the language experts.

 
Ann

Hi @Christopher_Creel,

what is the filename extension? If it’s .json it should not be analyzed at all by the JS/TS analyzer. If it’s .js, you should probably add the file to the excluded list.

Cheers,
Victor

We are seeing this error pop up 5 different times, each for a JSON file.

Here are the file names -

  • agencyFieldMapping.json
  • contactCompanyFieldMapping.json
  • hubspotAgencyProperties.json
  • hubspotCompanyProperties.json
  • hubspotContactProperties.json

Hello @Christopher_Creel,

I am taking over my colleague’s investigation, as he is on holiday.

I tried to reproduce your issue locally but without any success. Would it be possible to share your SonarCloud project configuration?

There might be something here that instructs the JS/TS analyzer to pick your JSON files during analysis despite not being supposed to.

Thank you,
Yassin

I would be happy to share my SonarCloud project configuration. How do I do that?

By the way, the number of bugs has now increased to 7. We have not changed the configuration of these projects in months, so something new is afoot.

I would be happy to share my SonarCloud project configuration. How do I do that?

You can send me a private message directly through my profile :slight_smile:

By the way, the number of bugs has now increased to 7. We have not changed the configuration of these projects in months, so something new is afoot.

Since you are using SonarCloud, this can be expected whenever the JS/TS analyzer releases a version that includes new rules, which happens to be the case for the past few months.

Sent you an invitation to the GitHub repo in question. Access to the Sonar Cloud repo in through GitHub.

Hello @Christopher_Creel,

Thank you for giving me access to your GitHub repository. I was able to identify the root cause of these issues raised on JSON files. You can now kick me out of your repository :slight_smile: . Before explaining what’s going on, let me briefly share with you how the JS/TS analyzer works under the hood.

When scanning a TypeScript project, the analyzer queries first the TypeScript compiler to identify all the files to consider during the analysis. This process relies on the TSConfig-s of the project and leads to the creation of “TypeScript programs”. These programs are intermediate representations of your project in the TypeScript compiler, which include the files part of the compilation process. The analyzer then analyses all the files.

Now, if a TSConfig instructs the TypeScript compiler to resolve JSON files with resolveJsonModule because those are imported in the codebase, they will be considered during the compilation process. As a result, they will be scanned as well by the JS/TS analyzer and issues will be raised accordingly since a JSON file is nothing more than a JavaScript object. This is what’s unexpectedly happening in your case.

I discussed with my colleagues the relevance of scanning JSON files in this scenario, and we concluded that analyzing such files is of little interest. Consequently, I created this ticket to rectify this behavior of the analyzer. In the meantime, you can use the property sonar.exclusions to exclude JSON files during scanning [1].

Hope this helps,
Yassin

[1] Narrowing the focus with an analysis scope

1 Like