Skip to content

createTempVariable() should also try to not shadowing upper lexical environment temp variables #40826

Closed
@Jack-Works

Description

@Jack-Works

TypeScript Version: 3.9.3

Search Terms:

Code

I'm using createTempVariable but I found it is buggy.

The buggy version: Jack-Works/react@792a7f7#diff-6624517c3255932b3079f651161b7df0

Fixed version:

Jack-Works/react@4ae646b#diff-6624517c3255932b3079f651161b7df0

Reproduction:

const ts = require('typescript')
const t = require('./ReactFreshTypeScriptTransformer.js')

console.log(
    ts.transpileModule(
        `
import { useDefaultWallet } from '../../plugins/Wallet/hooks/useWallet'

/**
 * Get the address of the default wallet
 */
export function useAccount() {
    const wallet = useDefaultWallet()
    return wallet?.address ?? ''
}
`,
        {
            transformers: { before: [t()] },
            compilerOptions: { target: ts.ScriptTarget.ES2017 },
        }
    ).outputText
)

The buggy result using createTempVariable():

var _a;
_a = $RefreshSig$();
import { useDefaultWallet } from '../../plugins/Wallet/hooks/useWallet';
/**
 * Get the address of the default wallet
 */
export function useAccount() {
    var _a;
    _a();
    const wallet = useDefaultWallet();
    return (_a = wallet === null || wallet === void 0 ? void 0 : wallet.address) !== null && _a !== void 0 ? _a : '';        
}
_a(useAccount, "Zqcse9ORXvHAGVGDGSjWLnVdvA4=", false, () => [useDefaultWallet]);

As you can see, downleveling ?? syntax create a new variable also called "_a" which shadows my upper variable "_a", then _a() leads to an runtime error.

The correct version using createFileLevelUniqueName():

var $c0;
$c0 = $RefreshSig$();
import { useDefaultWallet } from '../../plugins/Wallet/hooks/useWallet';
/**
 * Get the address of the default wallet
 */
export function useAccount() {
    var _a;
    $c0();
    const wallet = useDefaultWallet();
    return (_a = wallet === null || wallet === void 0 ? void 0 : wallet.address) !== null && _a !== void 0 ? _a : '';
}
$c0(useAccount, "Zqcse9ORXvHAGVGDGSjWLnVdvA4=", false, () => [useDefaultWallet]);

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions