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

Make ESLint rules stricter #37

Merged
merged 1 commit into from
Nov 5, 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
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** @type{import("eslint").Linter.Config} */
module.exports = {
extends: ["@kachkaev/eslint-config-base"],
extends: [
"@kachkaev/eslint-config-base",
"@kachkaev/eslint-config-base/extra-type-checking",
],
reportUnusedDisableDirectives: true,
rules: {
/* eslint-disable @typescript-eslint/naming-convention */
Expand Down
27 changes: 13 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Run build
run: yarn build

lint:
linting:
name: Linting
runs-on: ubuntu-latest

Expand Down Expand Up @@ -66,23 +66,22 @@ jobs:
if: ${{ success() || failure() }}
run: yarn lint:yarn-dedupe

unit-tests:
name: Unit tests
tests:
name: Tests
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-12, ubuntu-22.04, windows-2022]
os: [macos-12, windows-2022, ubuntu-22.04]
node: [14, 16, 18, 19]
exclude:
- os: macos-12
node: 14
- os: macos-12
node: 19
- os: windows-2022
node: 14
- os: windows-2022
node: 19
- os: ubuntu-22.04
node: 18
include:
- os: ubuntu-22.04
node: 18
extra: coverage
- os: ubuntu-22.04

steps:
- name: Check out repository
Expand All @@ -100,10 +99,10 @@ jobs:
- name: Run unit tests
run: yarn test
env:
TEST_COVERAGE: ${{ matrix.os == 'ubuntu-22.04' && matrix.node == 18 }}
COVERAGE: ${{ matrix.extra == 'coverage' }}

- name: Report code coverage to codecov.io
uses: codecov/codecov-action@v3
if: matrix.os == 'ubuntu-22.04' && matrix.node == 18
if: matrix.extra == 'coverage'
with:
files: coverage/lcov.info
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*
!dist/**/*
!dist/**
dist/**/*.test.*
!package.json
!README.md
Expand Down
2 changes: 1 addition & 1 deletion jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable import/no-anonymous-default-export */
/** @type {import("jest").Config} */
export default {
collectCoverage: !!process.env.TEST_COVERAGE,
collectCoverage: process.env.COVERAGE === "true",
collectCoverageFrom: ["dist/**/*.js", "!dist/**/*.test.js"],
testRegex: ".test.js$",
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"scripts": {
"build": "tsc",
"clean": "rimraf cache coverage dist",
"fix": "npm-run-all --continue-on-error fix:*",
"fix": "npm-run-all --continue-on-error \"fix:*\"",
"fix:eslint": "eslint --fix .",
"fix:markdownlint": "markdownlint --fix .",
"fix:prettier": "prettier --write .",
"fix:yarn-dedupe": "yarn dedupe",
"lint": "npm-run-all --continue-on-error lint:*",
"lint": "npm-run-all --continue-on-error \"lint:*\"",
"lint:markdownlint": "markdownlint .",
"lint:prettier": "prettier --check .",
"lint:tsc": "tsc --noEmit",
Expand Down
4 changes: 2 additions & 2 deletions src/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import prettier from "prettier";
import rimraf from "rimraf";
import sleep from "sleep-promise";

import * as util from "./util";
import * as helpers from "./helpers";

const fixturesDir = path.resolve(__dirname, "../fixtures");
const cacheDir = path.resolve(__dirname, "../cache");
Expand All @@ -18,7 +18,7 @@ test(`correctly deals with cache`, async () => {
process.env.PRETTIER_PLUGIN_ELM_CACHE_GC_INTERVAL = `${cacheGcInterval}`;

const spyForFormatTextWithElmFormat = jest.spyOn(
util,
helpers,
"formatTextWithElmFormat",
);

Expand Down
9 changes: 6 additions & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export const getCachedValue = <Args extends any[], Result>(

// load value or error from cache
try {
record = JSON.parse(fs.readFileSync(recordFilePath, "utf8"));
record = JSON.parse(fs.readFileSync(recordFilePath, "utf8")) as {
value: Result;
error?: undefined;
};
recordIsFromCache = true;
} catch {
// a failure to load from cache implies calling fn
Expand Down Expand Up @@ -122,11 +125,11 @@ export const getCachedValue = <Args extends any[], Result>(

if ("error" in record) {
// eslint-disable-next-line unicorn/error-message
const errorToThrow = new Error();
const errorToThrow = new Error() as Error & Record<string, unknown>;
for (const errorProperty in record.error) {
/* istanbul ignore else */
if (Object.prototype.hasOwnProperty.call(record.error, errorProperty)) {
(errorToThrow as any)[errorProperty] = record.error.property;
errorToThrow[errorProperty] = record.error.property;
}
}
throw errorToThrow;
Expand Down
1 change: 1 addition & 0 deletions src/util.ts → src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import execa from "execa";

let cachedElmFormatVersion: string;

export const getElmFormatVersion = () => {
if (!cachedElmFormatVersion) {
// a cleaner way of getting elm-format version
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Parser, Printer } from "prettier";

import { parse } from "./parser";
import { print } from "./printer";
import type { ElmNode } from "./types";

export const defaultOptions = {};

Expand All @@ -16,21 +19,21 @@ export const languages = [
},
];

export const parsers = {
export const parsers: Record<string, Parser<ElmNode>> = {
elm: {
parse,
astFormat: "elm-format",
// there's only a single node
locStart(node: any) {
locStart(node) {
return node.start;
},
locEnd(node: any) {
locEnd(node) {
return node.end;
},
},
};

export const printers = {
export const printers: Record<string, Printer<ElmNode>> = {
// eslint-disable-next-line @typescript-eslint/naming-convention
"elm-format": {
print,
Expand Down
13 changes: 7 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Parser, ParserOptions } from "prettier";
import type { Parser, ParserOptions } from "prettier";

import { getCachedValue } from "./cache";
import { formatTextWithElmFormat, getElmFormatVersion } from "./util";
import { formatTextWithElmFormat, getElmFormatVersion } from "./helpers";
import type { ElmNode } from "./types";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require("../package.json");
const pkg = require("../package.json") as { version: string };

/*
* Simply passing text to elm-format is not enough because of two problems:
Expand Down Expand Up @@ -34,8 +35,8 @@ const autogeneratedModuleDefinitionRegExp =
export const parse = (
text: string,
parsers: { [parserName: string]: Parser },
opts: ParserOptions & { parentParser?: string },
) => {
options: ParserOptions,
): ElmNode => {
// patch 1 step 1
const textToSend = `${text}${dummyComment}`;

Expand All @@ -54,7 +55,7 @@ export const parse = (

// patch 2
if (
opts.parentParser === "markdown" &&
options.parentParser === "markdown" &&
autogeneratedModuleDefinitionRegExp.test(formattedText) &&
!autogeneratedModuleDefinitionRegExp.test(text)
) {
Expand Down
6 changes: 4 additions & 2 deletions src/printer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { AstPath } from "prettier";
import type { AstPath, Doc } from "prettier";

export const print = (path: AstPath) => {
import type { ElmNode } from "./types";

export const print = (path: AstPath<ElmNode>): Doc => {
const node = path.getValue();

switch (node.type) {
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface ElmNode {
type: "elm-format";
body: string;
source: string;
start: number;
end: number;
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"esModuleInterop": true,
"inlineSourceMap": true,
"lib": ["ES2019"],
"isolatedModules": true,
"module": "commonjs",
"checkJs": true,
"outDir": "dist",
Expand Down