github-script icon indicating copy to clipboard operation
github-script copied to clipboard

Parser error? - Unexpected token ':' at new AsyncFunction

Open sgarciajaramillo opened this issue 2 years ago • 1 comments

Hi there,

I'm going over a set of inputs and checking which are set to true to eventually feed them to another workflow. Running the code below work fine out of actions. i.e (https://www.typescriptlang.org/play)

When I running with the action, I get the following. I've tried a few things but can't get any more detail.

Thanks for your help, sorry if I cannot provide much detail beside this.

    github-token: ***
    debug: false
    user-agent: actions/github-script
    result-encoding: json
SyntaxError: Unexpected token ':'
    at new AsyncFunction (<anonymous>)
Error: Unhandled error: SyntaxError: Unexpected token ':'
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:4807:16)
    at main (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:4862:26)
    at Module.272 (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:4846:1)
    at __webpack_require__ (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:24:31)
    at startup (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:43:19)
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:49:18
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:52:10)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
name: deploy-services

on:
  workflow_dispatch:
    inputs:
      enableapi-github-actions:
        type: boolean
        description: enableapi-github-actions
        required: false
      iam-github-actions:
        type: boolean
        description: iam-github-actions
        required: false
      networking-github-actions:
        type: boolean
        description: networking-github-actions
        required: false
      gke-github-actions:
        type: boolean
        description: gke-github-actions
        required: false
      k8srunners-github-actions:
        type: boolean
        description: k8srunners-github-actions
        required: false

jobs:
  get-services:
    name: validating services selection
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        with:
          script: | 
              const enableapi = ${{ github.event.inputs.enableapi-github-actions }}
              const networking = ${{ github.event.inputs.networking-github-actions }}
              const iam = ${{ github.event.inputs.iam-github-actions }}
              const gke = ${{ github.event.inputs.gke-github-actions }}
              const k8srunners = ${{ github.event.inputs.k8srunners-github-actions }}
              let validate = new Map([
                ["enableapi-github-actions", enableapi],
                ["networking-github-actions", networking],
                ["iam-github-actions", iam],
                ["gke-github-actions", gke ],
                ["k8srunners-github-actions", k8srunners]
              ]);
              var services: string[] = []
              validate.forEach((value: boolean, key: string) => {
                if (value === true) {
                  services.push(key)
                  console.log(key)
                }
              });
              console.log(services)

sgarciajaramillo avatar Jul 19 '22 05:07 sgarciajaramillo

Update: I think there is something wrong with the parser, this does not work either, I get the same error:

Run actions/github-script@v6
  with:
    script: 
  var services: boolean = true
  
  console.log(services)
  
  
  ERROR:
... 
SyntaxError: Unexpected token ':'
...

Ended up replacing that line with:

var services = Array.from(validate.entries()).filter(entry => entry[1]).map(entry => entry[0])

and got me going.

Thanks!

sgarciajaramillo avatar Jul 19 '22 18:07 sgarciajaramillo

👋 @sgarciajaramillo this action doesn't support TypeScript syntax. In JavaScript, something like var services: string[] = [] or var services: boolean = true isn't valid. You should be able to remove the : Type from the variable declaration.

Looks like you're unblocked, so going to close this issue.

joshmgross avatar Jan 21 '23 04:01 joshmgross