]> git.openstreetmap.org Git - rails.git/commitdiff
Add eslint-plugin-erb for .js.erb file linting
authorMarwin Hochfelsner <50826859+hlfan@users.noreply.github.com>
Sun, 26 Jan 2025 21:47:11 +0000 (22:47 +0100)
committerMarwin Hochfelsner <e12123674@student.tuwien.ac.at>
Wed, 5 Feb 2025 16:00:38 +0000 (17:00 +0100)
* Add eslint-plugin-erb to provide linting of our .js.erb files
* Lint osm.js.erb and API-ify OSM.params()
---------
Co-authored-by: Andy Allan <git@gravitystorm.co.uk>
app/assets/javascripts/osm.js.erb
config/eslint.js
lib/tasks/eslint.rake
package.json
yarn.lock

index 254e00810cd6201e459388dd5754b9df9c0f57f6..749cf5b7736767b6e52a59e70c02babac07d9022 100644 (file)
@@ -4,45 +4,44 @@
 //= depend_on key.yml
 
 OSM = {
-<% if defined?(Settings.matomo) %>
-  MATOMO:                  <%= Settings.matomo.to_json %>,
-<% end %>
-
-  MAX_REQUEST_AREA:        <%= Settings.max_request_area.to_json %>,
-  SERVER_PROTOCOL:         <%= Settings.server_protocol.to_json %>,
-  SERVER_URL:              <%= Settings.server_url.to_json %>,
-  API_VERSION:             <%= Settings.api_version.to_json %>,
-  STATUS:                  <%= Settings.status.to_json %>,
-  MAX_NOTE_REQUEST_AREA:   <%= Settings.max_note_request_area.to_json %>,
-  OVERPASS_URL:            <%= Settings.overpass_url.to_json %>,
-  OVERPASS_CREDENTIALS:    <%= Settings.overpass_credentials.to_json %>,
-  NOMINATIM_URL:           <%= Settings.nominatim_url.to_json %>,
-  GRAPHHOPPER_URL:         <%= Settings.graphhopper_url.to_json %>,
-  FOSSGIS_OSRM_URL:        <%= Settings.fossgis_osrm_url.to_json %>,
-  FOSSGIS_VALHALLA_URL:    <%= Settings.fossgis_valhalla_url.to_json %>,
-  DEFAULT_LOCALE:          <%= I18n.default_locale.to_json %>,
-
-<% if Settings.key?(:thunderforest_key) %>
-  THUNDERFOREST_KEY:       <%= Settings.thunderforest_key.to_json %>,
-<% end %>
-
-<% if Settings.key?(:tracestrack_key) %>
-  TRACESTRACK_KEY:         <%= Settings.tracestrack_key.to_json %>,
-<% end %>
-
-  LAYER_DEFINITIONS:       <%= YAML.load_file(Rails.root.join("config/layers.yml")).to_json %>,
-  LAYERS_WITH_MAP_KEY:     <%= YAML.load_file(Rails.root.join("config/key.yml")).keys.to_json %>,
-
-  MARKER_GREEN:            <%= image_path("marker-green.png").to_json %>,
-  MARKER_RED:              <%= image_path("marker-red.png").to_json %>,
-
-  MARKER_ICON:             <%= image_path("leaflet/dist/images/marker-icon.png").to_json %>,
-  MARKER_ICON_2X:          <%= image_path("leaflet/dist/images/marker-icon-2x.png").to_json %>,
-  MARKER_SHADOW:           <%= image_path("leaflet/dist/images/marker-shadow.png").to_json %>,
-
-  NEW_NOTE_MARKER:         <%= image_path("new_note_marker.svg").to_json %>,
-  OPEN_NOTE_MARKER:        <%= image_path("open_note_marker.svg").to_json %>,
-  CLOSED_NOTE_MARKER:      <%= image_path("closed_note_marker.svg").to_json %>,
+  ...<%=
+    %i[
+      matomo
+      max_request_area
+      server_protocol
+      server_url
+      api_version
+      status
+      max_note_request_area
+      overpass_url
+      overpass_credentials
+      nominatim_url
+      graphhopper_url
+      fossgis_osrm_url
+      fossgis_valhalla_url
+      thunderforest_key
+      tracestrack_key
+    ]
+      .each_with_object({}) do |key, hash|
+        hash[key.to_s.upcase] = Settings.send(key) if Settings.respond_to?(key)
+      end.to_json
+  %>,
+
+  DEFAULT_LOCALE: <%= I18n.default_locale.to_json %>,
+
+  LAYER_DEFINITIONS: <%= YAML.load_file(Rails.root.join("config/layers.yml")).to_json %>,
+  LAYERS_WITH_MAP_KEY: <%= YAML.load_file(Rails.root.join("config/key.yml")).keys.to_json %>,
+
+  MARKER_GREEN: <%= image_path("marker-green.png").to_json %>,
+  MARKER_RED: <%= image_path("marker-red.png").to_json %>,
+
+  MARKER_ICON: <%= image_path("leaflet/dist/images/marker-icon.png").to_json %>,
+  MARKER_ICON_2X: <%= image_path("leaflet/dist/images/marker-icon-2x.png").to_json %>,
+  MARKER_SHADOW: <%= image_path("leaflet/dist/images/marker-shadow.png").to_json %>,
+
+  NEW_NOTE_MARKER: <%= image_path("new_note_marker.svg").to_json %>,
+  OPEN_NOTE_MARKER: <%= image_path("open_note_marker.svg").to_json %>,
+  CLOSED_NOTE_MARKER: <%= image_path("closed_note_marker.svg").to_json %>,
 
   apiUrl: function (object) {
     var apiType = object.type === "note" ? "notes" : object.type;
@@ -58,28 +57,12 @@ OSM = {
   },
 
   params: function (search) {
-    var params = {};
-
-    search = (search || window.location.search).replace("?", "").split(/&|;/);
-
-    for (var i = 0; i < search.length; ++i) {
-      var pair = search[i],
-          j = pair.indexOf("="),
-          key = pair.slice(0, j),
-          val = pair.slice(++j);
-
-      try {
-        params[key] = decodeURIComponent(val);
-      } catch (e) {
-        // Ignore parse exceptions
-      }
-    }
-
-    return params;
+    var query = search || window.location.search;
+    return Object.fromEntries(new URLSearchParams(query));
   },
 
   mapParams: function (search) {
-    var params = OSM.params(search), mapParams = {}, match;
+    var params = OSM.params(search), mapParams = {};
 
     if (params.mlon && params.mlat) {
       mapParams.marker = true;
@@ -229,7 +212,7 @@ OSM = {
     return 6372795 * 2 * Math.asin(
       Math.sqrt(
         Math.pow(Math.sin(latdiff / 2), 2) +
-        Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(lngdiff / 2), 2)
+        (Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(lngdiff / 2), 2))
       ));
   }
 };
index b2c527b7511fffbdb0086274b63ef927c14ec600..b99179d27a4b200e7c9337e171cf33314d781551 100644 (file)
@@ -1,9 +1,11 @@
 const globals = require("globals");
 const js = require("@eslint/js");
+const erb = require("eslint-plugin-erb");
 const stylisticJs = require("@stylistic/eslint-plugin-js");
 
 module.exports = [
   js.configs.recommended,
+  erb.configs.recommended,
   {
     plugins: {
       "@stylistic": stylisticJs
@@ -24,6 +26,15 @@ module.exports = [
         updateLinks: "readonly"
       }
     },
+    linterOptions: {
+      // The "unused disable directive" is set to "warn" by default.
+      // For the ERB plugin to work correctly, you must disable
+      // this directive to avoid issues described here
+      // https://github.com/eslint/eslint/discussions/18114
+      // If you're using the CLI, you might also use the following flag:
+      // --report-unused-disable-directives-severity=off
+      reportUnusedDisableDirectives: "off"
+    },
     rules: {
       "@stylistic/array-bracket-newline": ["error", "consistent"],
       "@stylistic/array-bracket-spacing": "error",
index 790ef150d6e7b3220921348ee373857eb86356de..27b550e40bd1f00ff0be62f75c43d5c67f4d08a2 100644 (file)
@@ -10,7 +10,7 @@ end
 
 def js_files
   Rails.application.assets.each_file.select do |file|
-    file.ends_with?(".js") && !file.match?(%r{/(gems|vendor|i18n|node_modules)/})
+    (file.ends_with?(".js") || file.ends_with?(".js.erb")) && !file.match?(%r{/(gems|vendor|i18n|node_modules)/})
   end
 end
 
index 1b331ec96390b42142baeb0e53ca102a3fc0579b..371d01aff40a1c24a000a65e0e906f498df93fcb 100644 (file)
@@ -10,6 +10,7 @@
   },
   "devDependencies": {
     "eslint": "^9.0.0",
+    "eslint-plugin-erb": "^2.1.0",
     "@stylistic/eslint-plugin-js": "^3.0.0",
     "eslint-formatter-compact": "^8.40.0"
   }
index fc144d73436325ec27365ee840a60bc83388edea..c15c97e29b3c446b7835d4e34d130e056b94bc7b 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -225,6 +225,11 @@ eslint-formatter-compact@^8.40.0:
   resolved "https://registry.yarnpkg.com/eslint-formatter-compact/-/eslint-formatter-compact-8.40.0.tgz#d7455b2d75fd70e8c0e7a98a5e189f168e9dfe2d"
   integrity sha512-cwGUs113TgmTQXecx5kfRjB7m0y2wkDLSadPTE2pK6M/wO4N8PjmUaoWOFNCP9MHgsiZwgqd5bZFnDCnszC56Q==
 
+eslint-plugin-erb@^2.1.0:
+  version "2.1.1"
+  resolved "https://registry.yarnpkg.com/eslint-plugin-erb/-/eslint-plugin-erb-2.1.1.tgz#8a0a6c2bcaf3a8573c381b595969145aff93cfc6"
+  integrity sha512-AhznaVwRpQqR8NADjN4SZnKNbaIdAbGxTjCg6cj3UhwGyQOUJ6kXwhYrl1LYrGDNx7Ouyd8xuEG7wepFZyPgFw==
+
 eslint-scope@^8.2.0:
   version "8.2.0"
   resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-8.2.0.tgz#377aa6f1cb5dc7592cfd0b7f892fd0cf352ce442"