Skip to content

Commit

Permalink
refactor: renamed configs
Browse files Browse the repository at this point in the history
  • Loading branch information
0xGorilla committed Jan 12, 2024
1 parent a49ddd5 commit c5c8a23
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { getConfig } from './config';
const { config: configPath } = getArguments();
const config = await getConfig(configPath);

const ignoredPaths = config.ignore.map((path) => globSync(path, { cwd: config.root })).flat();
const sourceUnits = await getProjectCompiledSources(config.root, config.contracts, ignoredPaths);
const excludedPaths = config.exclude.map((path) => globSync(path, { cwd: config.root })).flat();
const sourceUnits = await getProjectCompiledSources(config.root, config.include, excludedPaths);
if (!sourceUnits.length) return console.error('No solidity files found in the specified directory');

const warnings = await processSources(sourceUnits, config);
Expand Down
4 changes: 2 additions & 2 deletions src/processor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { parseNodeNatspec } from './parser';
import { Config } from './utils';
import { validate } from './validator';
import { SourceUnit, FunctionDefinition } from 'solc-typed-ast';
import fs from 'fs';
import { InternalConfig } from './types/config';

interface IWarning {
location: string;
messages: string[];
}

export async function processSources(sourceUnits: SourceUnit[], config: Config): Promise<IWarning[]> {
export async function processSources(sourceUnits: SourceUnit[], config: InternalConfig): Promise<IWarning[]> {
let warnings: IWarning[] = [];

sourceUnits.forEach((sourceUnit) => {
Expand Down
8 changes: 4 additions & 4 deletions src/types/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export interface UserConfig {
contracts: string; // Required: The directory containing your Solidity contracts.
include: string; // Required: Glob pattern of files to process.
exclude?: string[]; // Optional: Glob patterns of files to exclude.
root?: string; // Optional: The target root directory.
enforceInheritdoc?: boolean; // Optional: If set to true, all external and public functions must have @inheritdoc.
constructorNatspec?: boolean; // Optional: True if constructor natspec is mandatory.
ignore?: string[]; // Optional: Glob pattern of files and directories to exclude from processing.
}

// Config type after all the default values are applied
export interface InternalConfig {
contracts: string;
include: string;
exclude: string[];
root: string;
enforceInheritdoc: boolean;
constructorNatspec: boolean;
ignore: string[];
}
18 changes: 5 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ import fs from 'fs/promises';
import path from 'path';
import { ASTKind, ASTReader, SourceUnit, compileSol } from 'solc-typed-ast';

export interface Config {
root: string;
contracts: string;
enforceInheritdoc: boolean;
constructorNatspec: boolean;
ignore: string[];
}

export async function getSolidityFiles(dir: string): Promise<string[]> {
let files = await fs.readdir(dir, { withFileTypes: true });
let solidityFiles: string[] = [];
Expand Down Expand Up @@ -41,9 +33,9 @@ export async function getRemappings(rootPath: string): Promise<string[]> {
}
}

export async function getProjectCompiledSources(rootPath: string, contractsPath: string, ignoredPaths: string[]): Promise<SourceUnit[]> {
export async function getProjectCompiledSources(rootPath: string, includedPath: string, excludedPaths: string[]): Promise<SourceUnit[]> {
// Fetch Solidity files from the specified directory
const solidityFiles: string[] = await getSolidityFiles(contractsPath);
const solidityFiles: string[] = await getSolidityFiles(includedPath);
const remappings: string[] = await getRemappings(rootPath);

const compiledFiles = await compileSol(solidityFiles, 'auto', {
Expand All @@ -56,9 +48,9 @@ export async function getProjectCompiledSources(rootPath: string, contractsPath:
new ASTReader()
.read(compiledFiles.data, ASTKind.Any, compiledFiles.files)
// avoid processing files that are not in the specified directory, e.g. node modules or other imported files
.filter((sourceUnit) => isFileInDirectory(contractsPath, sourceUnit.absolutePath))
// avoid processing files from ignored directories
.filter((sourceUnit) => !ignoredPaths.some((ignoredPath) => ignoredPath === sourceUnit.absolutePath))
.filter((sourceUnit) => isFileInDirectory(includedPath, sourceUnit.absolutePath))
// avoid processing files from excluded directories
.filter((sourceUnit) => !excludedPaths.some((excludedPath) => excludedPath === sourceUnit.absolutePath))
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InternalConfig } from './types/config';
import { Natspec } from './types/natspec';
import { Config } from './utils';
import { NodeToProcess } from './types/solc-typed-ast';
import {
EnumDefinition,
Expand All @@ -11,7 +11,7 @@ import {
VariableDeclaration,
} from 'solc-typed-ast';

export function validate(node: NodeToProcess, natspec: Natspec, config: Config): string[] {
export function validate(node: NodeToProcess, natspec: Natspec, config: InternalConfig): string[] {
// There is inheritdoc, no other validation is needed
if (natspec.inheritdoc) return [];

Expand Down
8 changes: 4 additions & 4 deletions test/processor.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { processSources } from '../src/processor';
import { getFileCompiledSource } from '../src/utils';
import { Config } from '../src/utils';
import { InternalConfig } from '../src/types/config';

describe('processSources', () => {
const config: Config = {
const config: InternalConfig = {
root: '.',
contracts: './sample-data',
include: './sample-data',
exclude: [],
enforceInheritdoc: false,
constructorNatspec: false,
ignore: [],
};

describe('LibrarySample.sol', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/validator.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { validate } from '../src/validator';
import { Config } from '../src/utils';
import { getFileCompiledSource } from '../src/utils';
import { NodeToProcess } from '../src/types/solc-typed-ast';
import { ContractDefinition } from 'solc-typed-ast';
import { InternalConfig } from '../src/types/config';

describe('validator function', () => {
let contract: ContractDefinition;
let node: NodeToProcess;

const config: Config = {
const config: InternalConfig = {
root: '.',
contracts: './sample-data',
include: './sample-data',
exclude: [],
enforceInheritdoc: false,
constructorNatspec: false,
ignore: [],
};

beforeAll(async () => {
Expand Down

0 comments on commit c5c8a23

Please sign in to comment.