Last active
April 20, 2023 08:44
-
-
Save justingrant/d4c41940a3c8d511114db53cb43b57f3 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# script to transform ecmascript.mjs to use exported functions | |
cd polyfill/lib | |
# Exports from ES2022 | |
sed -i '' 's/^const ES2022 =/export/' ecmascript.mjs | |
# Beginning and ending of ES object | |
sed -i '' 's/^export const ES = ObjectAssign({}, ES2022, {//' ecmascript.mjs | |
sed -i '' 's/^});//' ecmascript.mjs | |
# If nothing else is using ObjectAssign, delete its declaration | |
if [[ $(grep -c 'ObjectAssign' ecmascript.mjs) -eq 1 ]] | |
then | |
sed -i '' '/ObjectAssign/d' ecmascript.mjs | |
fi | |
# SystemUTCEpochNanoSeconds is an IIFE that returns a function | |
sed -i '' 's/ SystemUTCEpochNanoSeconds: (() => {/ export const SystemUTCEpochNanoSeconds = (() => {/' ecmascript.mjs | |
sed -i '' 's/ })(),/ })();/' ecmascript.mjs | |
# Remove commas after each function | |
sed -i '' 's/^ },$/ }/' ecmascript.mjs | |
# Non-arrow functions first line with one line of params | |
sed -E -i '' 's/^ ([a-zA-Z0-9_]+): \((.*)\) => {/ export function \1(\2) {/' ecmascript.mjs | |
# One-line arrow functions first line | |
sed -E -i '' 's/^ ([a-zA-Z0-9_]+): \((.*)\) => (.*),$/ export function \1(\2) { return \3; }/' ecmascript.mjs | |
# Multi-line arrow functions: IsTemporalDuration, IsTemporalTime, IsTemporalDateTime: | |
sed -E -i '' 's/^ ([a-zA-Z0-9_]+): \((.*)\) =>$/ export function \1(\2) { return (/' ecmascript.mjs | |
# Special cases for the end of those three functions | |
sed -E -i '' 's/^ ),$/ )); }/' ecmascript.mjs | |
sed -E -i '' 's/^ !HasSlot\(item, ISO_YEAR, ISO_MONTH, ISO_DAY\),$/ !HasSlot(item, ISO_YEAR, ISO_MONTH, ISO_DAY)); }/' ecmascript.mjs | |
sed -E -i '' 's/^ HasSlot\(item, YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS\),$/ HasSlot(item, YEARS, MONTHS, DAYS, HOURS, MINUTES, SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS)); }/' ecmascript.mjs | |
# Exported functions defined locally | |
sed -E -i '' 's/^ (ToPositiveIntegerWithTruncation|ToIntegerWithTruncation|ToIntegerIfIntegral),$//' ecmascript.mjs | |
sed -E -i '' 's/^const (.*) = \((.*)\) => {$/ export function \1(\2) {/' ecmascript.mjs | |
# Special case the REQUIRED symbol | |
sed -i '' "s/ REQUIRED: Symbol('~required~'),/ export const REQUIRED = Symbol('~required~');/" ecmascript.mjs | |
# Multi-line parameter lists | |
sed -E -i '' 's/^ \) => {$/ ) {/' ecmascript.mjs | |
# Non-arrow functions first line with multiple lines of params | |
sed -E -i '' 's/^ (.*): \(/ export function \1(/' ecmascript.mjs | |
# Special case for one function defined without an arrow but also without the function keyword | |
sed -E -i '' 's/^ DifferenceInstant\(/ export function DifferenceInstant(/' ecmascript.mjs | |
# Remove "ES." | |
sed -E -i '' 's/ES\.//g' ecmascript.mjs | |
# Add a blank line before each export (doubled blanks will be removed by prettier) | |
sed -E -i '' $'s/^ export/\\\n export/' ecmascript.mjs | |
# Fix imports in other polyfill files | |
for i in *.mjs; do sed -i '' 's/import { ES } from/import * as ES from/g' $i; done | |
# Fix imports in test code | |
cd ../test | |
for i in *.mjs; do sed -i '' 's/import { ES } from/import * as ES from/g' $i; done | |
cd ../.. | |
# Fix up spacing in ecmascript.mjs | |
npm run pretty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment