Skip to content

Commit beee4b8

Browse files
Provided Externs and Generated Externs Equally Applied (#50)
* Ensure provided externs are leveraged * Ensure transformer externs are not overwritten by provided externs * Remove pasted type * Messaging for tests to clarify purpose
1 parent 332ac4a commit beee4b8

File tree

9 files changed

+806
-678
lines changed

9 files changed

+806
-678
lines changed

package-lock.json

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

src/options.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,26 @@ export default function(
8080
transforms: Array<Transform> | null,
8181
): [CompileOptions, string] {
8282
const mapFile = sync('');
83-
const externs = (compileOptions: CompileOptions): Array<string> => {
84-
if ('externs' in compileOptions) {
85-
switch (typeof compileOptions.externs) {
86-
case 'boolean':
87-
return [];
88-
case 'string':
89-
return [compileOptions.externs as string];
90-
default:
91-
return compileOptions.externs as Array<string>;
92-
}
83+
let externs: Array<string> = [];
84+
85+
if ('externs' in compileOptions) {
86+
switch (typeof compileOptions.externs) {
87+
case 'boolean':
88+
externs = [];
89+
break;
90+
case 'string':
91+
externs = [compileOptions.externs as string];
92+
break;
93+
default:
94+
externs = compileOptions.externs as Array<string>;
95+
break;
9396
}
9497

95-
return [];
96-
};
98+
delete compileOptions.externs;
99+
}
97100

98101
const options = {
99-
...defaults(outputOptions, externs(compileOptions), transforms),
102+
...defaults(outputOptions, externs, transforms),
100103
...compileOptions,
101104
js: sync(code),
102105
create_source_map: mapFile,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** @externs */
2+
var x = '';
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 from '../../transpile/options';
19+
import path from 'path';
20+
import fs from 'fs';
21+
import util from 'util';
22+
const readFile = util.promisify(fs.readFile);
23+
24+
const PROVIDED_EXTERN = path.resolve('test', 'closure-config', 'fixtures', 'externs.js');
25+
const IIFE_TRANSFORM_EXTERN_CONTENT = '/** @externs */ function wrapper(){}';
26+
27+
const IifeTransform = class {
28+
// Transforms have a public method `extern` that generates an extern
29+
// if one is needed for the transform.
30+
31+
// This test ensures the externs created by transforms are passed to
32+
// closure compiler when the caller also passes externs.
33+
extern() {
34+
return IIFE_TRANSFORM_EXTERN_CONTENT;
35+
}
36+
};
37+
38+
test('when rollup configuration specifies externs, extern is leveraged', async t => {
39+
t.plan(3);
40+
41+
const compilerOptionsExterns = compile({
42+
externs: PROVIDED_EXTERN,
43+
}, {
44+
format: 'iife',
45+
name: 'wrapper',
46+
}, 'var x = 1;', [new IifeTransform()])[0].externs;
47+
48+
t.is(compilerOptionsExterns.length, 2);
49+
t.true(compilerOptionsExterns.includes(PROVIDED_EXTERN));
50+
51+
// While we can use the path for the provided extern, we need to inspect the content of
52+
// the other extern to ensure it is the generated extern.
53+
// Externs are passed as filepaths to Closure Compiler.
54+
const fileContent = await readFile(compilerOptionsExterns.filter(path => path !== PROVIDED_EXTERN)[0], 'utf8');
55+
t.true(fileContent === IIFE_TRANSFORM_EXTERN_CONTENT);
56+
});

test/provided-externs/class.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 {generator, ESM_OUTPUT} from '../generator';
18+
const path = require('path');
19+
20+
const EXTERNS = path.resolve('test', 'provided-externs', 'fixtures', 'class.externs.js');
21+
22+
generator('provided-externs', 'class', [ESM_OUTPUT], {
23+
default: {
24+
externs: EXTERNS
25+
},
26+
advanced: {
27+
externs: EXTERNS,
28+
compilation_level: 'ADVANCED_OPTIMIZATIONS',
29+
language_out: 'ECMASCRIPT_2015',
30+
}
31+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class a{constructor(b){this.a=b}console(){console.log(this.a)}}var Exported=a;export{Exported};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class a{constructor(b){this.name_=b}console(){console.log(this.name_)}}var Exported=a;export{Exported};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** @externs */
2+
3+
/**
4+
* @constructor @struct
5+
*/
6+
var Exported = class {
7+
/**
8+
* @param {!string} name
9+
*/
10+
constructor(name) {}
11+
12+
/**
13+
* @return {string}
14+
*/
15+
console() {}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export class Exported {
2+
/**
3+
* @param {!string} name
4+
*/
5+
constructor(name) {
6+
/**
7+
* @private {!string}
8+
*/
9+
this.name_ = name;
10+
}
11+
12+
/**
13+
* @return {string}
14+
*/
15+
console() {
16+
console.log(this.name_);
17+
}
18+
}

0 commit comments

Comments
 (0)