Skip to content

Commit 6fb767e

Browse files
Error reporting (#59)
* Ensure provided externs are leveraged * Ensure transformer externs are not overwritten by provided externs * Remove pasted type * Error Reporting tests * Logging indicates closure-compiler-npm might be swallowing the data * Tests for warnings regarding mixing warning_level and language_out
1 parent 25d5b29 commit 6fb767e

File tree

8 files changed

+440
-513
lines changed

8 files changed

+440
-513
lines changed

package-lock.json

Lines changed: 300 additions & 512 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ export default function(
5555
}
5656

5757
instance.run(async (exitCode: number, code: string, stdErr: string) => {
58-
if (exitCode !== 0) {
58+
if (
59+
'warning_level' in compileOptions &&
60+
compileOptions.warning_level === 'VERBOSE' &&
61+
stdErr !== ''
62+
) {
63+
reject(new Error(`Google Closure Compiler ${stdErr}`));
64+
} else if (exitCode !== 0) {
5965
reject(new Error(`Google Closure Compiler exit ${exitCode}: ${stdErr}`));
6066
} else {
6167
resolve(await postCompilation(code, chunk, transforms));

src/options.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import { ModuleFormat, OutputOptions } from 'rollup';
1919
import { CompileOptions } from 'google-closure-compiler';
2020
import { sync } from 'temp-write';
2121

22+
export const ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED =
23+
'Providing the warning_level=VERBOSE compile option also requires a valid language_out compile option.';
24+
export const ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_INVALID =
25+
'Providing the warning_level=VERBOSE and language_out=NO_TRANSPILE compile option will remove warnings.';
26+
2227
/**
2328
* Checks if output format is ESM
2429
* @param format
@@ -29,6 +34,20 @@ export const isESMFormat = (format?: ModuleFormat | 'esm'): boolean => {
2934
return format === 'esm' || format === 'es';
3035
};
3136

37+
/**
38+
* Throw Errors if compile options will result in unexpected behaviour.
39+
* @param compileOptions
40+
*/
41+
const validateCompileOptions = (compileOptions: CompileOptions): void => {
42+
if ('warning_level' in compileOptions && compileOptions.warning_level === 'VERBOSE') {
43+
if (!('language_out' in compileOptions)) {
44+
throw new Error(ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED);
45+
} else if ('language_out' in compileOptions && compileOptions.language_out === 'NO_TRANSPILE') {
46+
throw new Error(ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_INVALID);
47+
}
48+
}
49+
};
50+
3251
/**
3352
* Generate default Closure Compiler CompileOptions an author can override if they wish.
3453
* These must be derived from configuration or input sources.
@@ -88,6 +107,7 @@ export default function(
88107
const mapFile = sync('');
89108
let externs: Array<string> = [];
90109

110+
validateCompileOptions(compileOptions);
91111
if ('externs' in compileOptions) {
92112
switch (typeof compileOptions.externs) {
93113
case 'boolean':

test/closure-config/warning-level.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import test from 'ava';
18+
import compile, {ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED, ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_INVALID} from '../../transpile/options';
19+
20+
test('with no language out set, and warnings set to verbose... an error is returned', t => {
21+
try {
22+
compile({
23+
warning_level: 'VERBOSE',
24+
}, {
25+
format: 'es',
26+
}, 'var x = 1;', []);
27+
28+
t.fail('compile completed without throwing an error.');
29+
} catch (e) {
30+
t.is(e.message, ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_UNSPECIFIED);
31+
}
32+
});
33+
34+
test('with language out set to no_transpile, and warnings set to verbose... an error is returned', t => {
35+
try {
36+
compile({
37+
warning_level: 'VERBOSE',
38+
language_out: 'NO_TRANSPILE',
39+
}, {
40+
format: 'es',
41+
}, 'var x = 1;', []);
42+
43+
t.fail('compile completed without throwing an error.');
44+
} catch (e) {
45+
t.is(e.message, ERROR_WARNINGS_ENABLED_LANGUAGE_OUT_INVALID);
46+
}
47+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(document.getElementById(1));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(document.getElementById(1));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(document.getElementById(1));

test/error-reporting/warnings.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import test from 'ava';
18+
import compiler from '../../transpile';
19+
import * as rollup from 'rollup';
20+
import * as fs from 'fs';
21+
import { join } from 'path';
22+
import { promisify } from 'util';
23+
24+
const readFile = promisify(fs.readFile);
25+
const closureFlags = {
26+
default: {
27+
warning_level: 'VERBOSE',
28+
language_out: 'ECMASCRIPT5_STRICT',
29+
},
30+
advanced: {
31+
warning_level: 'VERBOSE',
32+
compilation_level: 'ADVANCED_OPTIMIZATIONS',
33+
language_out: 'ECMASCRIPT5_STRICT',
34+
},
35+
};
36+
37+
async function compile(name, option) {
38+
const bundle = await rollup.rollup({
39+
input: `test/${name}/fixtures/warnings.js`,
40+
plugins: [
41+
compiler(closureFlags[option]),
42+
],
43+
});
44+
45+
return {
46+
minified: await readFile(join(`test/${name}/fixtures/warnings.esm.${option}.js`), 'utf8'),
47+
code: (await bundle.generate({
48+
format: 'es',
49+
sourcemap: true,
50+
})).code,
51+
};
52+
}
53+
54+
Object.keys(closureFlags).forEach(option => {
55+
test(`provides warnings – es, ${option}`, async t => {
56+
try {
57+
await compile('error-reporting', option);
58+
t.fail('successfully built files without warning about input');
59+
} catch(e) {
60+
t.pass();
61+
}
62+
});
63+
});

0 commit comments

Comments
 (0)