(def x x)
此代码在JS中执行良好,但没有任何有用的输出,我想知道我们是否应该对此进行检查。当运行{{:advanced}}编译时,它将因一个难以理解的错误信息而失败:
`
线程 "main" 发生异常 java.lang.IllegalStateException: ASSIGN 5 [length: 23] [source_file: .../test/foo.js] 不是 GETPROP 5 [length: 10] [source_file: .../test/foo.js] 的父项
at com.google.common.base.Preconditions.checkState(Preconditions.java:733)
at com.google.javascript.rhino.Node.replaceChild(Node.java:900)
at com.google.javascript.jscomp.AggressiveInlineAliases.inlineGlobalAliasIfPossible(AggressiveInlineAliases.java:557)
at com.google.javascript.jscomp.AggressiveInlineAliases.inlineAliases(AggressiveInlineAliases.java:180)
at com.google.javascript.jscomp.AggressiveInlineAliases.process(AggressiveInlineAliases.java:123)
at com.google.javascript.jscomp.PhaseOptimizer$NamedPass.process(PhaseOptimizer.java:303)
at com.google.javascript.jscomp.PhaseOptimizer.process(PhaseOptimizer.java:230)
at com.google.javascript.jscomp.Compiler.performOptimizations(Compiler.java:2480)
at com.google.javascript.jscomp.Compiler$3.call(Compiler.java:808)
at com.google.javascript.jscomp.Compiler$3.call(Compiler.java:804)
at com.google.javascript.jscomp.CompilerExecutor$2.call(CompilerExecutor.java:102)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:844)
`
此代码本身是不可用的,并且很可能是错误地编写的。这个错误在shadow-cljs用户将一些代码从CLJSJS迁移到本地命名空间时发生了几次。
`
(ns demo.foo
(:require [cljsjs.foo]))
(def foo js/foo)
`
被重写为
`
(ns demo.foo
(:require ["that-npm-foo" :as foo]))
(def foo foo)
`
犯的错误是将{{def}}的右侧认为是命名空间别名{{foo}}的右侧,但实际上它是左侧。