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

const in ambient context incorrectly flagged as uninitialised #3882

Closed
sverweij opened this issue Mar 6, 2022 · 7 comments · Fixed by #3883
Closed

const in ambient context incorrectly flagged as uninitialised #3882

sverweij opened this issue Mar 6, 2022 · 7 comments · Fixed by #3883
Assignees
Labels
Milestone

Comments

@sverweij
Copy link

sverweij commented Mar 6, 2022

Describe the bug

Since #3742 was introduced in version 1.2.46 (issue: #3730), not only uninitialised. const declarations in 'strict' mode/ real code get flagged with error: 'const' declarations must be initialized but also those in ambient context (type declaration only/ .d.ts). However in ambient contexts a const cannot be initialised; tsc will complain with a TS1039: Initializers are not allowed in ambient contexts.ts(1039)

Input code

// filename: types.d.ts
export const aThing: IAnInterface[];

export interface IAnInterface {
  anAttribute: string;
  anotherAttribute: boolean;
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6",
    "strict": false,
    "strictMode": false
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.2.149&code=H4sIAAAAAAAAA1WMMQqEMBBF%2BznFnEB7U1nab7dYRHeigTAjyRd2Wby7Igjafd5%2FvLrmEJM0jN8ipfpUKCTfxTJ4NC1g%2F5qjTg13rXYKycGP8u4dXVa84MPgPzF7bYEchxVHvxxLJ3diwyz59g1mSbw62mgHJbEkGZAAAAA%3D&config=H4sIAAAAAAAAA0WOSw6DMAwF7%2BI1iyqLLrgDh4iCQakAR35GKoq4e0OgsPNvxi%2FTB4HaTMkrWI8K22L%2BSy3ZlhhBYzJqyFBGg5%2FADfUcRL2JglrTlfey9jqyFYjhXs4VYBIB38gclzhshz7InJSBZ%2BWXcfpf7sU1S78eg1wTVOe7CGEag93Y2XbS3%2Bjz5TqJ6C5VTfkDjgOVHewAAAA%3D

Expected behavior

In ambient contexts (type declaration files) uninitialised const declarations are allowed.

Actual behavior

error: 'const' declarations must be initialized

Version

@swc/core 1.2.149

Additional context

  • I've tried to play around with parser options (e.g. to indicate the input is not strict) but failed to do so. If there's an option to influence this behavior, I'll be all ears.
  • I encountered this issue while using @swc/core for static analysis (and not with the swc cli per se). Sample snippet:
const SWC_PARSE_OPTIONS = {
  dynamicImport: true,
  syntax: "typescript",
  target: "es2022",
};
console.dir(
  swc.parseFileSync('types.d.ts', SWC_PARSE_OPTIONS), 
  {depth:10}
)

// no output, but instead on stderr:
/* 
Uncaught Error: error: 'const' declarations must be initialized
  --> types.d.ts:2:14
   |
14 | export const aThing: IAnInterface[];
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



Caused by:
    0: error was recoverable, but proceeding would result in wrong codegen
    1: Syntax Error
    at Compiler.parseFileSync (/Users/sander/thing/node_modules/@swc/core/index.js:87:36)
    at Object.parseFileSync (/Users/sander/thing/node_modules/@swc/core/index.js:201:21) {
  code: 'GenericFailure'
*/
@sverweij sverweij added the C-bug label Mar 6, 2022
@sverweij sverweij changed the title Uninitialised const in ambient context flagged const in ambient context incorrectly flagged as uninitialised Mar 6, 2022
@kdy1
Copy link
Member

kdy1 commented Mar 6, 2022

I'll fix this right away and publish a new verison.

@kdy1 kdy1 self-assigned this Mar 6, 2022
@kdy1 kdy1 added this to the v1.2.150 milestone Mar 6, 2022
kdy1 added a commit to kdy1/swc that referenced this issue Mar 6, 2022
@kdy1 kdy1 closed this as completed in #3883 Mar 6, 2022
@sverweij
Copy link
Author

sverweij commented Mar 6, 2022

That was very quick!! Thanks @kdy1 👍

@sverweij
Copy link
Author

sverweij commented Mar 6, 2022

@kdy1 I've retested with v1.2.150 - but it seems the issue remains - at least when run against swc.parseFile or swc.parseFileSync with the code from Additional context in the description above:

// file: types.d.ts
export const aThing: IAnInterface[];

export interface IAnInterface {
  anAttribute: string;
  anotherAttribute: boolean;
}
// file: test_swc.js
const swc = require('@swc/core')

console.log(`swc version: ${swc.version}`)

const SWC_PARSE_OPTIONS = {
  dynamicImport: true,
  syntax: "typescript",
  target: "es2022",
};

console.dir(
  swc.parseFileSync('types.d.ts', SWC_PARSE_OPTIONS), 
  {depth:10}
)
$ node test_swc.js
swc version: 1.2.150
/Users/sander/thing/node_modules/@swc/core/index.js:87
        return JSON.parse(bindings.parseFileSync(path, toBuffer(options)));
                                   ^

Error: error: 'const' declarations must be initialized
 --> types.d.ts:1:14
  |
1 | export const aThing: IAnInterface[];
  |              ^^^^^^^^^^^^^^^^^^^^^^



Caused by:
    0: error was recoverable, but proceeding would result in wrong codegen
    1: Syntax Error
    at Compiler.parseFileSync (/Users/sander/thing/node_modules/@swc/core/index.js:87:36)
    at Object.parseFileSync (/Users/sander/thing/node_modules/@swc/core/index.js:201:21)
    at Object.<anonymous> (/Users/sander/thing/test_swc.js:13:7)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'GenericFailure'
}

Node.js v17.4.0

@kdy1
Copy link
Member

kdy1 commented Mar 7, 2022

For parseFile it's matter of option

@sverweij
Copy link
Author

sverweij commented Mar 7, 2022

Hi @kdy1 that's what I expected as well. However, I've been looking for an option that'd work here, but weren't able to.

parseFile (/ parseFileSync/ parse/ parseSync) takes a 2nd parameter of the type ParseOptions which is defined in node-swc/rc/types.ts, but it doesn't seem to have something to e.g. influence whether to interpret typescript strictly (as targeted in PR #3742) or ambiently f.t.m.; summarised, for modules of "typescript" syntax, these are the options I see:

{
  syntax: "typescript";
  /**
   * Defaults to `false`.
   */
  tsx?: boolean;
  /**
   * Defaults to `false`.
   */
  decorators?: boolean;
  /**
   * Defaults to `false`
   */
  dynamicImport?: boolean;
  comments?: boolean;
  script?: boolean;
  /**
   * Defaults to es3.
   */
  target?: JscTarget;
}

Is it possible to pass more/ different things to the parse* functions than documented in that type?

There are strict and strictMode options in the module part of an swc config, but if I understand correctly that's for influencing what swc emits only.

@kdy1
Copy link
Member

kdy1 commented Mar 7, 2022

It has a hidden field.
Btw, you are not expected to process d.ts file using swc

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 17, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants