From 5dfd5445c841881c395d55c6d3b5d82f085688d9 Mon Sep 17 00:00:00 2001 From: filip mertens Date: Wed, 30 Nov 2022 19:18:41 +0100 Subject: [PATCH] parse config files --- apps/remix-ide/src/app/plugins/code-format.ts | 103 +++++++++++++----- package.json | 1 + yarn.lock | 2 +- 3 files changed, 77 insertions(+), 29 deletions(-) diff --git a/apps/remix-ide/src/app/plugins/code-format.ts b/apps/remix-ide/src/app/plugins/code-format.ts index 45b2191d2d..e732bb9ae9 100644 --- a/apps/remix-ide/src/app/plugins/code-format.ts +++ b/apps/remix-ide/src/app/plugins/code-format.ts @@ -7,6 +7,8 @@ import * as ts from 'prettier/parser-typescript' import * as babel from 'prettier/parser-babel' import * as espree from 'prettier/parser-espree' import path from 'path' +import yaml from 'js-yaml' +import toml from 'toml' const profile = { name: 'codeFormatter', @@ -28,10 +30,6 @@ export class CodeFormat extends Plugin { const content = await this.call('fileManager', 'readFile', file) if (!content) return let parserName = '' - // parse TOML file - // parse YAML file - // parse JSON file - let options: Options = { } switch (path.extname(file)) { @@ -62,24 +60,76 @@ export class CodeFormat extends Plugin { parserName = 'json' break } - const possibleFileNames = [ - '.prettierrc', - '.prettierrc.json', - '.prettierrc.yaml', - '.prettierrc.yml', - '.prettierrc.toml', - 'prettier.js', - 'prettier.cjs', - 'prettier.config.js', - 'prettier.config.cjs', - 'prettier.config.mjs', - 'prettier.config.ts', - ] - // find first file that exists - const prettierConfigFile = possibleFileNames.find(async fileName => await this.call('fileManager', 'exists', fileName)) + const possibleFileNames = [ + '.prettierrc', + '.prettierrc.json', + '.prettierrc.yaml', + '.prettierrc.yml', + '.prettierrc.toml', + '.prettierrc.js', + '.prettierrc.cjs', + 'prettier.config.js', + 'prettier.config.cjs', + '.prettierrc.json5', + ] - console.log(prettierConfigFile) + const prettierConfigFile = await findAsync(possibleFileNames, async (fileName) => { + const exists = await this.call('fileManager', 'exists', fileName) + return exists + }) + let parsed = null + if (prettierConfigFile) { + let prettierConfig = await this.call('fileManager', 'readFile', prettierConfigFile) + if (prettierConfig) { + if (prettierConfigFile.endsWith('.yaml') || prettierConfigFile.endsWith('.yml')) { + parsed = yaml.load(prettierConfig) + } else if (prettierConfigFile.endsWith('.toml')) { + parsed = toml.parse(prettierConfig) + } else if (prettierConfigFile.endsWith('.json') || prettierConfigFile.endsWith('.json5')) { + parsed = JSON.parse(prettierConfig) + } else if (prettierConfigFile === '.prettierrc') { + try { + parsed = JSON.parse(prettierConfig) + } catch (e) { + // do nothing + } + if (!parsed) { + try { + parsed = yaml.load(prettierConfig) + } catch (e) { + // do nothing + } + } + } else if (prettierConfigFile.endsWith('.js') || prettierConfigFile.endsWith('.cjs')) { + // remove any comments + prettierConfig = prettierConfig.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '') + // add quotes to keys + prettierConfig = prettierConfig.replace(/([a-zA-Z0-9_]+)(\s*):/g, '"$1"$2:') + // remove comma from last key + prettierConfig = prettierConfig.replace(/,(\s*})/g, '$1') + // remove any semi-colons + prettierConfig = prettierConfig.replace(/;/g, '') + // convert single quotes to double quotes + prettierConfig = prettierConfig.replace(/'/g, '"') + try { + parsed = JSON.parse(prettierConfig.replace('module.exports = ', '').replace('module.exports=', '')) + } catch (e) { + // do nothing + } + } + } + } + if(!parsed && prettierConfigFile) { + this.call('notification', 'toast', `Error parsing prettier config file: ${prettierConfigFile}`) + } + // merge options + if (parsed) { + options = { + ...options, + ...parsed, + } + } const result = prettier.format(content, { plugins: [sol as any, ts, babel, espree], parser: parserName, @@ -93,12 +143,9 @@ export class CodeFormat extends Plugin { } -function getRange(index, node) { - if (node.range) { - return node.range[index]; - } - if (node.expression && node.expression.range) { - return node.expression.range[index]; - } - return null; +async function findAsync(arr, asyncCallback) { + const promises = arr.map(asyncCallback); + const results = await Promise.all(promises); + const index = results.findIndex(result => result); + return arr[index]; } diff --git a/package.json b/package.json index 1b1aa766c1..a93bb7b3a4 100644 --- a/package.json +++ b/package.json @@ -194,6 +194,7 @@ "isbinaryfile": "^3.0.2", "isomorphic-git": "^1.8.2", "jquery": "^3.3.1", + "js-yaml": "^4.1.0", "jszip": "^3.6.0", "latest-version": "^5.1.0", "merge": "^2.1.1", diff --git a/yarn.lock b/yarn.lock index 66ac5c3d01..9f02594ed0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14745,7 +14745,7 @@ js-yaml@4.0.0: dependencies: argparse "^2.0.1" -js-yaml@4.1.0: +js-yaml@4.1.0, js-yaml@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602" integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==