Use Prettier for JS files (#3913)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
Francisco
2023-01-04 11:03:40 -03:00
committed by GitHub
parent 88754d0b36
commit a28aafdc85
135 changed files with 2737 additions and 3121 deletions

View File

@ -1,14 +1,14 @@
function formatLines (...lines) {
function formatLines(...lines) {
return [...indentEach(0, lines)].join('\n') + '\n';
}
function *indentEach (indent, lines) {
function* indentEach(indent, lines) {
for (const line of lines) {
if (Array.isArray(line)) {
yield * indentEach(indent + 1, line);
yield* indentEach(indent + 1, line);
} else {
const padding = ' '.repeat(indent);
yield * line.split('\n').map(subline => subline === '' ? '' : padding + subline);
yield* line.split('\n').map(subline => (subline === '' ? '' : padding + subline));
}
}
}

View File

@ -5,17 +5,15 @@ const fs = require('fs');
const path = require('path');
const format = require('./format-lines');
function getVersion (path) {
function getVersion(path) {
try {
return fs
.readFileSync(path, 'utf8')
.match(/\/\/ OpenZeppelin Contracts \(last updated v[^)]+\)/)[0];
return fs.readFileSync(path, 'utf8').match(/\/\/ OpenZeppelin Contracts \(last updated v[^)]+\)/)[0];
} catch (err) {
return null;
}
}
for (const [ file, template ] of Object.entries({
for (const [file, template] of Object.entries({
'utils/math/SafeCast.sol': './templates/SafeCast.js',
'utils/structs/EnumerableSet.sol': './templates/EnumerableSet.js',
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
@ -27,7 +25,7 @@ for (const [ file, template ] of Object.entries({
const version = getVersion(output);
const content = format(
'// SPDX-License-Identifier: MIT',
...(version ? [ version + ` (${file})` ] : []),
...(version ? [version + ` (${file})`] : []),
`// This file was procedurally generated from ${input}.`,
'',
require(template),

View File

@ -1,6 +1,6 @@
const format = require('../format-lines');
const VALUE_SIZES = [ 224, 160 ];
const VALUE_SIZES = [224, 160];
const header = `\
pragma solidity ^0.8.0;
@ -264,7 +264,7 @@ function _unsafeAccess(${opts.checkpointTypeName}[] storage self, uint256 pos)
/* eslint-enable max-len */
// OPTIONS
const defaultOpts = (size) => ({
const defaultOpts = size => ({
historyTypeName: `Trace${size}`,
checkpointTypeName: `Checkpoint${size}`,
checkpointFieldName: '_checkpoints',
@ -293,11 +293,7 @@ module.exports = format(
legacyOperations(LEGACY_OPTS),
common(LEGACY_OPTS),
// New flavors
...OPTS.flatMap(opts => [
types(opts),
operations(opts),
common(opts),
]),
...OPTS.flatMap(opts => [types(opts), operations(opts), common(opts)]),
],
'}',
);

View File

@ -245,9 +245,6 @@ function values(${name} storage set) internal view returns (${type}[] memory) {
module.exports = format(
header.trimEnd(),
'library EnumerableSet {',
[
defaultSet(),
TYPES.map(details => customSet(details).trimEnd()).join('\n\n'),
],
[defaultSet(), TYPES.map(details => customSet(details).trimEnd()).join('\n\n')],
'}',
);

View File

@ -8,55 +8,55 @@ const LENGTHS = range(8, 256, 8).reverse(); // 248 → 8 (in steps of 8)
// This is used in the docs for each function.
const version = (selector, length) => {
switch (selector) {
case 'toUint(uint)': {
switch (length) {
case 8:
case 16:
case 32:
case 64:
case 128:
return '2.5';
case 96:
case 224:
return '4.2';
default:
assert(LENGTHS.includes(length));
return '4.7';
case 'toUint(uint)': {
switch (length) {
case 8:
case 16:
case 32:
case 64:
case 128:
return '2.5';
case 96:
case 224:
return '4.2';
default:
assert(LENGTHS.includes(length));
return '4.7';
}
}
}
case 'toInt(int)': {
switch (length) {
case 8:
case 16:
case 32:
case 64:
case 128:
return '3.1';
default:
assert(LENGTHS.includes(length));
return '4.7';
case 'toInt(int)': {
switch (length) {
case 8:
case 16:
case 32:
case 64:
case 128:
return '3.1';
default:
assert(LENGTHS.includes(length));
return '4.7';
}
}
case 'toUint(int)': {
switch (length) {
case 256:
return '3.0';
default:
assert(false);
return;
}
}
case 'toInt(uint)': {
switch (length) {
case 256:
return '3.0';
default:
assert(false);
return;
}
}
}
case 'toUint(int)': {
switch (length) {
case 256:
return '3.0';
default:
assert(false);
return;
}
}
case 'toInt(uint)': {
switch (length) {
case 256:
return '3.0';
default:
assert(false);
return;
}
}
default:
assert(false);
}
};
@ -158,11 +158,6 @@ function toUint${length}(int${length} value) internal pure returns (uint${length
module.exports = format(
header.trimEnd(),
'library SafeCast {',
[
...LENGTHS.map(toUintDownCast),
toUint(256),
...LENGTHS.map(toIntDownCast),
toInt(256),
],
[...LENGTHS.map(toUintDownCast), toUint(256), ...LENGTHS.map(toIntDownCast), toInt(256)],
'}',
);

View File

@ -1,26 +1,26 @@
function toBytes32 (type, value) {
function toBytes32(type, value) {
switch (type) {
case 'bytes32':
return value;
case 'uint256':
return `bytes32(${value})`;
case 'address':
return `bytes32(uint256(uint160(${value})))`;
default:
throw new Error(`Conversion from ${type} to bytes32 not supported`);
case 'bytes32':
return value;
case 'uint256':
return `bytes32(${value})`;
case 'address':
return `bytes32(uint256(uint160(${value})))`;
default:
throw new Error(`Conversion from ${type} to bytes32 not supported`);
}
}
function fromBytes32 (type, value) {
function fromBytes32(type, value) {
switch (type) {
case 'bytes32':
return value;
case 'uint256':
return `uint256(${value})`;
case 'address':
return `address(uint160(uint256(${value})))`;
default:
throw new Error(`Conversion from bytes32 to ${type} not supported`);
case 'bytes32':
return value;
case 'uint256':
return `uint256(${value})`;
case 'address':
return `address(uint160(uint256(${value})))`;
default:
throw new Error(`Conversion from bytes32 to ${type} not supported`);
}
}