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

多个 if 分支中用 let 定义同名变量,在jsx内引用后会编译错乱 #3388

Closed
hyacinthus opened this issue Jun 11, 2019 · 4 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@hyacinthus
Copy link

问题描述
render() 中的多个 if 分支中用 let 定义同名变量,在jsx内引用后会编译错乱

原始代码

   if (q.type === 'choice') {
      if (q.choiceType === 'single') {
        // 单选
        let opt1 = []
        q.choices.map(c => {
          opt1.push({
            label: c.text,
            value: c.value.toString() // 这里组件需要字符串
          })
        })
        body = (
          <View className='panel__content'>
            <AtRadio
              options={opt1}
              value={this.state.singleValue}
              onClick={this.onRadioChange}
            />
          </View>
        )
      } else if (q.choiceType === 'multi') {
        // 多选
        console.log('multi')
        let opt1 = []
        q.choices.map(c => {
          // 多选排斥选项
          if (c.mutex) {
            this.data.mutex.push(c.value)
          }
          opt1.push({
            label: c.text,
            value: c.value.toString() // 这里组件需要字符串
          })
        })
        console.log(opt1)
        body = (
          <View className='panel__content no-padding'>
            <View className='example-item'>
              <View className='checkbox-container'>
                <AtCheckbox
                  options={opt1}
                  selectedList={this.state.selected}
                  onChange={this.onCheckboxChange}
                />
              </View>
            </View>
          </View>
        )
      }

编译后小程序

if (q.type === 'choice') {
        if (q.choiceType === 'single') {
          // 单选
          _$opt = [];

          q.choices.map(function (c) {
            _$opt.push({
              label: c.text,
              value: c.value.toString() // 这里组件需要字符串
            });
          });
          _index.propsManager.set({
            "options": _$opt1,
            "value": this.__state.singleValue,
            "onClick": this.onRadioChange
          }, $compid__19);
        } else if (q.choiceType === 'multi') {
          // 多选
          console.log('multi');
          _$opt2 = [];

          q.choices.map(function (c) {
            // 多选排斥选项
            if (c.mutex) {
              _this2.data.mutex.push(c.value);
            }
            _$opt2.push({
              label: c.text,
              value: c.value.toString() // 这里组件需要字符串
            });
          });
          console.log(_$opt2);
          _index.propsManager.set({
            "options": _$opt1,
            "selectedList": this.__state.selected,
            "onChange": this.onCheckboxChange
          }, $compid__20);
        }

期望行为
上面小程序代码倒数第五行应该为 _$opt2

系统信息
👽 Taro v1.3.0

Taro CLI 1.3.0 environment info:
System:
OS: macOS 10.14.2
Shell: 5.3 - /bin/zsh
Binaries:
Node: 12.3.1 - /usr/local/bin/node
Yarn: 1.16.0 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
npmPackages:
@tarojs/cli: 1.3.0 => 1.3.0
@tarojs/components: 1.3.0 => 1.3.0
@tarojs/plugin-babel: 1.3.0 => 1.3.0
@tarojs/plugin-csso: 1.3.0 => 1.3.0
@tarojs/plugin-sass: 1.3.0 => 1.3.0
@tarojs/plugin-uglifyjs: 1.3.0 => 1.3.0
@tarojs/router: 1.3.0 => 1.3.0
@tarojs/taro: 1.3.0 => 1.3.0
@tarojs/taro-alipay: 1.3.0 => 1.3.0
@tarojs/taro-h5: 1.3.0 => 1.3.0
@tarojs/taro-swan: 1.3.0 => 1.3.0
@tarojs/taro-weapp: 1.3.0 => 1.3.0
@tarojs/webpack-runner: 1.3.0 => 1.3.0
eslint-config-taro: 1.3.0 => 1.3.0
nervjs: ^1.3.9 => 1.4.0

@taro-bot
Copy link

taro-bot bot commented Jun 11, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@yuche yuche added good first issue Good for newcomers 编译器 labels Jun 11, 2019
@taro-bot taro-bot bot assigned yuche Jun 11, 2019
@taro-bot
Copy link

taro-bot bot commented Jun 11, 2019

CC @yuche

@Garfield550
Copy link
Collaborator

为什么不试试把 let opt1 = [] 提升一下层级

if (q.type === 'choice') {
  let opt1 = []
  if (q.choiceType === 'single') {
    // 单选
  } else if (q.choiceType === 'multi') {
    // 多选
  }
}

@hyacinthus
Copy link
Author

@Garfield550 提升当然能解决问题,但是重要的是通过这个偶然的事情发现了一个bug啊。

我再补充一些。

  1. 我本来是单选和多选分别 let options = []
    发现多选怎么都出不来选项。 后来发现 this 里本来就有个options变量为页面配置。单选的options被编译为 _$options1 ,表现正常。多选的 options 被编译为 _$options2 函数里表现正常,jsx中却成了最顶层的 options 。

  2. 我于是想避开顶层的 options 两个变量都被我定义成了 opts ,这时候还是出错,第二段jsx中引用的是第一段的变量。

  3. 于是我就想用 opts1 和 opts2 区别开来。 这时候我感觉我额外触发了一个bug,就是变量后边不能随便加数字,加了就更不对了。 看看我正文里贴的编译结果,第一个opts1被编译成了_$opt ,第二个 opts2 被编译成了 _$opts2 ,但是,两段jsx中引用的却是一个不存在的变量 _$opts1 。 这个情况下,程序会直接报错,jsx 出现了没有定义的变量。

所以我觉得可能这里有2个bug

@yuche yuche closed this as completed in 6df5d8d Jun 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants