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 <git@gravitystorm.co.uk>
This commit is contained in:
Marwin Hochfelsner 2025-01-26 22:47:11 +01:00 committed by Marwin Hochfelsner
parent 1dff2d5d5c
commit bc18e85d5c
5 changed files with 55 additions and 55 deletions

View file

@ -4,45 +4,44 @@
//= depend_on key.yml
OSM = {
<% if defined?(Settings.matomo) %>
MATOMO: <%= Settings.matomo.to_json %>,
<% end %>
...<%=
%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
%>,
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 %>,
DEFAULT_LOCALE: <%= I18n.default_locale.to_json %>,
<% if Settings.key?(:thunderforest_key) %>
THUNDERFOREST_KEY: <%= Settings.thunderforest_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 %>,
<% if Settings.key?(:tracestrack_key) %>
TRACESTRACK_KEY: <%= Settings.tracestrack_key.to_json %>,
<% end %>
MARKER_GREEN: <%= image_path("marker-green.png").to_json %>,
MARKER_RED: <%= image_path("marker-red.png").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_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 %>,
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 %>,
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))
));
}
};

View 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",

View 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

View 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"
}

View file

@ -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"