facebook / jscodeshift

A JavaScript codemod toolkit.

Home Page:https://jscodeshift.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Transformation error (Unexpected symbol <b> at ...

jy95 opened this issue · comments

Hello,

I am using JScodeshift to make some changes in json with my keycloak-capacitor project.

    // Replace dependancies
    return mainRoot
        .find(
            j.Property,
            {
                key: {
                    name: "dependancies"
                }
            }
        )
        .find(j.ObjectExpression)
        .replaceWith((nodePath) => {
            const { node } = nodePath;
            // replace properties
            node.properties = newProperties;
            return node;
        })
        .toSource({ quote: "double" });

However, it doesn't seem to work when toSource is invoked :

Transformation error (Unexpected symbol <b> at 37:28 35 |    }, 36 |    "dependencies":{ 37 |       "base64-js":"^1.5.1",base64-js"^1.5.1"
                      ^ 38 |       "js-sha256":"^0.9.0"js-sha256"^4.2.0" 39 |    },)
SyntaxError: Unexpected symbol <b> at 37:28
35 |    },
36 |    "dependencies":{
37 |       "base64-js":"^1.5.1",base64-js"^1.5.1"
                                ^
38 |       "js-sha256":"^0.9.0"js-sha256"^4.2.0"
39 |    },

If node.properties = newProperties; is put on comments, then I got no error but not the behavior I am expecting.

image

Steps to reproduce :

https://github.com/jy95/keycloak-capacitor/tree/jscodeshift-packageJson
git checkout jscodeshift-packageJson
npm install
npx jscodeshift -t update-packageJson.ts __testfixtures__/update-packageJson.input.json -originalKeycloakPath __testfixtures__/update-packageJson.input2.json  -d -p

Kind regards,

I've never tried JSCodeshift for JSON files... Which parser are you using? Please link to an ASTExplorer.net repro.

For JSON, wouldn't it be easier to just use JSON.parse and manipulate it that way?

@Daniel15 I was using json-estree-ast, a litle wrapper around json-to-ast
If I was able to deal with .d.ts files, why not the JSON files ?

When using another parser wrapper I created (momoa-estree-ast using this time @humanwhocodes/momoa ), I saw that JSCodeShift is doing weird stuff : dependencies isn't replaced and is broken, explaining why the other parser also failed :

{
       "name":"keycloak-capacitor",
       "version":"19.0.1",
       "description":"Keycloak adapter for Ionicframework with Capacitor support.",
       "main":"dist/keycloak.js",
       "module":"dist/keycloak.mjs",
       "types":"dist/keycloak.d.ts",
       "files":[
          "dist"
       ],
       "scripts":{
          "build":"rollup --config --configPlugin typescript",
          "prepublishOnly":"npm run build",
          "test":"npx jest"
       },
       "repository":{
          "type":"git",
          "url":"git+https://github.com/jy95/keycloak-capacitor.git"
       },
       "keywords":[
          "capacitor",
          "adapter",
          "auth",
          "authentication",
          "oauth2",
          "openid",
          "keycloak"
       ],
       "devDependencies":{
          "es6-promise":"^4.2.8",
          "jest":"^28.1.3",
          "jscodeshift":"^0.13.1",
          "tslib":"^2.4.0",
          "typescript":"^4.5.5"
       },
       "dependencies":{
          "base64-js":"^1.5.1","^1.5.1"
          "js-sha256":"^0.9.0""^4.2.0"
       },
       "author":"jy95",
       "license":"Apache-2.0",
       "bugs":{
          "url":"https://github.com/jy95/keycloak-capacitor/issues"
       },
       "homepage":"https://github.com/jy95/keycloak-capacitor#readme"
    }