Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply linting rules. #238

Merged
merged 5 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
end_of_line = lf
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
!.*
assets/js/prism.js
/dist/
docs/
node_modules/
13 changes: 8 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
"brace-style": [1, "stroustrup"],
"no-mixed-spaces-and-tabs": [1, "smart-tabs"],
"quotes": [1, "double", "avoid-escape"],
"spaced-comment": [1, "always", {
"block": {
"exceptions": ["*"]
}
"spaced-comment": [1, "always", {
"block": {
"exceptions": ["*"]
}
}],
"arrow-spacing": 1,
"comma-spacing": 1,
"keyword-spacing": 1
"keyword-spacing": 1,
"eol-last": 1,
"no-trailing-spaces": 1,
"space-before-function-paren": 1
}
}
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ All of the above also have `build` versions (e.g. `npm run build:js` or `npm run

Please install an ESLint plugin for your editor. There is an `.eslintrc.json` file in the repo which encodes most of the coding style of the project.

To lint and format the project, run `npm run lint`.

Here are a few other guidelines that cannot be enforced via ESLint:

- When you define a function, use a space between the opening paren and its name. That way we can search for "functionName (" and find its definition immediately, instead of having to wade through calls to the function.
- Prefer single-word names over multi-word names. 3+ word names are especially frowned upon.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LeaVerou I removed this note because I added an ESLint rule to enforce this convention -- so I thought the note is no longer applicable. If you prefer, I'm happy to add it back.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need :) Thanks for the explanation!

- camelCase over underscore_case, with few exceptions.
- Don't be afraid of unicode characters when appropriate, except on user-facing names. E.g. use ε over epsilon internally, but not ΔΕ over deltaE in the public-facing method name.
8 changes: 4 additions & 4 deletions apps/blackwhite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ for (let algo in contrastAlgorithms) {
const root = document.documentElement;
let previousAlgo;

function computeTextColor() {
function computeTextColor () {
let algo = contrast_algorithm.value;

previous_algo.textContent = previousAlgo;
Expand All @@ -29,7 +29,7 @@ function computeTextColor() {
previousAlgo = algo;
}

function drawColors() {
function drawColors () {
colors.innerHTML = "";
let granularity = Math.cbrt(number_of_colors.value);
// root.style.setProperty("--granularity", granularity);
Expand All @@ -56,7 +56,7 @@ function drawColors() {
computeTextColor();
}

function render(evt) {
function render (evt) {
if (!evt || evt.target === number_of_colors) {
number_of_colors.title = number_of_colors.value;
drawColors();
Expand All @@ -77,4 +77,4 @@ function render(evt) {

document.body.addEventListener("input", render);

render();
render();
12 changes: 6 additions & 6 deletions apps/convert/convert.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const favicon = document.querySelector('link[rel="shortcut icon"]');
const supportsP3 = window.CSS && CSS.supports("color", "color(display-p3 0 1 0)");

function getURLParams() {
function getURLParams () {
return Object.fromEntries(new URL(location).searchParams);
}

function update() {
function update () {
try {
var color = new Color(colorInput.value);
colorInput.setCustomValidity("");
Expand Down Expand Up @@ -52,7 +52,7 @@ function update() {

if (id === "srgb" || (id === "p3") && supportsP3) {
colorOutput.style.background = converted;
favicon.href = `data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"><rect width="100%" fill="${converted}" /></svg>`
favicon.href = `data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"><rect width="100%" fill="${converted}" /></svg>`;
}

let precision = precisionInput.value;
Expand Down Expand Up @@ -88,7 +88,7 @@ let urlParams = getURLParams();
colorInput.addEventListener("input", update);
precisionInput.addEventListener("input", update);

function updateFromURL() {
function updateFromURL () {
colorInput.value = urlParams.color || colorInput.value;
precisionInput.value = urlParams.precision || precisionInput.value;
update();
Expand All @@ -111,8 +111,8 @@ document.body.addEventListener("click", async evt => {
await wait(1000);
copyButton.textContent = "📋";
}
catch(e) {
catch (e) {
alert("Failed to copy to clipboard");
}
}
})
});
4 changes: 2 additions & 2 deletions apps/named/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ for (let method in Color.prototype) {
}
}

function render() {
function render () {
let color;

try {
Expand Down Expand Up @@ -59,4 +59,4 @@ function render() {
}

deltaE_method.oninput = input_color.oninput = render;
render();
render();
6 changes: 3 additions & 3 deletions apps/palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ let increments = [
-90, +90,
-45, +45, -135, +135,
-22.5, +22.5, -67.5, +67.5, -112.5, +112.5, -157.5, +157.5, -202.5, +202.5, -247.5, +247.5, -292.5, +292.5, -337.5, +337.5,
]
];

function render() {
function render () {
let color = new Color(accent_color.value).to("oklch");
let n = number_of_colors.value - 1;

Expand All @@ -32,4 +32,4 @@ function render() {
accent_color.addEventListener("input", render);
number_of_colors.addEventListener("input", render);

render();
render();
14 changes: 7 additions & 7 deletions apps/picker-mavo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ window.color_inGamut = function (color, o) {
return inGamut(color, o);
};

function has_color_resolved(color) {
function has_color_resolved (color) {
return color && has_spaceId_resolved(color.spaceId);
}

function has_spaceId_resolved(spaceId) {
function has_spaceId_resolved (spaceId) {
return spaceId && (spaceId + "").trim() && spaceId != "[id]";
}

Expand Down Expand Up @@ -74,9 +74,9 @@ window.getColorSpace = function (spaceId) {
spaceId = Mavo.clone(spaceId);

return ColorSpace.get(spaceId);
}
};

window.getSliderSteps = function(spaceId, coords, coord_meta, alpha) {
window.getSliderSteps = function (spaceId, coords, coord_meta, alpha) {
if (!has_spaceId_resolved(spaceId)) {
return;
}
Expand Down Expand Up @@ -119,7 +119,7 @@ window.getSliderSteps = function(spaceId, coords, coord_meta, alpha) {
ret.push(colorSteps);

return ret;
}
};

let handle;
document.body.addEventListener("mv-change", evt => {
Expand All @@ -136,7 +136,7 @@ document.body.addEventListener("mv-change", evt => {
}
});

window.CSS_color_to_LCH = function CSS_color_to_LCH(str) {
window.CSS_color_to_LCH = function CSS_color_to_LCH (str) {
str = str || prompt("Enter any CSS color");

if (!str) {
Expand All @@ -160,7 +160,7 @@ window.CSS_color_to_LCH = function CSS_color_to_LCH(str) {
alert(e.message);
return;
}
}
};

// Select text in readonly input fields when you focus them
document.addEventListener("click", evt => {
Expand Down
10 changes: 5 additions & 5 deletions apps/picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (!globalThis.requestIdleCallback) {
}

let app = createApp({
data() {
data () {
let ret = {
alpha: 100,
precision: 3,
Expand Down Expand Up @@ -65,7 +65,7 @@ let app = createApp({
return this.color.display({precision: this.precision}) + "";
},
color_srgb () {
return this.color.to('srgb');
return this.color.to("srgb");
},
serialized_color () {
return this.color.toString({precision: this.precision});
Expand Down Expand Up @@ -133,9 +133,9 @@ let app = createApp({
});
}
}
}).mount('#app')
}).mount("#app");

window.CSS_color_to_LCH = function CSS_color_to_LCH(str) {
window.CSS_color_to_LCH = function CSS_color_to_LCH (str) {
str = str || prompt("Enter any CSS color");

if (!str) {
Expand All @@ -152,7 +152,7 @@ window.CSS_color_to_LCH = function CSS_color_to_LCH(str) {
alert(e.message);
return;
}
}
};

// Select text in readonly input fields when you focus them
document.addEventListener("click", evt => {
Expand Down
8 changes: 4 additions & 4 deletions assets/js/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if (current) {
}
}

function idify(str) {
function idify (str) {
// from Mavo.Functions.idify()
return str
.normalize("NFD").replace(/[\u0300-\u036f]/g, "") // Convert accented letters to ASCII
Expand All @@ -49,7 +49,7 @@ function idify(str) {
.toLowerCase();
}

function makePageToc(pageToc) {
function makePageToc (pageToc) {
pageToc.textContent = "";

// Make toc for current page
Expand Down Expand Up @@ -120,7 +120,7 @@ if (location.pathname.indexOf("/spaces") > -1) {
data: {space: spaces}
});

Mavo.hooks.add("getdata-end", function(env) {
Mavo.hooks.add("getdata-end", function (env) {
if (this.id !== "colorSpaces") {
return;
}
Expand Down Expand Up @@ -149,4 +149,4 @@ if (location.pathname.indexOf("/spaces") > -1) {
// Mavo.all.colorSpaces.render(Mavo.all.colorSpaces.getData());

// }
}
}
2 changes: 1 addition & 1 deletion assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ for (let code of $$(":not(pre) > code")) {
around: code
});
}
}
}
2 changes: 1 addition & 1 deletion color.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Import all modules of Color.js and assign Color to a global,
// for easier testing and experimentation without building
import Color from "./src/index.js";
import * as util from "./src/util.js"
import * as util from "./src/util.js";

window.Color = window.Color || Color;

Expand Down
26 changes: 13 additions & 13 deletions elements/color-swatch/color-swatch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Color from "../../src/index.js";

export default class ColorSwatch extends HTMLElement {
#swatch
#swatch;

constructor() {
constructor () {
super();
this.attachShadow({mode: "open"});
this.shadowRoot.innerHTML = `<style>@import url("./color-swatch.css");</style>
Expand All @@ -12,26 +12,26 @@ export default class ColorSwatch extends HTMLElement {
this.#swatch = this.shadowRoot.querySelector("#swatch");
}

connectedCallback() {
connectedCallback () {
this.#render();
ColorSwatch.#mo.observe(this, {childList: true, subtree: true, characterData: true});
}

#value
get value() {
#value;
get value () {
return this.#value;
}
set value(value) {
set value (value) {
this.#value = value;
this.#render();
}

#color
get color() {
#color;
get color () {
return this.#color;
}

#render() {
#render () {
let colorText = this.value || this.textContent;

try {
Expand All @@ -45,11 +45,11 @@ export default class ColorSwatch extends HTMLElement {
}
}

static get observedAttributes() {
static get observedAttributes () {
return ["value"];
}

attributeChangedCallback(name, newValue) {
attributeChangedCallback (name, newValue) {
if (name === "value") {
this.value = newValue;
}
Expand All @@ -67,8 +67,8 @@ export default class ColorSwatch extends HTMLElement {
target.#render();
}
}
})
});
}


customElements.define('color-swatch', ColorSwatch);
customElements.define("color-swatch", ColorSwatch);
2 changes: 1 addition & 1 deletion get/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $("a[download]")?.addEventListener("click", evt => {
evt.target.href = createURL(code.textContent);
});

function createURL(code, type = "text/javascript") {
function createURL (code, type = "text/javascript") {
var blob = new Blob([code], {type});

return URL.createObjectURL(blob);
Expand Down
Loading