From bc18e85d5c9ded603937b0a2dc92d08973f81a80 Mon Sep 17 00:00:00 2001 From: Marwin Hochfelsner <50826859+hlfan@users.noreply.github.com> Date: Sun, 26 Jan 2025 22:47:11 +0100 Subject: [PATCH] Add eslint-plugin-erb for .js.erb file linting * 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 --- app/assets/javascripts/osm.js.erb | 101 +++++++++++++----------------- config/eslint.js | 11 ++++ lib/tasks/eslint.rake | 2 +- package.json | 1 + yarn.lock | 5 ++ 5 files changed, 60 insertions(+), 60 deletions(-) diff --git a/app/assets/javascripts/osm.js.erb b/app/assets/javascripts/osm.js.erb index 254e00810c..749cf5b773 100644 --- a/app/assets/javascripts/osm.js.erb +++ b/app/assets/javascripts/osm.js.erb @@ -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)) )); } }; diff --git a/config/eslint.js b/config/eslint.js index b2c527b751..b99179d27a 100644 --- a/config/eslint.js +++ b/config/eslint.js @@ -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", diff --git a/lib/tasks/eslint.rake b/lib/tasks/eslint.rake index 790ef150d6..27b550e40b 100644 --- a/lib/tasks/eslint.rake +++ b/lib/tasks/eslint.rake @@ -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 diff --git a/package.json b/package.json index 1b331ec963..371d01aff4 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/yarn.lock b/yarn.lock index fc144d7343..c15c97e29b 100644 --- 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"