Clasp: Automatically include polyfills for common methods that are valid typescript

Created on 7 May 2019  路  4Comments  路  Source: google/clasp

Not a bug-- feature proposal.

In my clasp projects, I often include a polyfill.js file that adds common ES6/ES7 methods to strings & arrays. For example:

  • String.includes
  • String.startswith
  • String.endswith
  • Array.includes

When writing GAS code in TypeScript, calling myString.startsWith("text") looks perfectly valid / doesn't raise any warnings in my editor, but when pushed & run GAS crashes, saying that function is not defined.

I think it could be a cool feature if clasp automatically detected some of those common methods being used in your code and created a polyfill.js, preventing run-time errors. I believe webpack does something like this?

Additionally / alternatively, clasp could warn you about methods in your code that won't run on GAS and can't be easily polyfilled.

I am willing to work on this if it seems like an appropriate addition!

Here is an example of a polyfill I often add:

polyfill.js

/**
 * Add "startsWith" method to strings
 * From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
 */
if (!String.prototype.startsWith) {
  Object.defineProperty(String.prototype, "startsWith", {
    value: function(search, pos) {
      pos = !pos || pos < 0 ? 0 : +pos;
      return this.substring(pos, pos + search.length) === search;
    }
  });
}

Most helpful comment

For reference, here is a draft cheat sheet with status of various ecmascript features and thier support in GAS https://docs.google.com/spreadsheets/d/1ClWEIgFJy22svbtkRWnRvTfJ7w_-Ofs48Pi9Ky4_DII/edit?usp=sharing

Also worth referencing, this es6 shim for GAS, bundled as a library. http://ramblings.mcpher.com/Home/excelquirks/gassnips/es6shim

All 4 comments

For reference, here is a draft cheat sheet with status of various ecmascript features and thier support in GAS https://docs.google.com/spreadsheets/d/1ClWEIgFJy22svbtkRWnRvTfJ7w_-Ofs48Pi9Ky4_DII/edit?usp=sharing

Also worth referencing, this es6 shim for GAS, bundled as a library. http://ramblings.mcpher.com/Home/excelquirks/gassnips/es6shim

@dustinmichels if you have idea on how it should be implemented, I am all ears.

@dustinmichels this issue should now be irrelevant with V8 engine support and PR #739

Amazing!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

freddy-daniel picture freddy-daniel  路  7Comments

grant picture grant  路  4Comments

imthenachoman picture imthenachoman  路  3Comments

OleksandrRakovets picture OleksandrRakovets  路  3Comments

smandava picture smandava  路  8Comments