Skip to content

Commit d946a58

Browse files
Maria Neiseswannodette
authored andcommitted
CLJS-1311: Improve error reporting when converting JavaScript modules
Show warnings and errors from Google Closure compiler when processing JS modules. Pull :closure-warnings and :closure-extra-annotations into set-options function so we can set those options and :pretty-print for module processing.
1 parent f14b561 commit d946a58

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

src/main/clojure/cljs/closure.clj

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,38 @@
125125
(defmethod js-source-file BufferedInputStream [^String name ^BufferedInputStream source]
126126
(SourceFile/fromInputStream name source))
127127

128+
(def check-level
129+
{:error CheckLevel/ERROR
130+
:warning CheckLevel/WARNING
131+
:off CheckLevel/OFF})
132+
133+
(def warning-types
134+
{:access-controls DiagnosticGroups/ACCESS_CONTROLS
135+
:ambiguous-function-decl DiagnosticGroups/AMBIGUOUS_FUNCTION_DECL
136+
:debugger-statement-present DiagnosticGroups/DEBUGGER_STATEMENT_PRESENT
137+
:check-regexp DiagnosticGroups/CHECK_REGEXP
138+
:check-types DiagnosticGroups/CHECK_TYPES
139+
:check-useless-code DiagnosticGroups/CHECK_USELESS_CODE
140+
:check-variables DiagnosticGroups/CHECK_VARIABLES
141+
:const DiagnosticGroups/CONST
142+
:constant-property DiagnosticGroups/CONSTANT_PROPERTY
143+
:deprecated DiagnosticGroups/DEPRECATED
144+
:duplicate-message DiagnosticGroups/DUPLICATE_MESSAGE
145+
:es5-strict DiagnosticGroups/ES5_STRICT
146+
:externs-validation DiagnosticGroups/EXTERNS_VALIDATION
147+
:fileoverview-jsdoc DiagnosticGroups/FILEOVERVIEW_JSDOC
148+
:global-this DiagnosticGroups/GLOBAL_THIS
149+
:internet-explorer-checks DiagnosticGroups/INTERNET_EXPLORER_CHECKS
150+
:invalid-casts DiagnosticGroups/INVALID_CASTS
151+
:missing-properties DiagnosticGroups/MISSING_PROPERTIES
152+
:non-standard-jsdoc DiagnosticGroups/NON_STANDARD_JSDOC
153+
:strict-module-dep-check DiagnosticGroups/STRICT_MODULE_DEP_CHECK
154+
:tweaks DiagnosticGroups/TWEAKS
155+
:undefined-names DiagnosticGroups/UNDEFINED_NAMES
156+
:undefined-variables DiagnosticGroups/UNDEFINED_VARIABLES
157+
:unknown-defines DiagnosticGroups/UNKNOWN_DEFINES
158+
:visiblity DiagnosticGroups/VISIBILITY})
159+
128160
(defn set-options
129161
"TODO: Add any other options that we would like to support."
130162
[opts ^CompilerOptions compiler-options]
@@ -157,39 +189,18 @@
157189

158190
(when (contains? opts :print-input-delimiter)
159191
(set! (.printInputDelimiter compiler-options)
160-
(:print-input-delimiter opts))))
192+
(:print-input-delimiter opts)))
161193

162-
(def check-level
163-
{:error CheckLevel/ERROR
164-
:warning CheckLevel/WARNING
165-
:off CheckLevel/OFF})
194+
(when (contains? opts :closure-warnings)
195+
(doseq [[type level] (:closure-warnings opts)]
196+
(. compiler-options
197+
(setWarningLevel (type warning-types) (level check-level)))))
166198

167-
(def warning-types
168-
{:access-controls DiagnosticGroups/ACCESS_CONTROLS
169-
:ambiguous-function-decl DiagnosticGroups/AMBIGUOUS_FUNCTION_DECL
170-
:debugger-statement-present DiagnosticGroups/DEBUGGER_STATEMENT_PRESENT
171-
:check-regexp DiagnosticGroups/CHECK_REGEXP
172-
:check-types DiagnosticGroups/CHECK_TYPES
173-
:check-useless-code DiagnosticGroups/CHECK_USELESS_CODE
174-
:check-variables DiagnosticGroups/CHECK_VARIABLES
175-
:const DiagnosticGroups/CONST
176-
:constant-property DiagnosticGroups/CONSTANT_PROPERTY
177-
:deprecated DiagnosticGroups/DEPRECATED
178-
:duplicate-message DiagnosticGroups/DUPLICATE_MESSAGE
179-
:es5-strict DiagnosticGroups/ES5_STRICT
180-
:externs-validation DiagnosticGroups/EXTERNS_VALIDATION
181-
:fileoverview-jsdoc DiagnosticGroups/FILEOVERVIEW_JSDOC
182-
:global-this DiagnosticGroups/GLOBAL_THIS
183-
:internet-explorer-checks DiagnosticGroups/INTERNET_EXPLORER_CHECKS
184-
:invalid-casts DiagnosticGroups/INVALID_CASTS
185-
:missing-properties DiagnosticGroups/MISSING_PROPERTIES
186-
:non-standard-jsdoc DiagnosticGroups/NON_STANDARD_JSDOC
187-
:strict-module-dep-check DiagnosticGroups/STRICT_MODULE_DEP_CHECK
188-
:tweaks DiagnosticGroups/TWEAKS
189-
:undefined-names DiagnosticGroups/UNDEFINED_NAMES
190-
:undefined-variables DiagnosticGroups/UNDEFINED_VARIABLES
191-
:unknown-defines DiagnosticGroups/UNKNOWN_DEFINES
192-
:visiblity DiagnosticGroups/VISIBILITY})
199+
(when (contains? opts :closure-extra-annotations)
200+
(. compiler-options
201+
(setExtraAnnotationNames (map name (:closure-extra-annotations opts)))))
202+
203+
compiler-options)
193204

194205
(defn ^CompilerOptions make-options
195206
"Create a CompilerOptions object and set options from opts map."
@@ -209,9 +220,6 @@
209220
(or (true? val)
210221
(false? val)) (.setDefineToBooleanLiteral compiler-options key val)
211222
:else (println "value for" key "must be string, int, float, or bool"))))
212-
(doseq [[type level] (:closure-warnings opts)]
213-
(. compiler-options
214-
(setWarningLevel (type warning-types) (level check-level))))
215223
(if-let [extra-annotations (:closure-extra-annotations opts)]
216224
(. compiler-options (setExtraAnnotationNames (map name extra-annotations))))
217225
(when (contains? opts :source-map)
@@ -1259,12 +1267,19 @@
12591267
:commonjs
12601268
module-type)))
12611269

1270+
(defn make-convert-js-module-options [opts]
1271+
(-> opts
1272+
(select-keys [:closure-warnings
1273+
:pretty-print
1274+
:closure-extra-annotations])
1275+
(set-options (CompilerOptions.))))
1276+
12621277
(util/compile-if can-convert-commonjs?
12631278
(defmethod convert-js-module :commonjs [js opts]
12641279
(let [{:keys [file module-type]} js
12651280
^List externs '()
12661281
^List source-files (get-source-files module-type opts)
1267-
^CompilerOptions options (CompilerOptions.)
1282+
^CompilerOptions options (make-convert-js-module-options opts)
12681283
closure-compiler (doto (make-closure-compiler)
12691284
(.init externs source-files options))
12701285
es6-loader (if is-new-es6-loader?
@@ -1276,14 +1291,15 @@
12761291
(when (= (:module-type js) :amd)
12771292
(.process (TransformAMDToCJSModule. closure-compiler) nil root)))
12781293
(.process cjs nil root)
1294+
(report-failure (.getResult closure-compiler))
12791295
(.toSource closure-compiler root))))
12801296

12811297
(util/compile-if can-convert-es6?
12821298
(defmethod convert-js-module :es6 [js opts]
12831299
(let [{:keys [file module-type]} js
12841300
^List externs '()
12851301
^List source-files (get-source-files module-type opts)
1286-
^CompilerOptions options (doto (CompilerOptions.)
1302+
^CompilerOptions options (doto (make-convert-js-module-options opts)
12871303
(.setLanguageIn CompilerOptions$LanguageMode/ECMASCRIPT6)
12881304
(.setLanguageOut CompilerOptions$LanguageMode/ECMASCRIPT5))
12891305
closure-compiler (doto (make-closure-compiler)
@@ -1294,6 +1310,7 @@
12941310
cjs (ProcessEs6Modules. closure-compiler es6-loader true)
12951311
^Node root (get-root-node file closure-compiler)]
12961312
(.processFile cjs root)
1313+
(report-failure (.getResult closure-compiler))
12971314
(.toSource closure-compiler root))))
12981315

12991316
(defmethod convert-js-module :default [js opts]

0 commit comments

Comments
 (0)