Skip to content

Commit

Permalink
refactor: reduce unknowns
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Jun 22, 2024
1 parent f2e8dba commit f99ee04
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
6 changes: 3 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ async function buildLocalProject(options) {
const userSrc = options.userSrc();
const reviewElmJsonPath = path.join(userSrc, 'elm.json');

const reviewElmJson = await FS.readJsonFile(reviewElmJsonPath).catch(
(error) => {
const reviewElmJson = /** @type {ApplicationElmJson} */ (
await FS.readJsonFile(reviewElmJsonPath).catch((error) => {
if (error.code === 'ENOENT') {
return Promise.reject(
new ErrorMessage.CustomError(
Expand All @@ -134,7 +134,7 @@ I can help set you up with an initial configuration if you run ${chalk.magenta('
}

return Promise.reject(error);
}
})
);

validateElmReviewVersion(options, reviewElmJsonPath, reviewElmJson);
Expand Down
2 changes: 1 addition & 1 deletion lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function getOrCompute(folder, key, fn) {

const cachedResult = await FS.readJsonFile(filepath).catch(() => null);
if (cachedResult) {
return cachedResult;
return /** @type {T} */ (cachedResult);
}

const result = await fn();
Expand Down
8 changes: 4 additions & 4 deletions lib/fs-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const {rimraf} = require('rimraf');
* Read a JSON file.
*
* @param {string} file
* @param {((this: any, key: string, value: any) => any) | undefined} reviver
* @returns {Promise<any>}
* @param {((key: string, value: unknown) => unknown) | undefined} reviver
* @returns {Promise<unknown>}
*/
async function readJsonFile(file, reviver = undefined) {
const data = await readFile(file, {encoding: 'utf8', reviver});
Expand Down Expand Up @@ -65,10 +65,10 @@ const writeFile = util.promisify(fs.writeFile);
* @param {string} file
* @param {object} content
* @param {string | number | undefined} space
* @param {((this: any, key: string, value: any) => any) | undefined} [replacer]
* @param {((key: string, value: unknown) => unknown) | undefined} [replacer]
* @returns {Promise<void>}
*/
function writeJson(file, content, space, replacer) {
function writeJson(file, content, space, replacer = undefined) {
return writeFile(file, JSON.stringify(content, replacer, space));
}

Expand Down
5 changes: 3 additions & 2 deletions lib/project-json-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const elmRoot =
* @param {string} elmVersion
* @param {string} name
* @param {string} packageVersion
* @returns {Promise<object>}
* @returns {Promise<unknown>}
*/
function getElmJson(options, elmVersion, name, packageVersion) {
// Look for the dependency in ELM_HOME first
Expand Down Expand Up @@ -114,7 +114,7 @@ function getPackagePathInElmHome(elmVersion, name) {
* @param {string} elmVersion
* @param {string} name
* @param {string} packageVersion
* @returns {Promise<object>}
* @returns {Promise<unknown>}
*/
function getDocsJson(options, elmVersion, name, packageVersion) {
return FS.readJsonFile(
Expand All @@ -134,6 +134,7 @@ function getDocsJson(options, elmVersion, name, packageVersion) {
packageVersion,
'docs.json'
);

return FS.readJsonFile(cacheLocation).catch((error) => {
// Finally, try to download it from the packages website
if (options.offline) {
Expand Down
6 changes: 4 additions & 2 deletions lib/suppressed-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ async function read(options) {

return Promise.all(
files.map(async (/** @type {Path} */ filePath) => {
/** @type {{version: number, suppressions: Suppression[]}} */
const entry = await FS.readJsonFile(filePath);
const entry =
/** @type {{version: number, suppressions: Suppression[]}} */ (
await FS.readJsonFile(filePath)
);
if (entry.version !== 1) {
throw new ErrorMessage.CustomError(
// prettier-ignore
Expand Down
30 changes: 16 additions & 14 deletions lib/template-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,27 @@ ${error.toString().replace(/^Error: /, '')}`,
*/
async function addElmSyntax(options, elmVersion, elmSyntaxVersion) {
const elmJsonPath = path.resolve(parseElmFolder, 'elm.json');
const elmJson = await FS.readJsonFile(elmJsonPath).catch((error) => {
if (error.code === 'ENOENT') {
return Promise.reject(
new ErrorMessage.CustomError(
// prettier-ignore
'UNEXPECTED INTERNAL ERROR',
// prettier-ignore
`I was expecting to find the "parseElm" project at ${chalk.cyan(elmJsonPath)} but could not find it.
const elmJson = /** @type {ApplicationElmJson} */ (
await FS.readJsonFile(elmJsonPath).catch((error) => {
if (error.code === 'ENOENT') {
return Promise.reject(
new ErrorMessage.CustomError(
// prettier-ignore
'UNEXPECTED INTERNAL ERROR',
// prettier-ignore
`I was expecting to find the "parseElm" project at ${chalk.cyan(elmJsonPath)} but could not find it.
Please open an issue at the following link:
https://github.com/jfmengels/node-elm-review/issues/new
`,
options.elmJsonPath
)
);
}
options.elmJsonPath
)
);
}

return Promise.reject(error);
});
return Promise.reject(error);
})
);

elmJson['elm-version'] = elmVersion;
delete elmJson.dependencies.direct['stil4m/elm-syntax'];
Expand Down
4 changes: 2 additions & 2 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const SuppressedErrors = require('./suppressed-errors');
* @import {ReviewOptions} from "./types/options"
* @import {App} from "./types/app"
* @import {WatchOptions} from "./types/watch"
* @import {ApplicationElmJson, ExtraFileRequest, SourceDirectories} from "./types/content"
* @import {ApplicationElmJson, ElmJson, ExtraFileRequest, SourceDirectories} from "./types/content"
* @import {Path} from "./types/path"
*/

Expand Down Expand Up @@ -73,7 +73,7 @@ function watchFiles(
.on('change', async () => {
const newValue = await FS.readJsonFile(options.elmJsonPath);
if (JSON.stringify(newValue) !== JSON.stringify(elmJsonContent)) {
elmJsonContent = newValue;
elmJsonContent = /** @type {ElmJson} */ (newValue);
runReview = () => {};
clearTimeout(suppressedErrorsTimeout);
await Promise.all([
Expand Down

0 comments on commit f99ee04

Please sign in to comment.