eslint-plugin-tidesquare
v1.1.4
Published
## Rules
Downloads
15
Readme
eslint-plugin-tidesquare
Rules
1. no-direct-class-method-passing
This rule is to prevent losing this
context while passing class method directly into functions parameter like below.
class Clock {
constructor() {
this.currentTime = '12:00 PM'
}
showTime() {
console.log(this.currentTime)
}
}
class Timer {
constructor() {}
afterDelay(callback, duration) {
setTimeout(callback, duration)
}
}
const myClock = new Clock()
const myTimer = new Timer()
// Passing the showTime method directly as a callback.
myTimer.afterDelay(myClock.showTime, 1000) // After 1 second, this logs: `undefined`.
2. no-direct-class-method-referencing
This rule is to prevent losing this
context while referencing class method directly like below.
class CoffeeMachine {
makeEspresso() {
console.log('Making Espresso in:', 2 - this.discountTime, 'minutes.')
}
makeLatte() {
console.log('Making Latte in:', 5 - this.discountTime, 'minutes.')
}
}
class Barista {
discountTime = 1 // Let's say the barista has a trick to make coffee 1 minute faster
constructor(private readonly machine: CoffeeMachine) {}
private readonly coffeeMakingTimeMap = {
Espresso: this.machine.makeEspresso,
Latte: this.machine.makeLatte,
}
orderCoffee(type: string) {
this.coffeeMakingTimeMap[type]()
}
}
const machine = new CoffeeMachine()
const john = new Barista(machine)
john.orderCoffee('Espresso') // Logs: "Making Espresso in: NaN minutes."