Convert some .js files to typescript

* Rename some .js files to .ts
* Add minor fixes for eslint problems
* Add "export {};" line to files without imports and exports to mark
  them as modules
* Updates server.go to handle imports without file extension correctly.

Change-Id: I4d4f1eaa689c6f351e64a2c3cfa4e8ac2d2e1f73
diff --git a/polygerrit-ui/app/elements/diff/gr-ranged-comment-themes/gr-ranged-comment-theme.js b/polygerrit-ui/app/elements/diff/gr-ranged-comment-themes/gr-ranged-comment-theme.ts
similarity index 85%
rename from polygerrit-ui/app/elements/diff/gr-ranged-comment-themes/gr-ranged-comment-theme.js
rename to polygerrit-ui/app/elements/diff/gr-ranged-comment-themes/gr-ranged-comment-theme.ts
index 49ed980..70ee196 100644
--- a/polygerrit-ui/app/elements/diff/gr-ranged-comment-themes/gr-ranged-comment-theme.js
+++ b/polygerrit-ui/app/elements/diff/gr-ranged-comment-themes/gr-ranged-comment-theme.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-ranged-comment-theme">
@@ -38,4 +44,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/elements/diff/gr-syntax-themes/gr-syntax-theme.js b/polygerrit-ui/app/elements/diff/gr-syntax-themes/gr-syntax-theme.ts
similarity index 94%
rename from polygerrit-ui/app/elements/diff/gr-syntax-themes/gr-syntax-theme.js
rename to polygerrit-ui/app/elements/diff/gr-syntax-themes/gr-syntax-theme.ts
index 76a01de..ac015e1 100644
--- a/polygerrit-ui/app/elements/diff/gr-syntax-themes/gr-syntax-theme.js
+++ b/polygerrit-ui/app/elements/diff/gr-syntax-themes/gr-syntax-theme.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-syntax-theme">
@@ -118,4 +124,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/elements/shared/gr-icons/gr-icons.js b/polygerrit-ui/app/elements/shared/gr-icons/gr-icons.ts
similarity index 99%
rename from polygerrit-ui/app/elements/shared/gr-icons/gr-icons.js
rename to polygerrit-ui/app/elements/shared/gr-icons/gr-icons.ts
index 5ffe028..ccaf40e 100644
--- a/polygerrit-ui/app/elements/shared/gr-icons/gr-icons.js
+++ b/polygerrit-ui/app/elements/shared/gr-icons/gr-icons.ts
@@ -14,8 +14,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-import '@polymer/iron-icon/iron-icon.js';
-import '@polymer/iron-iconset-svg/iron-iconset-svg.js';
+import '@polymer/iron-icon/iron-icon';
+import '@polymer/iron-iconset-svg/iron-iconset-svg';
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<iron-iconset-svg name="gr-icons" size="24">
diff --git a/polygerrit-ui/app/styles/dashboard-header-styles.js b/polygerrit-ui/app/styles/dashboard-header-styles.ts
similarity index 88%
rename from polygerrit-ui/app/styles/dashboard-header-styles.js
rename to polygerrit-ui/app/styles/dashboard-header-styles.ts
index 683202e..2354f65 100644
--- a/polygerrit-ui/app/styles/dashboard-header-styles.js
+++ b/polygerrit-ui/app/styles/dashboard-header-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="dashboard-header-styles">
@@ -55,4 +61,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/gr-change-list-styles.js b/polygerrit-ui/app/styles/gr-change-list-styles.ts
similarity index 95%
rename from polygerrit-ui/app/styles/gr-change-list-styles.js
rename to polygerrit-ui/app/styles/gr-change-list-styles.ts
index a7f231b..25d7f52 100644
--- a/polygerrit-ui/app/styles/gr-change-list-styles.js
+++ b/polygerrit-ui/app/styles/gr-change-list-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-change-list-styles">
@@ -190,4 +196,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/gr-change-metadata-shared-styles.js b/polygerrit-ui/app/styles/gr-change-metadata-shared-styles.ts
similarity index 89%
rename from polygerrit-ui/app/styles/gr-change-metadata-shared-styles.js
rename to polygerrit-ui/app/styles/gr-change-metadata-shared-styles.ts
index aabdde5..3d07d2e 100644
--- a/polygerrit-ui/app/styles/gr-change-metadata-shared-styles.js
+++ b/polygerrit-ui/app/styles/gr-change-metadata-shared-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-change-metadata-shared-styles">
@@ -55,4 +61,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/gr-change-view-integration-shared-styles.js b/polygerrit-ui/app/styles/gr-change-view-integration-shared-styles.ts
similarity index 91%
rename from polygerrit-ui/app/styles/gr-change-view-integration-shared-styles.js
rename to polygerrit-ui/app/styles/gr-change-view-integration-shared-styles.ts
index 4bfb742..57c8d78 100644
--- a/polygerrit-ui/app/styles/gr-change-view-integration-shared-styles.js
+++ b/polygerrit-ui/app/styles/gr-change-view-integration-shared-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-change-view-integration-shared-styles">
@@ -70,4 +76,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/gr-form-styles.js b/polygerrit-ui/app/styles/gr-form-styles.ts
similarity index 94%
rename from polygerrit-ui/app/styles/gr-form-styles.js
rename to polygerrit-ui/app/styles/gr-form-styles.ts
index 91763c5..3284ad5 100644
--- a/polygerrit-ui/app/styles/gr-form-styles.js
+++ b/polygerrit-ui/app/styles/gr-form-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-form-styles">
@@ -124,4 +130,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/gr-menu-page-styles.js b/polygerrit-ui/app/styles/gr-menu-page-styles.ts
similarity index 91%
rename from polygerrit-ui/app/styles/gr-menu-page-styles.js
rename to polygerrit-ui/app/styles/gr-menu-page-styles.ts
index e52a895..8e8b264 100644
--- a/polygerrit-ui/app/styles/gr-menu-page-styles.js
+++ b/polygerrit-ui/app/styles/gr-menu-page-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-menu-page-styles">
@@ -79,4 +85,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/gr-page-nav-styles.js b/polygerrit-ui/app/styles/gr-page-nav-styles.ts
similarity index 91%
rename from polygerrit-ui/app/styles/gr-page-nav-styles.js
rename to polygerrit-ui/app/styles/gr-page-nav-styles.ts
index 97f1a03..9010b2d 100644
--- a/polygerrit-ui/app/styles/gr-page-nav-styles.js
+++ b/polygerrit-ui/app/styles/gr-page-nav-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-page-nav-styles">
@@ -72,4 +78,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/gr-subpage-styles.js b/polygerrit-ui/app/styles/gr-subpage-styles.ts
similarity index 85%
rename from polygerrit-ui/app/styles/gr-subpage-styles.js
rename to polygerrit-ui/app/styles/gr-subpage-styles.ts
index f94cc9c..640da66 100644
--- a/polygerrit-ui/app/styles/gr-subpage-styles.js
+++ b/polygerrit-ui/app/styles/gr-subpage-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-subpage-styles">
@@ -42,4 +48,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/gr-table-styles.js b/polygerrit-ui/app/styles/gr-table-styles.ts
similarity index 94%
rename from polygerrit-ui/app/styles/gr-table-styles.js
rename to polygerrit-ui/app/styles/gr-table-styles.ts
index ceac675..52fdc67 100644
--- a/polygerrit-ui/app/styles/gr-table-styles.js
+++ b/polygerrit-ui/app/styles/gr-table-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-table-styles">
@@ -116,4 +122,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/gr-voting-styles.js b/polygerrit-ui/app/styles/gr-voting-styles.ts
similarity index 86%
rename from polygerrit-ui/app/styles/gr-voting-styles.js
rename to polygerrit-ui/app/styles/gr-voting-styles.ts
index 60bf623..d4e6d52 100644
--- a/polygerrit-ui/app/styles/gr-voting-styles.js
+++ b/polygerrit-ui/app/styles/gr-voting-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="gr-voting-styles">
@@ -40,4 +46,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/shared-styles.js b/polygerrit-ui/app/styles/shared-styles.ts
similarity index 95%
rename from polygerrit-ui/app/styles/shared-styles.js
rename to polygerrit-ui/app/styles/shared-styles.ts
index 3e81761..04dca9c 100644
--- a/polygerrit-ui/app/styles/shared-styles.js
+++ b/polygerrit-ui/app/styles/shared-styles.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `<dom-module id="shared-styles">
@@ -127,7 +133,7 @@
         --iron-icon-width: 20px;
       }
 
-      /* Stopgap solution until we remove hidden\$ attributes. */
+      /* Stopgap solution until we remove hidden$ attributes. */
 
       [hidden] {
         display: none !important;
@@ -196,4 +202,3 @@
   from HTML and may be out of place here. Review them and
   then delete this comment!
 */
-
diff --git a/polygerrit-ui/app/styles/themes/app-theme.js b/polygerrit-ui/app/styles/themes/app-theme.ts
similarity index 97%
rename from polygerrit-ui/app/styles/themes/app-theme.js
rename to polygerrit-ui/app/styles/themes/app-theme.ts
index 1a8296f..f48e43f 100644
--- a/polygerrit-ui/app/styles/themes/app-theme.js
+++ b/polygerrit-ui/app/styles/themes/app-theme.ts
@@ -14,6 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+// Mark the file as a module. Otherwise typescript assumes this is a script
+// and $_documentContainer is a global variable.
+// See: https://www.typescriptlang.org/docs/handbook/modules.html
+export {};
+
 const $_documentContainer = document.createElement('template');
 
 $_documentContainer.innerHTML = `
@@ -223,4 +229,4 @@
   }
 </style></custom-style>`;
 
-document.head.appendChild($_documentContainer.content);
\ No newline at end of file
+document.head.appendChild($_documentContainer.content);
diff --git a/polygerrit-ui/app/styles/themes/dark-theme.js b/polygerrit-ui/app/styles/themes/dark-theme.ts
similarity index 100%
rename from polygerrit-ui/app/styles/themes/dark-theme.js
rename to polygerrit-ui/app/styles/themes/dark-theme.ts
diff --git a/polygerrit-ui/server.go b/polygerrit-ui/server.go
index c556c37..b2eb3dc 100644
--- a/polygerrit-ui/server.go
+++ b/polygerrit-ui/server.go
@@ -167,8 +167,18 @@
 		// with the import error, so we can catch this problem easily.
 		writer.Header().Set("Content-Type", "text/html")
 	} else if isJsFile {
-		moduleImportRegexp := regexp.MustCompile("(?m)^(import.*)'([^/.].*)';$")
-		data = moduleImportRegexp.ReplaceAll(data, []byte("$1 '/node_modules/$2';"))
+	  // The following code updates import statements.
+	  // 1. Keep all imports started with '.' character unchanged (i.e. all relative
+	  // imports like import ... from './a.js' or import ... from '../b/c/d.js'
+	  // 2. For other imports it adds '/node_modules/' prefix. Additionally,
+	  //   if an in imported file has .js or .mjs extension, the code keeps
+	  //   the file extension unchanged. Otherwise, it adds .js extension.
+	  //   Examples:
+	  //   '@polymer/polymer.js' -> '/node_modules/@polymer/polymer.js'
+    //   'page/page.mjs' -> '/node_modules/page.mjs'
+    //   '@polymer/iron-icon' -> '/node_modules/@polymer/iron-icon.js'
+		moduleImportRegexp := regexp.MustCompile("(?m)^(import.*)'([^/.].*?)(\\.(m?)js)?';$")
+		data = moduleImportRegexp.ReplaceAll(data, []byte("$1 '/node_modules/$2.${4}js';"))
 		writer.Header().Set("Content-Type", "application/javascript")
 	} else if strings.HasSuffix(normalizedContentPath, ".css") {
 		writer.Header().Set("Content-Type", "text/css")