Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Target ES2021 mapping in transpilers/swc #1521

Merged
merged 4 commits into from Oct 22, 2021

Conversation

safareli
Copy link
Contributor

@safareli safareli commented Oct 18, 2021

I was getting error: jsc.target should be es5 or upper to use getter / setter
and after investigation I've isolated issue and here is the "fix"

I was getting error: `jsc.target should be es5 or upper to use getter / setter` 
and after investigation I've isolated issue and here is the "fix"
@cspotcode
Copy link
Collaborator

@cspotcode cspotcode commented Oct 18, 2021

Linking to #1510 since we may need to apply the same concept to target options. We may also want to add some >= logic so that unrecognized target values translate to the newest swc target we know about.

@codecov
Copy link

@codecov codecov bot commented Oct 18, 2021

Codecov Report

Merging #1521 (96ef4ce) into main (3b40365) will increase coverage by 0.10%.
The diff coverage is 91.66%.

Impacted Files Coverage Δ
src/transpilers/swc.ts 78.00% <91.66%> (+4.19%) ⬆️

@cspotcode
Copy link
Collaborator

@cspotcode cspotcode commented Oct 18, 2021

Thanks for investigating and sending a fix. I want to throw together a regression test. What did you do to cause the error? And did it terminate the node process with a non-zero exit code, or did it log a warning? This info will help write a quick test.

@safareli
Copy link
Contributor Author

@safareli safareli commented Oct 18, 2021

in my tsconfig.json I have:

{
  "compilerOptions": {
    "target": "ES2021",
    ...
 },
  "ts-node": {
    "require": ["tsconfig-paths/register"],
    "files": true,
    "transpileOnly": true,
    "transpiler": "ts-node/transpilers/swc-experimental"
  }
}

then I was requiring a module from local module (I have monorepos setup).
in package.json of the module that I required there is:

{
  ...
  "files": [
    "build"
  ],
  ....
}

so that when this module is required build js and d.ts files are used.

as target for my tsconfig was es2021 in compiled js the use of property get syntax was not de-suggered.
I had something like

class Foo {
  get foo() {
    return "foo"
  }
}

and the error is here:

$ npm run foo

> foo
> ts-node --project ../../tsconfig.json ./src/foo.ts

error: jsc.target should be es5 or upper to use getter / setter
  --> /Users/safareli/dev/my-project-foo/packages/foo/src/code/index.ts:31:7
   |
31 |   get foo() {
   |       ^^^

/Users/safareli/dev/my-project-foo/node_modules/@swc/core/index.js:135
        return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
                        ^
Error: failed to process js file

Caused by:
    failed to parse module: error was recoverable, but proceeding would result in wrong foo
    at Compiler.transformSync (/Users/safareli/dev/my-project-foo/node_modules/@swc/core/index.js:135:25)
    at Object.transformSync (/Users/safareli/dev/my-project-foo/node_modules/@swc/core/index.js:213:21)
    at Object.transpile (/Users/safareli/dev/my-project-foo/node_modules/ts-node/src/transpilers/swc.ts:114:32)
    at /Users/safareli/dev/my-project-foo/node_modules/ts-node/src/index.ts:1142:35
    at Object.compile (/Users/safareli/dev/my-project-foo/node_modules/ts-node/src/index.ts:1185:30)
    at Module.m._compile (/Users/safareli/dev/my-project-foo/node_modules/ts-node/src/index.ts:1309:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/safareli/dev/my-project-foo/node_modules/ts-node/src/index.ts:1313:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12) {
  code: 'GenericFailure'
}
npm ERR! Lifecycle script `foo` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: @my-project-foo/baz
npm ERR!   at location: /Users/safareli/dev/my-project-foo/packages/baz

@safareli
Copy link
Contributor Author

@safareli safareli commented Oct 18, 2021

I'm sure we would get same issue with ES2022.

@cspotcode
Copy link
Collaborator

@cspotcode cspotcode commented Oct 18, 2021

Thanks for including all the details. At a glance, I could not find a list of every single target for swc. I thought it was in the swc documentation, but today I can't find it. Do you know where it is listed?

@cspotcode
Copy link
Collaborator

@cspotcode cspotcode commented Oct 18, 2021

Does this look like a complete list?
https://rustdoc.swc.rs/swc/config/enum.JscTarget.html

@safareli
Copy link
Contributor Author

@safareli safareli commented Oct 18, 2021

looks like it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants