diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/index.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/index.js index bd4d21a2a265..c7b88407d590 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/index.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/index.js @@ -14,111 +14,137 @@ * limitations under the License. */ -function isRemovableMethod(t, node, names) { - if (!node || !t.isIdentifier(node)) { - return false; - } - return names.some(x => { - return t.isIdentifier(node, {name: x}); - }); -} - const typeMap = { - 'assertElement': '!Element', + 'assertElement': 'Element', 'assertString': 'string', 'assertNumber': 'number', 'assertBoolean': 'boolean', - 'assertArray': '!Array', + 'assertArray': 'Array', }; -const removableDevAsserts = [ - 'assert', - 'fine', - 'assertElement', - 'assertString', - 'assertNumber', - 'assertBoolean', - 'assertArray', -]; - -const removableUserAsserts = ['fine']; +const REMOVABLE = { + dev: [ + 'assert', + 'fine', + 'assertElement', + 'assertString', + 'assertNumber', + 'assertBoolean', + 'assertArray', + ], + user: ['fine'], +}; module.exports = function(babel) { - const {types: t} = babel; + const {types: t, template} = babel; + + /** + * @param {!NodePath} path + * @param {!Array} names + * @return {boolean} + */ + function isRemovableMethod(path, names) { + return names.some(name => { + return path.isIdentifier({name}); + }); + } + + /** + * @param {!NodePath} path + * @param {boolean} assertion + * @param {string|undefined} type + */ + function eliminate(path, assertion, type) { + const argument = path.get('arguments.0'); + if (!argument) { + if (assertion) { + throw path.buildCodeFrameError('assertion without a parameter!'); + } + + // This is to resolve right hand side usage of expression where + // no argument is passed in. This bare undefined value is eventually + // stripped by Closure Compiler. + path.replaceWith(t.identifier('undefined')); + return; + } + + const arg = argument.node; + if (assertion) { + const evaluation = argument.evaluate(); + + // If we can statically evaluate the value to a falsey expression + if (evaluation.confident) { + if (type) { + if (typeof evaluation.value !== type) { + path.replaceWith(template.ast` + (function() { + throw new Error('static type assertion failure'); + }()); + `); + return; + } + } else if (!evaluation.value) { + path.replaceWith(template.ast` + (function() { + throw new Error('static assertion failure'); + }()); + `); + return; + } + } + } + + path.replaceWith(t.parenthesizedExpression(arg)); + + if (type) { + // If it starts with a capital, make the type non-nullable. + if (/^[A-Z]/.test(type)) { + type = '!' + type; + } + // Add a cast annotation to fix type. + path.addComment('leading', `* @type {${type}} `); + } + + const {parenthesized} = path.node.extra || {}; + if (parenthesized) { + path.replaceWith(t.parenthesizedExpression(path.node)); + } + } + return { visitor: { CallExpression(path) { - const {node} = path; - const {callee} = node; - const {parenthesized} = node.extra || {}; + const callee = path.get('callee'); + + if (callee.isIdentifier({name: 'devAssert'})) { + return eliminate(path, true, ''); + } - const isDirectCallExpression = t.isIdentifier(callee, { - name: 'devAssert', - }); const isMemberAndCallExpression = - t.isMemberExpression(callee) && t.isCallExpression(callee.object); + callee.isMemberExpression() && + callee.get('object').isCallExpression(); - if (!(isDirectCallExpression || isMemberAndCallExpression)) { + if (!isMemberAndCallExpression) { return; } - // `property` here is the identifier we want to evaluate such as the - // `devAssert` in a direct call or the `assert` in a `dev().assert()` - // call. - let property = null; - let args = null; - // `type` is left null in a direct call expression like `devAssert` - let type = null; - - if (isDirectCallExpression) { - property = node.callee; - args = node.arguments[0]; - } else if (isMemberAndCallExpression) { - property = callee.property; - - const logCallee = callee.object.callee; - const isRemovableDevCall = - t.isIdentifier(logCallee, {name: 'dev'}) && - isRemovableMethod(t, property, removableDevAsserts); - - const isRemovableUserCall = - t.isIdentifier(logCallee, {name: 'user'}) && - isRemovableMethod(t, property, removableUserAsserts); - - if (!(isRemovableDevCall || isRemovableUserCall)) { - return; - } - args = path.node.arguments[0]; - type = typeMap[property.name]; + const logCallee = callee.get('object.callee'); + let removable = []; + + if ( + logCallee.isIdentifier({name: 'dev'}) || + logCallee.isIdentifier({name: 'user'}) + ) { + removable = REMOVABLE[logCallee.node.name]; } - if (args) { - if (parenthesized) { - path.replaceWith(t.parenthesizedExpression(args)); - path.skip(); - // If it is not an assert type, we won't need to do type annotation. - // If it has no type that we can cast to, then we also won't need to - // do type annotation. - } else if (!property.name.startsWith('assert') || !type) { - path.replaceWith(args); - } else { - // Special case null value argument since it's mostly used for - // interface methods with no implementation which will most likely - // get DCE'd by Closure Compiler since they are unused code methods. - if (args.type === 'NullLiteral') { - return; - } else { - path.replaceWith(t.parenthesizedExpression(args)); - // Add a cast annotation to fix type. - path.addComment('leading', `* @type {${type}} `); - } - } - } else { - // This is to resolve right hand side usage of expression where - // no argument is passed in. This bare undefined value is eventually - // stripped by Closure Compiler. - path.replaceWith(t.identifier('undefined')); + const prop = callee.get('property'); + if (!isRemovableMethod(prop, removable)) { + return; } + + const method = prop.node.name; + eliminate(path, method.startsWith('assert'), typeMap[method]); }, }, }; diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-dev-assert/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-dev-assert/input.js index b85484887bf3..3f14b75234ca 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-dev-assert/input.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-dev-assert/input.js @@ -15,4 +15,4 @@ */ user().assert(1 + 1); let result = user().assert(user(), 'hello', 'world'); -let result2 = user().assert(); +let result2 = user().assert(true); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-dev-assert/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-dev-assert/output.js index b85484887bf3..3f14b75234ca 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-dev-assert/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-dev-assert/output.js @@ -15,4 +15,4 @@ */ user().assert(1 + 1); let result = user().assert(user(), 'hello', 'world'); -let result2 = user().assert(); +let result2 = user().assert(true); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-userAssert/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-userAssert/output.js index 783eb6768f10..154ca3b5450b 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-userAssert/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/no-transform-userAssert/output.js @@ -14,7 +14,7 @@ * limitations under the License. */ userAssert(1 + 1); -userAssert(2 + 2); +userAssert((2 + 2)); userAssert(); let result = userAssert(dev(), 'hello', 'world'); let result2 = userAssert(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/preserve-parens/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/preserve-parens/output.js index fd193b570034..12c71af8ec96 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/preserve-parens/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/preserve-parens/output.js @@ -15,10 +15,14 @@ */ /** @type {x} */ + +/** @type {!Element} */ (dev()); function hello() { return ( + /** @type {!Element} */ + /** @type {x} */ (dev()) ); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean-non/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean-non/input.js new file mode 100644 index 000000000000..7668914ba7b1 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean-non/input.js @@ -0,0 +1,16 @@ +/** + * Copyright 2018 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +dev().assertBoolean(null); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean-non/options.json b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean-non/options.json new file mode 100644 index 000000000000..9f7ccda8f0e7 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean-non/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-transform-amp-asserts"] +} diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean-non/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean-non/output.js new file mode 100644 index 000000000000..2deb43744e5b --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean-non/output.js @@ -0,0 +1,18 @@ +/** + * Copyright 2018 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function () { + throw new Error('static type assertion failure'); +})(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean/input.js index c2249a764890..23b258faaa86 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean/input.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean/input.js @@ -17,4 +17,3 @@ const falsey = false; dev().assertBoolean(falsey); dev().assertBoolean(true); let result = dev().assertBoolean(false, 'hello', 'world'); -let result2 = dev().assertBoolean(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean/output.js index c4973511d871..b0cbb07e0e9e 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-boolean/output.js @@ -23,4 +23,3 @@ const falsey = false; let result = /** @type {boolean} */ (false); -let result2 = undefined; diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-element/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-element/input.js index 24b4b6e067d8..60d8a5251975 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-element/input.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-element/input.js @@ -15,4 +15,3 @@ */ dev().assertElement(element); let result = dev().assertElement(element, 'hello', 'world'); -let result2 = dev().assertElement(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-element/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-element/output.js index 4b42976cad9c..109bfd9a52fa 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-element/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-element/output.js @@ -19,4 +19,3 @@ let result = /** @type {!Element} */ (element); -let result2 = undefined; diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number-non/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number-non/input.js new file mode 100644 index 000000000000..7e78fd300345 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number-non/input.js @@ -0,0 +1,16 @@ +/** + * Copyright 2018 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +dev().assertNumber(null); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number-non/options.json b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number-non/options.json new file mode 100644 index 000000000000..9f7ccda8f0e7 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number-non/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-transform-amp-asserts"] +} diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number-non/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number-non/output.js new file mode 100644 index 000000000000..2deb43744e5b --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number-non/output.js @@ -0,0 +1,18 @@ +/** + * Copyright 2018 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function () { + throw new Error('static type assertion failure'); +})(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number/input.js index bea63f0fb7b9..b22567373263 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number/input.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number/input.js @@ -17,4 +17,4 @@ let num = 5; dev().assertNumber(num); dev().assertNumber(1 + 1); let result = dev().assertNumber(3, 'hello', 'world'); -let result2 = dev().assertNumber(); +dev().assertNumber(0); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number/output.js index e3406a937bda..f7361847231c 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-number/output.js @@ -23,4 +23,6 @@ let num = 5; let result = /** @type {number} */ (3); -let result2 = undefined; + +/** @type {number} */ +(0); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string-not-string-non/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string-not-string-non/input.js new file mode 100644 index 000000000000..437c670e9107 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string-not-string-non/input.js @@ -0,0 +1,16 @@ +/** + * Copyright 2018 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +dev().assertString(null); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string-not-string-non/options.json b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string-not-string-non/options.json new file mode 100644 index 000000000000..9f7ccda8f0e7 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string-not-string-non/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-transform-amp-asserts"] +} diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string-not-string-non/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string-not-string-non/output.js new file mode 100644 index 000000000000..2deb43744e5b --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string-not-string-non/output.js @@ -0,0 +1,18 @@ +/** + * Copyright 2018 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function () { + throw new Error('static type assertion failure'); +})(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string/input.js index 962ec3d2f7a6..7402b3c2d359 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string/input.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string/input.js @@ -17,5 +17,4 @@ let str = 'foo'; dev().assertString(str); dev().assertString('hello'); let result = dev().assertString('world'); -let result2 = dev().assertString(); -let result3 = dev().assertString(null); +let result2 = dev().assertString(''); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string/output.js index 90364214d448..b6f4a187e125 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert-string/output.js @@ -23,5 +23,6 @@ let str = 'foo'; let result = /** @type {string} */ ('world'); -let result2 = undefined; -let result3 = dev().assertString(null); +let result2 = +/** @type {string} */ +(''); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert/input.js index 1e45447ee8e0..bb509c0eab99 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert/input.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert/input.js @@ -15,6 +15,4 @@ */ dev().assert(1 + 1); dev().assert(dev().assert(2 + 2)); -dev().assert(); let result = dev().assert(dev(), 'hello', 'world'); -let result2 = dev().assert(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert/output.js index c7b098fed679..6169818473b8 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-dev-assert/output.js @@ -13,8 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -1 + 1; -2 + 2; -undefined; -let result = dev(); -let result2 = undefined; +(1 + 1); +((2 + 2)); +let result = (dev()); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-empty-string/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-empty-string/input.js new file mode 100644 index 000000000000..0ece38875290 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-empty-string/input.js @@ -0,0 +1,16 @@ +/** + * Copyright 2019 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +devAssert(''); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-empty-string/options.json b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-empty-string/options.json new file mode 100644 index 000000000000..9f7ccda8f0e7 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-empty-string/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-transform-amp-asserts"] +} diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-empty-string/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-empty-string/output.js new file mode 100644 index 000000000000..1b23996b7162 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-empty-string/output.js @@ -0,0 +1,18 @@ +/** + * Copyright 2019 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function () { + throw new Error('static assertion failure'); +})(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-false/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-false/input.js new file mode 100644 index 000000000000..ed957dacf406 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-false/input.js @@ -0,0 +1,16 @@ +/** + * Copyright 2019 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +devAssert(false); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-false/options.json b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-false/options.json new file mode 100644 index 000000000000..9f7ccda8f0e7 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-false/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-transform-amp-asserts"] +} diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-false/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-false/output.js new file mode 100644 index 000000000000..1b23996b7162 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-false/output.js @@ -0,0 +1,18 @@ +/** + * Copyright 2019 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function () { + throw new Error('static assertion failure'); +})(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-null/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-null/input.js new file mode 100644 index 000000000000..1bac650951bc --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-null/input.js @@ -0,0 +1,16 @@ +/** + * Copyright 2019 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +devAssert(null); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-null/options.json b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-null/options.json new file mode 100644 index 000000000000..9f7ccda8f0e7 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-null/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-transform-amp-asserts"] +} diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-null/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-null/output.js new file mode 100644 index 000000000000..1b23996b7162 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-null/output.js @@ -0,0 +1,18 @@ +/** + * Copyright 2019 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function () { + throw new Error('static assertion failure'); +})(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-zero/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-zero/input.js new file mode 100644 index 000000000000..09bca5fa5ee9 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-zero/input.js @@ -0,0 +1,16 @@ +/** + * Copyright 2019 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +devAssert(0); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-zero/options.json b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-zero/options.json new file mode 100644 index 000000000000..9f7ccda8f0e7 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-zero/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["../../../../../babel-plugin-transform-amp-asserts"] +} diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-zero/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-zero/output.js new file mode 100644 index 000000000000..1b23996b7162 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert-zero/output.js @@ -0,0 +1,18 @@ +/** + * Copyright 2019 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +(function () { + throw new Error('static assertion failure'); +})(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert/input.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert/input.js index cf5caa6619d5..2a55df63d337 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert/input.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert/input.js @@ -15,6 +15,4 @@ */ devAssert(1 + 1); devAssert(dev().assert(2 + 2)); -devAssert(); let result = devAssert(dev(), 'hello', 'world'); -let result2 = devAssert(); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert/output.js index 3a2ac018bcdb..83995bbdc16f 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-devAssert/output.js @@ -13,8 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -1 + 1; -2 + 2; -undefined; -let result = dev(); -let result2 = undefined; +(1 + 1); +((2 + 2)); +let result = (dev()); diff --git a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-user-fine/output.js b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-user-fine/output.js index 890202cf580c..e3c3190515ff 100644 --- a/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-user-fine/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-amp-asserts/test/fixtures/transform-assertions/transform-user-fine/output.js @@ -13,6 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -1; -let result = user(); +(1); +let result = (user()); let result2 = undefined; diff --git a/build-system/babel-plugins/babel-plugin-transform-html-template/test/fixtures/transform-assertions/other-template-literals/output.js b/build-system/babel-plugins/babel-plugin-transform-html-template/test/fixtures/transform-assertions/other-template-literals/output.js index f3e783f09c65..22f97010b0e9 100644 --- a/build-system/babel-plugins/babel-plugin-transform-html-template/test/fixtures/transform-assertions/other-template-literals/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-html-template/test/fixtures/transform-assertions/other-template-literals/output.js @@ -23,4 +23,4 @@ function test(string) { return string; } -console.log(test`string`); \ No newline at end of file +console.log(test`string`); diff --git a/build-system/babel-plugins/babel-plugin-transform-log-methods/test/fixtures/transform-log-methods/one-arg-variadic/output.js b/build-system/babel-plugins/babel-plugin-transform-log-methods/test/fixtures/transform-log-methods/one-arg-variadic/output.js index b564b5258e99..6aaa8c959331 100644 --- a/build-system/babel-plugins/babel-plugin-transform-log-methods/test/fixtures/transform-log-methods/one-arg-variadic/output.js +++ b/build-system/babel-plugins/babel-plugin-transform-log-methods/test/fixtures/transform-log-methods/one-arg-variadic/output.js @@ -24,4 +24,3 @@ devAssert(a + b, ["0", name]); userAssert(true, ["0", name]); user().assertElement(element, ["2", element]); dev().assertEnumValue(foo, bar, 'Unhandled because this argument is usually small'); -