Skip to content

Top-level comments should be placed after @swc/helpers imports #11167

@akcyp

Description

@akcyp

Describe the bug

Hello, I'm working on some legacy project that is using ng-annotate package.

I have a problem resulting from swc compilation, namely in the case of class definitions (and perhaps also in other situations that I have not encountered), the order of the JSDoc comment is shifted by placing the helper definition or helper import.

And this makes ng-annotate unable to process this structure. Furthermore, I suspect this state of affairs can also generate other problems, such as incorrect typing using JSDoc.

Is there any way to fix this bug, or force SWC to place these imports at the very top of the files rather than directly above the classes?

Input code

/** @ngInject */
class TestClass {
  constructor (private testField) {}
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true
    },
    "target": "es2015",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    },
    "transform": {
      "decoratorVersion": "2022-03"
    },
    "preserveAllComments": true,
    "externalHelpers": true
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.20&code=H4sIAAAAAAAAA9PXUnDIS%2FfMy0pNLlHQ0uflSs5JLC5WCEktLnEGs6p5uRQUkvPzikuKSpNL8osUNAqKMssSS1IVSoBq3DJTc1I0FapreblqAaNsl5RLAAAA&config=H4sIAAAAAAAAA2VQPY%2FCMAzd71dUng%2BpFN0NbKdbWFjZo%2BKioCSObBdRIf77JaUNrW6z%2Fez34cdHVcFVWthXj1SmJhoW5NKniQxBzT1NQIeI0rKNCp8zqpKhzjjBMjtjS2yUWBKk3OMIPF84qOELauZDaert18QFjkhwzQXeBtsNSzct%2Bcgo8k%2FUm3Bx5X6tyCZIR%2ByXRMXkCVkshWyoqZtmU%2B9gdZ3lkG%2F449wveY9B51jTAt4VORh3QBdxmXlkAE%2FnfjQ2fTh%2F8ZX%2BG95Lc9ASCqwc58uR7%2FkHjhFL0a0BAAA%3D

Expected behavior

SWC should compile to:

import { _ as _define_property } from "@swc/helpers/_/_define_property";
/** @ngInject */
class TestClass {
    constructor(testField){
        _define_property(this, "testField", void 0);
        this.testField = testField;
    }
}

Actual behavior

SWC returns:

/* @ngInject */ import { _ as _define_property } from "@swc/helpers/_/_define_property";
class TestClass {
    constructor(testField){
        _define_property(this, "testField", void 0);
        this.testField = testField;
    }
}

Version

1.13.20

Additional context

For anyone looking for workaround - I found out that preprocessing all JS/TS files to add semicolon (;) at the beginning of a file tricks compiler to work correctly.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions