Skip to content

Commit

Permalink
Add eslint for linting (denoland#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored and ry committed Mar 5, 2019
1 parent 385f866 commit c0390ad
Show file tree
Hide file tree
Showing 41 changed files with 689 additions and 604 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/flags/
/fs/
/http/
/ws/
23 changes: 23 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint"],
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
],
"rules": {
"@typescript-eslint/array-type": ["error", "array-simple"],
"@typescript-eslint/explicit-member-accessibility": ["off"],
"@typescript-eslint/no-non-null-assertion": ["off"],
"@typescript-eslint/no-parameter-properties": ["off"],
"@typescript-eslint/no-unused-vars": [
"error",
{ "argsIgnorePattern": "^_" }
]
}
}
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.DS_Store
.idea
tsconfig.json
deno.d.ts
deno.d.ts
node_modules
package.json
package-lock.json
60 changes: 34 additions & 26 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
variables:
DENO_VERSION: 'v0.3.1'
DENO_VERSION: "v0.3.1"
TS_VERSION: "3.2.1"

# TODO DRY up the jobs
# TODO Try to get eslint to run under Deno, like prettier
jobs:
- job: "Linux"
pool:
vmImage: "Ubuntu-16.04"
steps:
- script: npm install eslint typescript@$(TS_VERSION) @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier
- script: curl -L https://deno.land/x/install/install.sh | sh -s $(DENO_VERSION)
- script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/'
- script: npx eslint **/*.ts
- script: deno format.ts --allow-run --allow-write --allow-read --check
- script: deno test.ts --allow-run --allow-net --allow-write --allow-read

- job: 'Linux'
pool:
vmImage: 'Ubuntu-16.04'
steps:
- script: curl -L https://deno.land/x/install/install.sh | sh -s $(DENO_VERSION)
- script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/'
- script: deno test.ts --allow-run --allow-net --allow-write --allow-read
- script: deno format.ts --allow-run --allow-write --allow-read --check
- job: "Mac"
pool:
vmImage: "macOS-10.13"
steps:
- script: npm -g install eslint typescript@$(TS_VERSION) @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier
- script: curl -L https://deno.land/x/install/install.sh | sh -s $(DENO_VERSION)
- script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/'
- script: eslint **/*.ts
- script: deno format.ts --allow-run --allow-write --allow-read --check
- script: deno test.ts --allow-run --allow-net --allow-write --allow-read

- job: 'Mac'
pool:
vmImage: 'macOS-10.13'
steps:
- script: curl -L https://deno.land/x/install/install.sh | sh -s $(DENO_VERSION)
- script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/'
- script: deno test.ts --allow-run --allow-net --allow-write --allow-read
- script: deno format.ts --allow-run --allow-write --allow-read --check

- job: 'Windows'
pool:
vmImage: 'vs2017-win2016'
steps:
- powershell: iwr https://deno.land/x/install/install.ps1 -out install.ps1; .\install.ps1 $(DENO_VERSION)
- bash: echo "##vso[task.prependpath]C:\Users\VssAdministrator\.deno\\bin"
- bash: deno.exe test.ts --allow-run --allow-net --allow-write --allow-read
- bash: deno.exe format.ts --allow-run --allow-write --allow-read --check
- job: "Windows"
pool:
vmImage: "vs2017-win2016"
steps:
- bash: npm install eslint typescript@$(TS_VERSION) @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier
- powershell: iwr https://deno.land/x/install/install.ps1 -out install.ps1; .\install.ps1 $(DENO_VERSION)
- bash: echo "##vso[task.prependpath]C:\Users\VssAdministrator\.deno\\bin"
- bash: npx eslint **/*.ts
- bash: deno.exe format.ts --allow-run --allow-write --allow-read --check
- bash: deno.exe test.ts --allow-run --allow-net --allow-write --allow-read
2 changes: 1 addition & 1 deletion benching/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bench({
runs: 100,
func(b: BenchmarkTimer) {
b.start();
for (let i: number = 0; i < 1e6; i++);
for (let i = 0; i < 1e6; i++);
b.stop();
}
});
Expand Down
22 changes: 11 additions & 11 deletions benching/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export interface BenchmarkTimer {
}

/** Defines a benchmark through a named function. */
export type BenchmarkFunction = {
export interface BenchmarkFunction {
(b: BenchmarkTimer): void | Promise<void>;
name: string;
};
}

/** Defines a benchmark definition with configurable runs. */
export interface BenchmarkDefinition {
Expand Down Expand Up @@ -69,7 +69,7 @@ function createBenchmarkTimer(clock: BenchmarkClock): BenchmarkTimer {
};
}

const candidates: Array<BenchmarkDefinition> = [];
const candidates: BenchmarkDefinition[] = [];

/** Registers a benchmark as a candidate for the runBenchmarks executor. */
export function bench(
Expand All @@ -95,27 +95,27 @@ export async function runBenchmarks({
skip = /^\s*$/
}: BenchmarkRunOptions = {}): Promise<void> {
// Filtering candidates by the "only" and "skip" constraint
const benchmarks: Array<BenchmarkDefinition> = candidates.filter(
const benchmarks: BenchmarkDefinition[] = candidates.filter(
({ name }) => only.test(name) && !skip.test(name)
);
// Init main counters and error flag
const filtered: number = candidates.length - benchmarks.length;
let measured: number = 0;
let failed: boolean = false;
const filtered = candidates.length - benchmarks.length;
let measured = 0;
let failed = false;
// Setting up a shared benchmark clock and timer
const clock: BenchmarkClock = { start: NaN, stop: NaN };
const b: BenchmarkTimer = createBenchmarkTimer(clock);
const b = createBenchmarkTimer(clock);
// Iterating given benchmark definitions (await-in-loop)
console.log(
"running",
benchmarks.length,
`benchmark${benchmarks.length === 1 ? " ..." : "s ..."}`
);
for (const { name, runs, func } of benchmarks) {
for (const { name, runs = 0, func } of benchmarks) {
// See https://github.com/denoland/deno/pull/1452 about groupCollapsed
console.groupCollapsed(`benchmark ${name} ... `);
// Trying benchmark.func
let result: string;
let result = "";
try {
if (runs === 1) {
// b is a benchmark timer interfacing an unset (NaN) benchmark clock
Expand All @@ -126,7 +126,7 @@ export async function runBenchmarks({
} else if (runs > 1) {
// Averaging runs
let pendingRuns = runs;
let totalMs: number = 0;
let totalMs = 0;
// Would be better 2 not run these serially
while (true) {
// b is a benchmark timer interfacing an unset (NaN) benchmark clock
Expand Down
8 changes: 4 additions & 4 deletions benching/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import "example.ts";
test(async function benching() {
bench(function forIncrementX1e9(b: BenchmarkTimer) {
b.start();
for (let i: number = 0; i < 1e9; i++);
for (let i = 0; i < 1e9; i++);
b.stop();
});

bench(function forDecrementX1e9(b: BenchmarkTimer) {
b.start();
for (let i: number = 1e9; i > 0; i--);
for (let i = 1e9; i > 0; i--);
b.stop();
});

bench(async function forAwaitFetchDenolandX10(b: BenchmarkTimer) {
b.start();
for (let i: number = 0; i < 10; i++) {
for (let i = 0; i < 10; i++) {
await fetch("https://deno.land/");
}
b.stop();
Expand All @@ -36,7 +36,7 @@ test(async function benching() {
runs: 100,
func(b: BenchmarkTimer) {
b.start();
for (let i: number = 0; i < 1e6; i++);
for (let i = 0; i < 1e6; i++);
b.stop();
}
});
Expand Down
2 changes: 1 addition & 1 deletion bytes/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function bytesFindIndex(a: Uint8Array, pat: Uint8Array): number {
}

/** Find last index of binary pattern from a. If not found, then return -1 **/
export function bytesFindLastIndex(a: Uint8Array, pat: Uint8Array) {
export function bytesFindLastIndex(a: Uint8Array, pat: Uint8Array): number {
const e = pat[pat.length - 1];
for (let i = a.length - 1; i >= 0; i--) {
if (a[i] !== e) continue;
Expand Down
4 changes: 2 additions & 2 deletions colors/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Code {

let enabled = !noColor;

export function setEnabled(value: boolean) {
export function setEnabled(value: boolean): void {
if (noColor) {
return;
}
Expand All @@ -29,7 +29,7 @@ function code(open: number, close: number): Code {
};
}

function run(str: string, code: Code) {
function run(str: string, code: Code): string {
return enabled
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
: str;
Expand Down
18 changes: 9 additions & 9 deletions datetime/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export function parseDate(dateStr: string, format: DateFormat): Date {

if (format === "mm-dd-yyyy") {
const datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;
[, m, d, y] = datePattern.exec(dateStr);
[, m, d, y] = datePattern.exec(dateStr)!;
} else if (format === "dd-mm-yyyy") {
const datePattern = /^(\d{2})-(\d{2})-(\d{4})$/;
[, d, m, y] = datePattern.exec(dateStr);
[, d, m, y] = datePattern.exec(dateStr)!;
} else if (format === "yyyy-mm-dd") {
const datePattern = /^(\d{4})-(\d{2})-(\d{2})$/;
[, y, m, d] = datePattern.exec(dateStr);
[, y, m, d] = datePattern.exec(dateStr)!;
} else {
throw new Error("Invalid date format!");
}
Expand Down Expand Up @@ -50,22 +50,22 @@ export function parseDateTime(

if (format === "mm-dd-yyyy hh:mm") {
const datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/;
[, m, d, y, ho, mi] = datePattern.exec(datetimeStr);
[, m, d, y, ho, mi] = datePattern.exec(datetimeStr)!;
} else if (format === "dd-mm-yyyy hh:mm") {
const datePattern = /^(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2})$/;
[, d, m, y, ho, mi] = datePattern.exec(datetimeStr);
[, d, m, y, ho, mi] = datePattern.exec(datetimeStr)!;
} else if (format === "yyyy-mm-dd hh:mm") {
const datePattern = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/;
[, y, m, d, ho, mi] = datePattern.exec(datetimeStr);
[, y, m, d, ho, mi] = datePattern.exec(datetimeStr)!;
} else if (format === "hh:mm mm-dd-yyyy") {
const datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/;
[, ho, mi, m, d, y] = datePattern.exec(datetimeStr);
[, ho, mi, m, d, y] = datePattern.exec(datetimeStr)!;
} else if (format === "hh:mm dd-mm-yyyy") {
const datePattern = /^(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{4})$/;
[, ho, mi, d, m, y] = datePattern.exec(datetimeStr);
[, ho, mi, d, m, y] = datePattern.exec(datetimeStr)!;
} else if (format === "hh:mm yyyy-mm-dd") {
const datePattern = /^(\d{2}):(\d{2}) (\d{4})-(\d{2})-(\d{2})$/;
[, ho, mi, y, m, d] = datePattern.exec(datetimeStr);
[, ho, mi, y, m, d] = datePattern.exec(datetimeStr)!;
} else {
throw new Error("Invalid datetime format!");
}
Expand Down
2 changes: 2 additions & 0 deletions datetime/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ test(function parseDateTime() {

test(function invalidParseDateTimeFormatThrows() {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(datetime as any).parseDateTime("2019-01-01 00:00", "x-y-z");
assert(false, "no exception was thrown");
} catch (e) {
Expand All @@ -55,6 +56,7 @@ test(function parseDate() {

test(function invalidParseDateFormatThrows() {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(datetime as any).parseDate("2019-01-01", "x-y-z");
assert(false, "no exception was thrown");
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions examples/gist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function pathBase(p: string): string {
return parts[parts.length - 1];
}

async function main() {
async function main(): Promise<void> {
const token = env()["GIST_TOKEN"];
if (!token) {
console.error("GIST_TOKEN environmental variable not set.");
Expand Down Expand Up @@ -46,7 +46,7 @@ async function main() {
headers: [
["Content-Type", "application/json"],
["User-Agent", "Deno-Gist"],
["Authorization", "token " + token]
["Authorization", `token ${token}`]
],
body
});
Expand Down
4 changes: 2 additions & 2 deletions examples/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
isWebSocketPingEvent
} from "https://deno.land/x/ws/mod.ts";

async function main() {
async function main(): Promise<void> {
console.log("websocket server is running on 0.0.0.0:8080");
for await (const req of serve("0.0.0.0:8080")) {
if (req.url === "/ws") {
Expand All @@ -22,7 +22,7 @@ async function main() {
// binary message
console.log("ws:Binary", ev);
} else if (isWebSocketPingEvent(ev)) {
const [_, body] = ev;
const [, body] = ev;
// ping
console.log("ws:Ping", body);
} else if (isWebSocketCloseEvent(ev)) {
Expand Down
2 changes: 1 addition & 1 deletion io/bufio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class BufReader implements Reader {
* delim.
* For simple uses, a Scanner may be more convenient.
*/
async readString(delim: string): Promise<string> {
async readString(_delim: string): Promise<string> {
throw new Error("Not implemented");
}

Expand Down
Loading

0 comments on commit c0390ad

Please sign in to comment.