From 6e497022a3220fee3e4fcb3a41a284520bcc1069 Mon Sep 17 00:00:00 2001 From: Alan Orozco Date: Wed, 26 Feb 2020 11:05:40 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20Transform=20aliased=20configured?= =?UTF-8?q?=20components=20(#26541)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Partial for #26572. `transform-inline-configure-component` finds `configureComponent(MyConstructor, {foo: 'bar'})` calls and: 1. Inlines imported `MyConstructor` in current scope. 2. Replaces `*.*.STATIC_CONG_.foo` access with its value as defined in config object `{foo: 'bar'}`. 3. Replaces `configureComponent()` call with inlined `MyConstructor`'s id. For runtime use of `configureComponent` like: ```js import {MyConstructor} from './my-ctor'; const TAG = 'amp-foo'; const MyWrappedConstructor = configureComponent( MyConstructor, {TAG, foo: 'foo'} ); ``` Composed class gets aliased, hoisted and its configuration replaced inline: ```js import {something} from './imported-by-constructor'; class MyConstructor { constructor() { // references to config object are replaced here, from: // `console.log(this.STATIC_CONFIG_.foo)` console.log(_foo); // ids in global scope are kept: const TAG = TAG; // unset properties are replaced with undefined, so this would // get minified const something = undefined || 'default value'; // destructuring keeps only the default value when prop undefined: // `const {bar = 'default value'} = this.STATIC_CONFIG_` const bar = 'default value'; } } const _foo = 'foo'; const TAG = 'amp-foo'; const MyWrappedConstructor = MyConstructor; ``` --- .../index.js | 221 ++++++++++++++++++ .../destructuring/input-base-class.js | 39 ++++ .../destructuring/input.js | 23 ++ .../destructuring/options.json | 4 + .../destructuring/output.mjs | 51 ++++ .../direct-access/input-base-class.js | 30 +++ .../direct-access/input.js | 27 +++ .../direct-access/options.json | 4 + .../direct-access/output.mjs | 41 ++++ .../input-base-class.js | 22 ++ .../relative-imports/input.js | 24 ++ .../relative-imports/options.json | 4 + .../relative-imports/output.mjs | 28 +++ .../remove-assignment/input-base-class.js | 24 ++ .../remove-assignment/input.js | 18 ++ .../remove-assignment/options.json | 4 + .../remove-assignment/output.mjs | 22 ++ .../test/index.js | 19 ++ build-system/compile/build.conf.js | 1 + 19 files changed, 606 insertions(+) create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/index.js create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/input-base-class.js create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/input.js create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/options.json create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/output.mjs create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/input-base-class.js create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/input.js create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/options.json create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/output.mjs create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/input-nested-directory/input-base-class.js create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/input.js create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/options.json create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/output.mjs create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/input-base-class.js create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/input.js create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/options.json create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/output.mjs create mode 100644 build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/index.js diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/index.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/index.js new file mode 100644 index 000000000000..f83761fd9595 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/index.js @@ -0,0 +1,221 @@ +/** + * Copyright 2020 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. + */ +const {dirname, relative, join} = require('path'); +const {transformFileSync} = require('@babel/core'); + +/** + * @fileoverview + * Finds `configureComponent(MyConstructor, {foo: 'bar'})` calls and: + * + * 1. Inlines imported `MyConstructor` in current scope. + * + * 2. Replaces `*.STATIC_CONFIG_.foo` accesses to their value as + * defined in config object `{foo: 'bar'}`. + * + * 3. Replaces `configureComponent(...)` call with identifier for inlined, static + * `MyConstructor`. + */ + +const calleeName = 'configureComponent'; +const replacedMember = 'STATIC_CONFIG_'; + +/** + * Sub-plugin that transforms inlined file that exports wrapped constructor. + * @return {!Object} + */ +function transformRedefineInline({types: t}) { + const propValueNode = (propValues, key, opt_default) => + propValues[key] || opt_default || t.identifier('undefined'); + + function unjsdoc({leadingComments}) { + if (!leadingComments) { + return; + } + for (let i = 0; i < leadingComments.length; i++) { + const comment = leadingComments[i]; + comment.value = comment.value.replace(/^\*/, ' [removed]'); + } + } + + function unexport(path) { + if (path.node.declaration) { + path.replaceWith(path.node.declaration); + return; + } + path.remove(); + } + + return { + name: 'transform-redefine-inline', + visitor: { + ExportDefaultDeclaration: unexport, + ExportNamedDeclaration: unexport, + ExportAllDeclaration: unexport, + ImportDeclaration(path, {opts}) { + const {source} = path.node; + if (source.value.startsWith('.')) { + source.value = join(opts.from, source.value).replace(/^[^.]/, './$&'); + } + }, + MemberExpression(path, {opts}) { + // Handle x.y.{...}.$replacedMember prop accesses + if (!t.isIdentifier(path.node.property, {name: replacedMember})) { + return; + } + + const assignment = path.find( + ({parent, parentKey, parentPath}) => + parentKey == 'left' && + t.isAssignmentExpression(parent) && + t.isExpressionStatement(parentPath.parent) + ); + + if (assignment) { + unjsdoc(assignment.parentPath.parent); + assignment.parentPath.parentPath.remove(); + return; + } + + if ( + t.isMemberExpression(path.parent) && + t.isIdentifier(path.parent.property) + ) { + const {name} = path.parent.property; + path.parentPath.replaceWith(propValueNode(opts.propValues, name)); + return; + } + + if ( + t.isVariableDeclarator(path.parent) && + t.isObjectPattern(path.parent.id) && + t.isVariableDeclaration(path.parentPath.parent) + ) { + const assignments = path.parent.id.properties.map(({key, value}) => + t.variableDeclarator( + value.left || value, + propValueNode(opts.propValues, key.name, value.right) + ) + ); + path.parentPath.replaceWithMultiple(assignments); + } + }, + }, + }; +} + +/** + * Transforms using transformRedefineInline sub-plugin. + * @param {string} sourceFilename + * @param {!Object} opts + * @return {!Object} + */ +const redefineInline = (sourceFilename, opts) => + transformFileSync(sourceFilename.toString(), { + configFile: false, + code: false, + ast: true, + sourceType: 'module', + plugins: [[transformRedefineInline, opts]], + }); + +/** + * Replaces `configureComponent()` wrapping calls. + * @return {!Object} + */ +module.exports = function({types: t}) { + function getImportPath(nodes, name) { + for (let i = 0; i < nodes.length; i++) { + const node = nodes[i]; + if ( + t.isImportDeclaration(node) && + node.specifiers.find(({imported}) => t.isIdentifier(imported, {name})) + ) { + return node.source.value; + } + } + } + + return { + name: 'transform-inline-decl-extensions', + visitor: { + CallExpression(path, {file}) { + if (!t.isIdentifier(path.node.callee, {name: calleeName})) { + return; + } + + const [importedId, propsObj] = path.node.arguments; + if (!t.isIdentifier(importedId) || !t.isObjectExpression(propsObj)) { + return; + } + + const program = path.findParent(p => t.isProgram(p)); + + const importPath = getImportPath(program.node.body, importedId.name); + if (!importPath) { + return; + } + + for (const name in program.scope.bindings) { + if (name) { + path.scope.rename(name, program.scope.generateUid(name)); + } + } + + const propValues = Object.create(null); + + for (const {key, value} of propsObj.properties) { + const {name} = key; + + if (t.isMemberExpression(value)) { + throw path.buildCodeFrameError( + `${replacedMember} properties must not be assigned to members. ` + + 'Set necessary values to program-level constants.' + ); + } + + if (t.isIdentifier(value)) { + const binding = path.scope.getBinding(value.name); + if (!binding || !t.isProgram(binding.scope.block)) { + throw path.buildCodeFrameError( + `ids used in ${replacedMember} must be defined as ` + + 'program-level constants.' + ); + } + propValues[name] = value; + continue; + } + + const id = program.scope.generateUidIdentifier(name); + program.scope.push({id, init: value, kind: 'const'}); + propValues[name] = id; + } + + const currentDirname = dirname(file.opts.filename); + const importedModule = join(currentDirname, importPath); + + // TODO(go.amp.dev/issue/26948): sourcemaps + const importedInline = redefineInline(require.resolve(importedModule), { + propValues, + from: relative(currentDirname, dirname(importedModule)), + }); + + program.unshiftContainer('body', importedInline.ast.program.body); + + path.replaceWith(t.identifier(importedId.name)); + }, + }, + }; +}; diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/input-base-class.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/input-base-class.js new file mode 100644 index 000000000000..b6dc3ef7c861 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/input-base-class.js @@ -0,0 +1,39 @@ +/** + * Copyright 2020 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. + */ +const {a, b, c} = x.STATIC_CONFIG_; +const {a: a1, b: b1, c: c1} = x.y.STATIC_CONFIG_; + +export class Destructuring { + method() { + const {a, b: bRenamed, c} = this.STATIC_CONFIG_; + } + withDefaultValues() { + const { + a = 'default value for a', + b: renamedBbbb = 'default value for b', + c = 'default value for c', + d = 'default value for d', + e: renamedE = 'default value for e', + } = this.STATIC_CONFIG_; + } + unset() { + const { + a, + thisPropIsUnset, + thisPropIsUnsetToo, + } = this.STATIC_CONFIG_; + } +} diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/input.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/input.js new file mode 100644 index 000000000000..f62ac7b6ad7b --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/input.js @@ -0,0 +1,23 @@ +/** + * Copyright 2020 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. + */ +import {Destructuring} from './input-base-class'; +foo( + configureComponent(Destructuring, { + a: 'value for a', + b: 'value for b', + c: 'value for c', + }) +); diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/options.json b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/options.json new file mode 100644 index 000000000000..ece2997aa86d --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["../../../../"], + "sourceType": "module" +} diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/output.mjs b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/output.mjs new file mode 100644 index 000000000000..3afe6c613434 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/destructuring/output.mjs @@ -0,0 +1,51 @@ +const _a = 'value for a', + _b = 'value for b', + _c = 'value for c'; + +/** + * Copyright 2020 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. + */ +const a = _a, + b = _b, + c = _c; +const a1 = _a, + b1 = _b, + c1 = _c; + +class Destructuring { + method() { + const a = _a, + bRenamed = _b, + c = _c; + } + + withDefaultValues() { + const a = _a, + renamedBbbb = _b, + c = _c, + d = 'default value for d', + renamedE = 'default value for e'; + } + + unset() { + const a = _a, + thisPropIsUnset = undefined, + thisPropIsUnsetToo = undefined; + } + +} + +import { Destructuring as _Destructuring } from './input-base-class'; +foo(_Destructuring); diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/input-base-class.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/input-base-class.js new file mode 100644 index 000000000000..d901ebd08f67 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/input-base-class.js @@ -0,0 +1,30 @@ +/** + * Copyright 2020 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. + */ +export class DirectAccess { + setProps() { + this.STATIC_CONFIG_.foo; + somethingSomething(this.STATIC_CONFIG_.bar); + tacos(this.STATIC_CONFIG_.nestedObject.baz); + } + + unsetProps() { + return this.STATIC_CONFIG_.thisPropIsUnset; + } + + propsSetToIds() { + return this.STATIC_CONFIG_.scopedId; + } +} diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/input.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/input.js new file mode 100644 index 000000000000..8f58e6e44c00 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/input.js @@ -0,0 +1,27 @@ +/** + * Copyright 2020 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. + */ +import {DirectAccess} from './input-base-class'; + +const scopedId = 'value for scopedId'; + +foo( + configureComponent(DirectAccess, { + scopedId, + foo: 'value for foo', + bar: 'value for bar', + nestedObject: {foo: 'foo'}, + }) +); diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/options.json b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/options.json new file mode 100644 index 000000000000..ece2997aa86d --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["../../../../"], + "sourceType": "module" +} diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/output.mjs b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/output.mjs new file mode 100644 index 000000000000..31e2916bdcc1 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/direct-access/output.mjs @@ -0,0 +1,41 @@ +const _foo = 'value for foo', + _bar = 'value for bar', + _nestedObject = { + foo: 'foo' +}; + +/** + * Copyright 2020 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. + */ +class DirectAccess { + setProps() { + _foo; + somethingSomething(_bar); + tacos(_nestedObject.baz); + } + + unsetProps() { + return undefined; + } + + propsSetToIds() { + return _scopedId; + } + +} + +import { DirectAccess as _DirectAccess } from './input-base-class'; +const _scopedId = 'value for scopedId'; +foo(_DirectAccess); diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/input-nested-directory/input-base-class.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/input-nested-directory/input-base-class.js new file mode 100644 index 000000000000..519f1ddb2bb1 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/input-nested-directory/input-base-class.js @@ -0,0 +1,22 @@ +/** + * Copyright 2020 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. + */ +import '../../backwards'; +import './for/wards'; + +import {leaveThis} from 'alone'; +import {baz} from './foo/bar'; + +export class RelativeImports {} diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/input.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/input.js new file mode 100644 index 000000000000..43daed5b3f7d --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/input.js @@ -0,0 +1,24 @@ +/** + * Copyright 2020 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. + */ +import {RelativeImports} from './input-nested-directory/input-base-class'; + +foo( + configureComponent(RelativeImports, { + a: 'value for a', + b: 'value for b', + d: 'value for c', + }) +); diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/options.json b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/options.json new file mode 100644 index 000000000000..ece2997aa86d --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["../../../../"], + "sourceType": "module" +} diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/output.mjs b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/output.mjs new file mode 100644 index 000000000000..0d5fb9cbbab9 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/relative-imports/output.mjs @@ -0,0 +1,28 @@ +const _a = 'value for a', + _b = 'value for b', + _d = 'value for c'; + +/** + * Copyright 2020 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. + */ +import "../backwards"; +import "./input-nested-directory/for/wards"; +import { leaveThis } from 'alone'; +import { baz } from "./input-nested-directory/foo/bar"; + +class RelativeImports {} + +import { RelativeImports as _RelativeImports } from './input-nested-directory/input-base-class'; +foo(_RelativeImports); diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/input-base-class.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/input-base-class.js new file mode 100644 index 000000000000..99c46a77faec --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/input-base-class.js @@ -0,0 +1,24 @@ +/** + * Copyright 2020 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. + */ +export class RemoveAssignment { + method() { + /** @private {something} */ + this.STATIC_CONFIG_ = {}; + + /** @private {something} */ + this.STATIC_CONFIG_.whatever = 'foo'; + } +} diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/input.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/input.js new file mode 100644 index 000000000000..6073bbe36711 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/input.js @@ -0,0 +1,18 @@ +/** + * Copyright 2020 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. + */ +import {RemoveAssignment} from './input-base-class'; + +foo(configureComponent(RemoveAssignment, {})); diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/options.json b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/options.json new file mode 100644 index 000000000000..ece2997aa86d --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["../../../../"], + "sourceType": "module" +} diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/output.mjs b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/output.mjs new file mode 100644 index 000000000000..4dd0e7d73a75 --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/fixtures/inline-configure-component/remove-assignment/output.mjs @@ -0,0 +1,22 @@ +/** + * Copyright 2020 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. + */ +class RemoveAssignment { + method() {} + +} + +import { RemoveAssignment as _RemoveAssignment } from './input-base-class'; +foo(_RemoveAssignment); diff --git a/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/index.js b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/index.js new file mode 100644 index 000000000000..bb2e50c56b6a --- /dev/null +++ b/build-system/babel-plugins/babel-plugin-transform-inline-configure-component/test/index.js @@ -0,0 +1,19 @@ +/** + * Copyright 2020 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. + */ + +const runner = require('@babel/helper-plugin-test-runner').default; + +runner(__dirname); diff --git a/build-system/compile/build.conf.js b/build-system/compile/build.conf.js index bfdf72909a0b..f5bae95e66de 100644 --- a/build-system/compile/build.conf.js +++ b/build-system/compile/build.conf.js @@ -20,6 +20,7 @@ const localPlugin = name => require.resolve(`../babel-plugins/babel-plugin-${name}`); const defaultPlugins = isEsmBuild => [ + localPlugin('transform-inline-configure-component'), // TODO(alanorozco): Remove `replaceCallArguments` once serving infra is up. [localPlugin('transform-log-methods'), {replaceCallArguments: false}], localPlugin('transform-parenthesize-expression'),