Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 27d2381

Browse files
committed
fix: run eslint-fix (use const, optimize regex, etc)
1 parent ac226e5 commit 27d2381

File tree

4 files changed

+99
-53
lines changed

4 files changed

+99
-53
lines changed

lib/dist-fetch.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function checkDatedDistHasRls(date, channel = "nightly") {
2121
const dateString = _.isString(date) ? date : date.toISOString().split("T")[0]
2222
const cacheKey = `${channel}-${dateString}`
2323

24-
let fetch =
24+
const fetch =
2525
datedNightlyHasRlsCache.get(cacheKey) ||
2626
new Promise((resolve, reject) => {
2727
https
@@ -33,7 +33,9 @@ async function checkDatedDistHasRls(date, channel = "nightly") {
3333
let body = ""
3434
res.on("data", (data) => {
3535
body += data
36-
if (manifest_includes_rls(body)) resolve(cacheKey)
36+
if (manifest_includes_rls(body)) {
37+
resolve(cacheKey)
38+
}
3739
})
3840
res.on("end", () => reject(new Error("no 'rls-preview'")))
3941
})
@@ -60,11 +62,16 @@ async function fetchLatestDatedDistWithRls(channel) {
6062

6163
const check = (day) => {
6264
return checkDatedDistHasRls(day, channel).catch((e) => {
63-
if (e && e.code === "ENOTFOUND") throw e
65+
if (e && e.code === "ENOTFOUND") {
66+
throw e
67+
}
6468

6569
const yesterday = new Date(day - aDayMillis)
66-
if (yesterday >= minDate) return check(yesterday)
67-
else throw new Error("No nightly with 'rls-preview'")
70+
if (yesterday >= minDate) {
71+
return check(yesterday)
72+
} else {
73+
throw new Error("No nightly with 'rls-preview'")
74+
}
6875
})
6976
}
7077

@@ -89,9 +96,11 @@ async function fetchLatestDist({ toolchain, currentVersion = "none" }) {
8996
res.on("data", (data) => (body += data))
9097
res.on("end", () => {
9198
// use a subsection as toml is slow to parse fully
92-
let rustcInfo = body.match(/(\[pkg\.rustc\][^[]*)/m)
93-
if (!rustcInfo) return reject(new Error("could not split channel toml output"))
94-
let rustcVersion = require("toml").parse(rustcInfo[1]).pkg.rustc.version.trim()
99+
const rustcInfo = body.match(/(\[pkg\.rustc][^[]*)/m)
100+
if (!rustcInfo) {
101+
return reject(new Error("could not split channel toml output"))
102+
}
103+
const rustcVersion = require("toml").parse(rustcInfo[1]).pkg.rustc.version.trim()
95104
resolve(
96105
!currentVersion.trim().endsWith(rustcVersion) && manifest_includes_rls(body) && `rustc ${rustcVersion}`
97106
)
@@ -111,14 +120,14 @@ async function fetchLatestDist({ toolchain, currentVersion = "none" }) {
111120
* @returns {Promise<boolean>}
112121
*/
113122
async function checkHasRls(toolchain) {
114-
let dated = toolchain.match(DATED_REGEX)
123+
const dated = toolchain.match(DATED_REGEX)
115124
if (dated) {
116125
return checkDatedDistHasRls(dated[2], dated[1])
117126
.then(() => true)
118127
.catch(() => false)
119128
}
120129
return fetchLatestDist({ toolchain })
121-
.then((v) => !!v)
130+
.then((v) => Boolean(v))
122131
.catch(() => false)
123132
}
124133

@@ -127,10 +136,12 @@ async function checkHasRls(toolchain) {
127136
* @returns {Promise<string>} Latest channel dated version with rls-preview or the channel itself if ok
128137
*/
129138
async function suggestChannelOrDated(channel) {
130-
let latestDatedPromise = fetchLatestDatedDistWithRls(channel)
139+
const latestDatedPromise = fetchLatestDatedDistWithRls(channel)
131140
try {
132-
let latestIsOk = await fetchLatestDist({ toolchain: channel })
133-
if (latestIsOk) return channel
141+
const latestIsOk = await fetchLatestDist({ toolchain: channel })
142+
if (latestIsOk) {
143+
return channel
144+
}
134145
} catch (e) {
135146
console.warn(e)
136147
}

lib/index.js

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function exec(command, { cwd } = {}) {
2626
}
2727

2828
function logErr(e, logFn = console.warn) {
29-
e && logFn("" + e)
29+
e && logFn(`${e}`)
3030
}
3131

3232
function clearIdeRustNotifications(prefix = "ide-rust") {
@@ -78,7 +78,7 @@ function rustupRun(toolchain, command, opts = {}) {
7878

7979
function notifyLanguageServerCommandFailed(languageServerCmd) {
8080
clearIdeRustNotifications("ide-rust.langServerCommand")
81-
let description =
81+
const description =
8282
"Make sure the **rust-analyzer** binary is installed and in `$PATH`." +
8383
"\n\nSee https://rust-analyzer.github.io/manual.html#rust-analyzer-language-server-binary."
8484

@@ -92,7 +92,7 @@ function notifyLanguageServerCommandFailed(languageServerCmd) {
9292
async function rustupDefaultToolchain() {
9393
// linux: "stable-x86_64-unknown-linux-gnu (default)"
9494
// mac: "stable (default)"
95-
let { stdout } = await exec("rustup default")
95+
const { stdout } = await exec("rustup default")
9696
return stdout.split("-")[0].trim().split(" ")[0]
9797
}
9898

@@ -107,12 +107,12 @@ async function hasCommand(rustCommand) {
107107

108108
/** @returns {{ path: string; toolchain: String }[]} */
109109
async function rustupOverrides() {
110-
let { stdout } = await exec("rustup override list")
110+
const { stdout } = await exec("rustup override list")
111111
return stdout
112-
.split(/[\r\n]+/g)
112+
.split(/[\n\r]+/g)
113113
.map((line) => {
114114
// line.trimEnd is not a function ?
115-
let lastSpace = line.trimEnd().lastIndexOf(" ")
115+
const lastSpace = line.trimEnd().lastIndexOf(" ")
116116
return {
117117
path: line.slice(0, lastSpace).trim(),
118118
toolchain: line.slice(lastSpace).trim(),
@@ -150,7 +150,9 @@ function shouldIgnoreProjectPath(projectPath) {
150150
let envPath = () => {
151151
// Make sure the cargo directory is in PATH
152152
let envPath = process.env.PATH || ""
153-
if (!envPath.includes(".cargo/bin")) envPath += `:${path.join(os.homedir(), ".cargo/bin")}`
153+
if (!envPath.includes(".cargo/bin")) {
154+
envPath += `:${path.join(os.homedir(), ".cargo/bin")}`
155+
}
154156
return envPath
155157
}
156158

@@ -236,23 +238,31 @@ class RustLanguageClient extends AutoLanguageClient {
236238
this.activeOverrides = new Set(overrides)
237239
if (this.activeOverrides.size > oldActive.size) {
238240
const confToolchain = configToolchain() || (await rustupDefaultToolchain())
239-
if (confToolchain) oldActive.add(confToolchain)
241+
if (confToolchain) {
242+
oldActive.add(confToolchain)
243+
}
240244
this._promptToUpdateToolchain({ ignore: oldActive })
241245
}
242246
} catch (e) {
243-
if (await hasCommand("rustup")) logErr(e)
247+
if (await hasCommand("rustup")) {
248+
logErr(e)
249+
}
244250
}
245251
}
246252

247253
/** @param {string | null} reason Reason for the restart shown in the notification */
248254
async _restartLanguageServers(reason) {
249255
await this.restartAllServers()
250-
if (reason) atom.notifications.addSuccess(reason, { _src: "ide-rust" })
256+
if (reason) {
257+
atom.notifications.addSuccess(reason, { _src: "ide-rust" })
258+
}
251259
}
252260

253261
// check for toolchain updates if installed & not dated
254262
async _promptToUpdateToolchain({ ignore } = {}) {
255-
if (!atom.config.get("ide-rust.checkForToolchainUpdates")) return
263+
if (!atom.config.get("ide-rust.checkForToolchainUpdates")) {
264+
return
265+
}
256266

257267
if (!(await hasCommand("rustup"))) {
258268
atom.config.set("ide-rust.checkForToolchainUpdates", false)
@@ -263,7 +273,9 @@ class RustLanguageClient extends AutoLanguageClient {
263273
const toolchains = new Set(this.activeOverrides)
264274

265275
const confToolchain = configToolchain() || (await rustupDefaultToolchain())
266-
if (confToolchain) toolchains.add(confToolchain)
276+
if (confToolchain) {
277+
toolchains.add(confToolchain)
278+
}
267279

268280
Array.from(toolchains)
269281
.filter((toolchain) => !ignore || !ignore.has(toolchain))
@@ -274,9 +286,11 @@ class RustLanguageClient extends AutoLanguageClient {
274286
toolchain = toolchain.split("-")[0]
275287
}
276288

277-
let { stdout: currentVersion } = await rustupRun(confToolchain, "rustc --version")
278-
let newVersion = await fetchLatestDist({ toolchain, currentVersion }).catch(() => false)
279-
if (!newVersion) return
289+
const { stdout: currentVersion } = await rustupRun(confToolchain, "rustc --version")
290+
const newVersion = await fetchLatestDist({ toolchain, currentVersion }).catch(() => false)
291+
if (!newVersion) {
292+
return
293+
}
280294

281295
atom.notifications.addInfo(`Rust \`${toolchain}\` toolchain update available`, {
282296
description: newVersion,
@@ -339,7 +353,7 @@ class RustLanguageClient extends AutoLanguageClient {
339353
if (!(await exec("rustup --version").catch(() => false))) {
340354
this._handleMissingRustup()
341355
} else {
342-
let clicked = await atomPrompt(
356+
const clicked = await atomPrompt(
343357
`\`rustup\` missing ${toolchain} toolchain`,
344358
{
345359
detail: `rustup toolchain install ${toolchain}`,
@@ -355,7 +369,7 @@ class RustLanguageClient extends AutoLanguageClient {
355369
.catch((e) => {
356370
console.warn(e)
357371
clearIdeRustNotifications()
358-
let err = (e + "").split("\n")
372+
let err = `${e}`.split("\n")
359373
err = (err.length && err[0]) || `Error installing rust \`${toolchain}\``
360374
atom.notifications.addError(err, {
361375
detail: "Check the toolchain is valid & connection is available",
@@ -374,10 +388,11 @@ class RustLanguageClient extends AutoLanguageClient {
374388
async _handleMissingRustup() {
375389
try {
376390
let description = "Installs from https://www.rustup.rs"
377-
if (process.platform === "linux")
391+
if (process.platform === "linux") {
378392
description += ", alternatively install rustup with _`apt install rustup`_ or similar and restart."
393+
}
379394

380-
let clicked = await atomPrompt(
395+
const clicked = await atomPrompt(
381396
"`rustup` is not available",
382397
{
383398
description,
@@ -388,7 +403,7 @@ class RustLanguageClient extends AutoLanguageClient {
388403

389404
if (clicked === "Install") {
390405
// Install rustup and try again
391-
let installRustupPromise = installRustup()
406+
const installRustupPromise = installRustup()
392407
if (this.busySignalService) {
393408
this.busySignalService.reportBusyWhile(`Installing rustup`, () => installRustupPromise)
394409
}
@@ -424,7 +439,9 @@ class RustLanguageClient extends AutoLanguageClient {
424439
// watch config toolchain updates -> check for updates if enabling
425440
this.disposables.add(
426441
atom.config.onDidChange("ide-rust.checkForToolchainUpdates", ({ newValue: enabled }) => {
427-
if (enabled) this._promptToUpdateToolchain().catch(logErr)
442+
if (enabled) {
443+
this._promptToUpdateToolchain().catch(logErr)
444+
}
428445
})
429446
)
430447

@@ -455,7 +472,7 @@ class RustLanguageClient extends AutoLanguageClient {
455472

456473
postInitialization(server) {
457474
// track the server so we can keep its config updated
458-
let project = new RustProject(server, () => this.busySignalService)
475+
const project = new RustProject(server, () => this.busySignalService)
459476
this.projects[server.projectPath] = project
460477

461478
server.process.on("exit", () => {
@@ -545,11 +562,11 @@ class RustLanguageClient extends AutoLanguageClient {
545562
// only used for local variables by the language server. This makes the
546563
// outline view cleaner and more useful.
547564
provideOutlines() {
548-
let provide = super.provideOutlines()
549-
let superOutline = provide.getOutline
565+
const provide = super.provideOutlines()
566+
const superOutline = provide.getOutline
550567

551568
provide.getOutline = async (...args) => {
552-
let outline = await superOutline.apply(this, args)
569+
const outline = await superOutline.apply(this, args)
553570
outline.outlineTrees = outline.outlineTrees.filter((o) => o.icon !== "type-variable")
554571
return outline
555572
}
@@ -584,7 +601,7 @@ class RustLanguageClient extends AutoLanguageClient {
584601
* @returns {Promise<atomIde.Datatip | null>}
585602
*/
586603
async getDatatip(editor, ...args) {
587-
let datatip = await super.getDatatip(editor, ...args)
604+
const datatip = await super.getDatatip(editor, ...args)
588605
try {
589606
if (datatip) {
590607
datatip.markedStrings = datatip.markedStrings
@@ -613,7 +630,7 @@ class RustLanguageClient extends AutoLanguageClient {
613630
*/
614631
function convertMarkdownToSnippets(value) {
615632
// even indices are text, odds are rust snippets
616-
return value.split(/\s*```rust\s*((?:.|\s)+?)\s*```/).map((bit, index) => {
633+
return value.split(/\s*```rust\s*([\s.]+?)\s*```/).map((bit, index) => {
617634
if (index % 2 == 0) {
618635
return {
619636
type: "markdown",
@@ -647,7 +664,9 @@ if (process.platform === "win32") {
647664
envPath = () => {
648665
// Make sure the cargo directory is in PATH
649666
let envPath = process.env.PATH || ""
650-
if (!envPath.includes(".cargo\\bin")) envPath += `;${path.join(os.homedir(), ".cargo", "bin")}`
667+
if (!envPath.includes(".cargo\\bin")) {
668+
envPath += `;${path.join(os.homedir(), ".cargo", "bin")}`
669+
}
651670
return envPath
652671
}
653672

0 commit comments

Comments
 (0)