Skip to content

Commit 1d86a8e

Browse files
Tests for platform preference
1 parent 5702f22 commit 1d86a8e

File tree

2 files changed

+55
-31
lines changed

2 files changed

+55
-31
lines changed

src/compiler.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,41 @@ enum Platform {
2727
JAVA = 'java',
2828
JAVASCRIPT = 'javascript',
2929
}
30+
const DEFAULT_PLATFORM_PRECEDENCE = [Platform.NATIVE, Platform.JAVA, Platform.JAVASCRIPT];
3031

3132
/**
32-
* Splits user `prefer` option from compiler options object
33+
* Splits user `platform` option from compiler options object
3334
* returns new object containing options and preferred platform.
3435
* @param {CompileOptions} content - compiler options object
3536
* @return {Object}
3637
* @example in rollup.config.js
37-
* buble(),
3838
* compiler({
39-
* prefer: 'javascript',
39+
* platform: 'javascript',
4040
* }),
4141
*/
42-
function filterContent(content: CompileOptions) {
43-
let prefer: any = '';
44-
if ('prefer' in content) {
45-
prefer = content['prefer'];
46-
delete content.prefer;
47-
};
48-
const res = { config: content, prefer: prefer };
49-
return res;
42+
function filterContent(content: CompileOptions): [CompileOptions, Platform] {
43+
let platform: string = '';
44+
if ('platform' in content && typeof content.platform === 'string') {
45+
platform = content.platform;
46+
delete content.platform;
47+
}
48+
return [content, platform as Platform];
5049
}
5150

5251
/**
53-
* Finds prefered user platform precedence in list of defaults
54-
* and re-orders the list with prefered option first.
55-
* @param {Array} haystack - array of allowed platform strings
56-
* @param {String} needle - preferred platform string
52+
* Reorders platform preferences based on configuration.
53+
* @param {String} platformPreference - preferred platform string
5754
* @return {Array}
5855
*/
59-
function reOrder(haystack: string[], needle: string) {
60-
const index = haystack.indexOf(needle);
61-
const precedent = haystack.splice(index, 1);
62-
return precedent.concat(haystack);
63-
}
56+
function orderPlatforms(platformPreference: Platform | string): Array<Platform> {
57+
if (platformPreference === '') {
58+
return DEFAULT_PLATFORM_PRECEDENCE;
59+
}
6460

65-
const PLATFORM_PRECEDENCE = [Platform.NATIVE, Platform.JAVA, Platform.JAVASCRIPT];
61+
const index = DEFAULT_PLATFORM_PRECEDENCE.indexOf(platformPreference as Platform);
62+
const newPlatformPreferences = DEFAULT_PLATFORM_PRECEDENCE.splice(index, 1);
63+
return newPlatformPreferences.concat(DEFAULT_PLATFORM_PRECEDENCE);
64+
}
6665

6766
/**
6867
* Run Closure Compiler and `postCompilation` Transforms on input source.
@@ -75,19 +74,11 @@ export default function(
7574
transforms: Array<Transform>,
7675
): Promise<string> {
7776
return new Promise((resolve: (stdOut: string) => void, reject: (error: any) => void) => {
78-
79-
const options = filterContent(compileOptions);
80-
81-
const { prefer, config } = options;
82-
83-
const USER_PLATFORM_PRECEDENCE = (prefer !== '') ? reOrder(PLATFORM_PRECEDENCE, prefer) : PLATFORM_PRECEDENCE;
84-
77+
const [config, platform] = filterContent(compileOptions);
8578
const instance = new compiler(config);
86-
87-
const firstSupportedPlatform = getFirstSupportedPlatform(USER_PLATFORM_PRECEDENCE);
79+
const firstSupportedPlatform = getFirstSupportedPlatform(orderPlatforms(platform));
8880

8981
if (firstSupportedPlatform !== Platform.JAVA) {
90-
9182
// TODO(KB): Provide feedback on this API. It's a little strange to nullify the JAR_PATH
9283
// and provide a fake java path.
9384
instance.JAR_PATH = null;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2019 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 options from '../../transpile/options';
19+
20+
test('platform unspecified is respected', t => {
21+
const typical = options({}, 'let x = 1;', []);
22+
23+
t.is(typical[0].platform, undefined);
24+
});
25+
26+
test('platform javascript is respected', t => {
27+
const javascriptPlatform = options({
28+
platform: 'javascript',
29+
}, 'let x = 1;', []);
30+
31+
t.is(javascriptPlatform[0].platform, 'javascript');
32+
});
33+

0 commit comments

Comments
 (0)