|
| 1 | +/** |
| 2 | + * Copyright 2020 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 { |
| 18 | + ExpressionStatement, |
| 19 | + AssignmentExpression, |
| 20 | + FunctionExpression, |
| 21 | + MemberExpression, |
| 22 | +} from 'estree'; |
| 23 | +import { ExportDetails, Range } from '../types'; |
| 24 | +import MagicString from 'magic-string'; |
| 25 | + |
| 26 | +function PreserveFunction( |
| 27 | + code: string, |
| 28 | + source: MagicString, |
| 29 | + ancestor: ExpressionStatement, |
| 30 | + exportDetails: ExportDetails, |
| 31 | + exportInline: boolean, |
| 32 | +): boolean { |
| 33 | + // Function Expressions can be inlined instead of preserved as variable references. |
| 34 | + // window['foo'] = function(){}; => export function foo(){} / function foo(){} |
| 35 | + const assignmentExpression = ancestor.expression as AssignmentExpression; |
| 36 | + const memberExpression = assignmentExpression.left as MemberExpression; |
| 37 | + const functionExpression = assignmentExpression.right as FunctionExpression; |
| 38 | + const [memberExpressionObjectStart] = memberExpression.object.range as Range; |
| 39 | + const functionName = exportInline ? exportDetails.exported : exportDetails.local; |
| 40 | + |
| 41 | + if (functionExpression.params.length > 0) { |
| 42 | + const [paramsStart] = functionExpression.params[0].range as Range; |
| 43 | + // FunctionExpression has parameters. |
| 44 | + source.overwrite( |
| 45 | + memberExpressionObjectStart, |
| 46 | + paramsStart, |
| 47 | + `${exportInline ? 'export ' : ''}function ${functionName}(`, |
| 48 | + ); |
| 49 | + } else { |
| 50 | + const [bodyStart] = functionExpression.body.range as Range; |
| 51 | + source.overwrite( |
| 52 | + memberExpressionObjectStart, |
| 53 | + bodyStart, |
| 54 | + `${exportInline ? 'export ' : ''}function ${functionName}()`, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + return !exportInline; |
| 59 | +} |
| 60 | + |
| 61 | +function PreserveIdentifier( |
| 62 | + code: string, |
| 63 | + source: MagicString, |
| 64 | + ancestor: ExpressionStatement, |
| 65 | + exportDetails: ExportDetails, |
| 66 | + exportInline: boolean, |
| 67 | +): boolean { |
| 68 | + const assignmentExpression = ancestor.expression as AssignmentExpression; |
| 69 | + const left = assignmentExpression.left; |
| 70 | + const right = assignmentExpression.right; |
| 71 | + const [ancestorStart, ancestorEnd]: Range = ancestor.range as Range; |
| 72 | + const [rightStart, rightEnd]: Range = right.range as Range; |
| 73 | + |
| 74 | + if (exportInline) { |
| 75 | + source.overwrite( |
| 76 | + ancestorStart, |
| 77 | + ancestorEnd, |
| 78 | + `export var ${exportDetails.exported}=${code.substring(rightStart, rightEnd)};`, |
| 79 | + ); |
| 80 | + } else if (exportDetails.source === null && 'name' in right) { |
| 81 | + // This is a locally defined identifier with a name we can use. |
| 82 | + exportDetails.local = right.name; |
| 83 | + source.remove((left.range as Range)[0], rightEnd + 1); |
| 84 | + return true; |
| 85 | + } else { |
| 86 | + // exportDetails.local = |
| 87 | + source.overwrite( |
| 88 | + ancestorStart, |
| 89 | + ancestorEnd, |
| 90 | + `var ${exportDetails.local}=${code.substring(rightStart, rightEnd)};`, |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + return !exportInline; |
| 95 | +} |
| 96 | + |
| 97 | +export function PreserveNamedConstant( |
| 98 | + code: string, |
| 99 | + source: MagicString, |
| 100 | + ancestor: ExpressionStatement, |
| 101 | + exportDetails: ExportDetails, |
| 102 | + exportInline: boolean, |
| 103 | +): boolean { |
| 104 | + const assignmentExpression = ancestor.expression as AssignmentExpression; |
| 105 | + switch (assignmentExpression.right.type) { |
| 106 | + case 'FunctionExpression': |
| 107 | + return PreserveFunction(code, source, ancestor, exportDetails, exportInline); |
| 108 | + default: |
| 109 | + return PreserveIdentifier(code, source, ancestor, exportDetails, exportInline); |
| 110 | + } |
| 111 | +} |
0 commit comments