-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Exports, Add UTF-8 Tests (#277)
* Better debugging * utf8 import tests * Test now pass * reenable utf8 test for es5
- Loading branch information
1 parent
4eb87fa
commit 1ff3e2f
Showing
40 changed files
with
437 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
*/ | ||
|
||
import { ExpressionStatement, AssignmentExpression, MemberExpression } from 'estree'; | ||
import { ExportDetails, Range } from '../types'; | ||
import MagicString from 'magic-string'; | ||
|
||
export function PreserveDefault( | ||
code: string, | ||
source: MagicString, | ||
ancestor: ExpressionStatement, | ||
exportDetails: ExportDetails, | ||
exportInline: boolean, | ||
): boolean { | ||
const assignmentExpression = ancestor.expression as AssignmentExpression; | ||
const memberExpression = assignmentExpression.left as MemberExpression; | ||
const [memberExpressionStart, memberExpressionEnd]: Range = memberExpression.range as Range; | ||
|
||
source.overwrite( | ||
memberExpressionStart, | ||
memberExpressionEnd + assignmentExpression.operator.length, | ||
'export default ', | ||
); | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* 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 { | ||
ExpressionStatement, | ||
AssignmentExpression, | ||
FunctionExpression, | ||
MemberExpression, | ||
} from 'estree'; | ||
import { ExportDetails, Range } from '../types'; | ||
import MagicString from 'magic-string'; | ||
|
||
function PreserveFunction( | ||
code: string, | ||
source: MagicString, | ||
ancestor: ExpressionStatement, | ||
exportDetails: ExportDetails, | ||
exportInline: boolean, | ||
): boolean { | ||
// Function Expressions can be inlined instead of preserved as variable references. | ||
// window['foo'] = function(){}; => export function foo(){} / function foo(){} | ||
const assignmentExpression = ancestor.expression as AssignmentExpression; | ||
const memberExpression = assignmentExpression.left as MemberExpression; | ||
const functionExpression = assignmentExpression.right as FunctionExpression; | ||
const [memberExpressionObjectStart] = memberExpression.object.range as Range; | ||
const functionName = exportInline ? exportDetails.exported : exportDetails.local; | ||
|
||
if (functionExpression.params.length > 0) { | ||
const [paramsStart] = functionExpression.params[0].range as Range; | ||
// FunctionExpression has parameters. | ||
source.overwrite( | ||
memberExpressionObjectStart, | ||
paramsStart, | ||
`${exportInline ? 'export ' : ''}function ${functionName}(`, | ||
); | ||
} else { | ||
const [bodyStart] = functionExpression.body.range as Range; | ||
source.overwrite( | ||
memberExpressionObjectStart, | ||
bodyStart, | ||
`${exportInline ? 'export ' : ''}function ${functionName}()`, | ||
); | ||
} | ||
|
||
return !exportInline; | ||
} | ||
|
||
function PreserveIdentifier( | ||
code: string, | ||
source: MagicString, | ||
ancestor: ExpressionStatement, | ||
exportDetails: ExportDetails, | ||
exportInline: boolean, | ||
): boolean { | ||
const assignmentExpression = ancestor.expression as AssignmentExpression; | ||
const left = assignmentExpression.left; | ||
const right = assignmentExpression.right; | ||
const [ancestorStart, ancestorEnd]: Range = ancestor.range as Range; | ||
const [rightStart, rightEnd]: Range = right.range as Range; | ||
|
||
if (exportInline) { | ||
source.overwrite( | ||
ancestorStart, | ||
ancestorEnd, | ||
`export var ${exportDetails.exported}=${code.substring(rightStart, rightEnd)};`, | ||
); | ||
} else if (exportDetails.source === null && 'name' in right) { | ||
// This is a locally defined identifier with a name we can use. | ||
exportDetails.local = right.name; | ||
source.remove((left.range as Range)[0], rightEnd + 1); | ||
return true; | ||
} else { | ||
// exportDetails.local = | ||
source.overwrite( | ||
ancestorStart, | ||
ancestorEnd, | ||
`var ${exportDetails.local}=${code.substring(rightStart, rightEnd)};`, | ||
); | ||
} | ||
|
||
return !exportInline; | ||
} | ||
|
||
export function PreserveNamedConstant( | ||
code: string, | ||
source: MagicString, | ||
ancestor: ExpressionStatement, | ||
exportDetails: ExportDetails, | ||
exportInline: boolean, | ||
): boolean { | ||
const assignmentExpression = ancestor.expression as AssignmentExpression; | ||
switch (assignmentExpression.right.type) { | ||
case 'FunctionExpression': | ||
return PreserveFunction(code, source, ancestor, exportDetails, exportInline); | ||
default: | ||
return PreserveIdentifier(code, source, ancestor, exportDetails, exportInline); | ||
} | ||
} |
Oops, something went wrong.