Skip to content

Template string is not compressed as possible #7008

@rentalhost

Description

@rentalhost

Describe the bug

In some cases, template strings is not compressed as possible.

Input code

const example1 = (param) => `a${param}b` + param + `b`; // Not works
const example2 = (param) => `a${param}b` + `${param}` + `b`; // Works

alert(example1);
alert(example2);

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": {
        "toplevel": false,
        "keep_classnames": false,
        "keep_fnames": false,
        "keep_private_props": false,
        "ie8": false,
        "safari10": false
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": true,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.37&code=H4sIAAAAAAAAA0vOzysuUUitSMwtyEk1VLBV0ChILErM1VSwtVNISFSpBvNqkxIUtBXATCCdkJRgzcuVjKzRCK%2FGBBgvAa6blysxJ7WoRANmsaY1moiRpjUAO2hXrZsAAAA%3D&config=H4sIAAAAAAAAA3VUy5LjIAz8F85zmM1ha2s%2BYG%2F7DRQxwmEWIxcSnrhS%2BfcVfkCczN6spiVaUpub%2BqROfdzUaBJBKl80RzZX9aF4HoG65EdWb4pJIGcCwV0Ck3pgoQCd3k8nOQ6IBBvhTQ0%2BejeXYh0OYwKi8i1JeYDIVHkmJfySkFOW6IwYwMSXWBvSPjL0kFpqhyGYkUBPJtWMcptJnjAeoMxg9ZhwfECj9ewxSrEds2Cs7tBCBXyCjv0EjSI1hBJJhFUlC2jhnPu%2BDHBlwmRCNlxrwXUZg9xY8y7oibXLkZ6gg9IVWpvcWN7pBJxT3Dmf6ONhDn8BRGcwRNEM0DIX3Mkejkz3DctHJ0vkuaKy4aYqQi%2FNae9d7bCohsS%2BTSuBzR2UDrtWfAMPDZG3oME5mXZNpi%2FP3aUVK2ZEV0OZmnFtB2uodz8d0WKBF%2FC3qOLHdWz4YPjyjNE8nDG8lBiAL2hfYBkE4zOYxL3X8RnN0YKMGezDQaYSVpvK8hl1KH%2FbPmcxhOToPuC52ld%2BysHEvtjyphjHABOE40b%2F64dvMLlkKgverLgbD361nRnxkf%2Fxvj8K96IAbd4UyLaW5%2BGnurfnYG3A05%2BNtwj%2FB12GzHmCBAAA

Expected behavior

const example1 = e => a${e}b${e}b, // here
      example2 = e => `a${e}b${e}b`;

alert(example1),
alert(example2);

Actual behavior

const example1 = e => `a${e}b`+e+"b", // here
      example2 = e => `a${e}b${e}b`;

alert(example1),
alert(example2);

Version

1.3.37

Additional context

Since a forced cast is happening between the template literals + whatever information is passed as an argument, then forcing the cast with a template literal will be the same as injecting it directly into a wider template string.

example1(123) => `abc` + 123 => "abc123"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions