Skip to content

structure

Layout rules for code: import rules for Python and Swift, and the files that no import rule covers. Level all adds lone files, pass-through folders, shared name prefixes, file and folder clashes, long files, trivial functions, private functions below public ones, bare Swift do blocks, and environment reads outside the environment readers.

Always.

Reports every block of code copied between files, naming both places.

Runs at the push stage, only at the all level, once per scope.

A fix in one place leaves the copied block broken in the other.

Move the shared block into one function both callers use.

Two files with the same long block of code report that block. Move it into one function, and call it from both places.

gspot ignore structure/jscpd --paths "<path>" --reason "<why>"

Applies [[import_rule]] to Python and Swift imports and to the types that Swift files name, and reports code files that no import rule covers. ESLint applies the rules to JavaScript and TypeScript imports.

Runs at the commit stage, once per scope. It skips until you set import_rule.

An import across the layers, or a file outside every rule, breaks the structure that the rules describe.

Move the code into a rule that may import it, move that rule above, list the rule in can_import, or add the file to the paths of a rule.

With [[import_rule]] records, a Python file under app/ that imports storage.models from a rule it may not import reports refused-import. A code file that no rule covers reports uncovered-file.

Rule What it checks What to do
refused-import A Python or Swift file imports only from its own import rule and from those that can_import lists, or every rule above it when the rule has no can_import. A Swift file also names only the types of those rules, because files of one target use each other with no import. Move the code into a layer that the file may import, or move the importing code up a layer.
uncovered-file Each code file matches the paths of an [[import_rule]] record. Extend the paths of the import rule that the file belongs to.
gspot ignore structure/import-rules --paths "<path>" --reason "<why>"
gspot ignore structure/import-rules --rule refused-import --reason "<why>"

Finds a folder that holds no files, directly or through its subfolders, and a folder that holds only .gitkeep or .keep.

Runs at the commit stage, once for the repository.

An empty folder promises code that is not there, and Git does not keep it, so it exists on one machine only.

Delete the folder.

An empty src/old/, or a src/old/ that holds only an empty src/old/tmp/, reports empty-folder, and so does a logs/ that holds only .gitkeep. Delete the folder.

gspot ignore structure/empty-folders --paths "<path>" --reason "<why>"

Finds a folder that holds one code file and nothing else.

Runs at the commit stage, only at the all level, once per scope.

A folder with one file adds a level to every import path and groups nothing.

Move the file up beside its neighbors.

With TypeScript at level all, a feature/only.ts alone in its folder reports lone-file. Move it up beside related code, and update its imports.

gspot ignore structure/lone-files --paths "<path>" --reason "<why>"

Finds a folder whose only entry is one folder.

Runs at the commit stage, only at the all level, once per scope.

A folder that only holds one folder adds a level to every path and groups nothing.

Move the inner folder up one level.

At level all, a src/models/ whose only entry is src/models/user/ reports pass-through-folder. Move user/ up to src/user/, and update its imports.

gspot ignore structure/pass-through-folders --paths "<path>" --reason "<why>"

Finds sibling files that share a name prefix, such as asset-card, asset-list, and asset-row.

Runs at the commit stage, only at the all level, once per scope.

Sibling files with one prefix split a single concept by suffix, and a folder of that name groups them.

Move the files into a folder named after the shared prefix, or raise limits.min_shared_prefix_files with a reason.

Sibling files asset-card.ts, asset-list.ts, and asset-row.ts share a repeated prefix. Move them into asset/ with names card.ts, list.ts, and row.ts, then update imports.

gspot ignore structure/shared-prefixes --paths "<path>" --reason "<why>"

Finds a file and a folder with the same name, apart from the extension, in the same place, such as turn.ts beside turn/.

Runs at the commit stage, only at the all level, once per scope.

An import of ./turn names both, and a reader cannot tell which one loads.

Rename the file or the folder so their names differ.

A report.ts beside a report/ folder reports a clash. Rename one after its job, and update each reference.

gspot ignore structure/file-folder-clashes --paths "<path>" --reason "<why>"

Checks the code line count of each Python, SQL and shell file against limits.<language>.max_file_lines. ESLint and SwiftLint count the other languages.

Runs at the commit stage, only at the all level.

A long file hides independent responsibilities from the reader.

Split the file by responsibility, or raise limits.<language>.max_file_lines with a reason.

A Python file over its line limit reports the count. Split it by responsibility.

gspot ignore structure/long-files --paths "<path>" --reason "<why>"

Finds Python, Swift, and Bash functions at or under limits.trivial_function_statements that at most one place calls, exported JavaScript and TypeScript functions of that size that one place calls, and files that hold only imports, re-exports, and aliases.

Runs at the commit stage, only at the all level.

A function of one statement that one place calls makes the reader jump for nothing.

Inline the function at its call and delete it, or import from the module that defines the code and delete the forwarding file.

A Bash function that only calls another, and that one place calls, reports a finding. Inline the call at that place, and delete the wrapper.

Rule What it checks What to do
trivial-function A named function has limits.<language>.trivial_function_statements statements or fewer, and one place in the scope calls it at most. Move the body into its caller, and delete the function. When a framework calls it by name, ignore the finding.
trivial-file A file holds only imports, re-exports, and aliases, and no code of its own. Point each import at the module that defines the code, then delete the file.
gspot ignore structure/trivial-functions --paths "<path>" --reason "<why>"
gspot ignore structure/trivial-functions --rule trivial-function --reason "<why>"

Finds a private top-level function below a public one in Python, Swift, JavaScript, and TypeScript.

Runs at the commit stage, only at the all level.

When each private function sits above the public functions, a reader finds a helper next to the code that uses it.

Move each private function above the public function that calls it.

A TypeScript file whose function parseRow sits below export function loadRows reports private-below-public. Move parseRow above loadRows.

gspot ignore structure/function-order --paths "<path>" --reason "<why>"

Finds Swift do blocks without catch. ESLint reports the bare blocks of JavaScript and TypeScript.

Runs at the commit stage, only at the all level.

A block that only groups statements hides a step that a named function would explain.

Move the statements into a function named after their step, or remove the do and keep the statements.

A Swift do { ... } without catch reports bare-block. Move its statements into a function named after their step, or remove the do.

gspot ignore structure/bare-blocks --paths "<path>" --reason "<why>"

Finds environment reads outside the files listed in structure.environment_readers.

Runs at the commit stage, only at the all level, once per scope. It skips until you set structure.environment_readers.

Environment reads spread through the code hide which settings a program needs.

Move the environment read into a file listed in structure.environment_readers, then pass the value to callers.

A Python file that uses os.environ outside structure.environment_readers reports a finding. Move that access into an environment reader, and pass the value to callers.

gspot ignore structure/environment-readers --paths "<path>" --reason "<why>"
Setting What it sets
limits.max_file_lines The most lines one file may have, not counting blank lines and comment-only lines.
limits.max_function_lines The most lines one function may have.
limits.max_function_parameters The most parameters one function may take.
limits.max_function_statements The most statements one function may have.
limits.max_block_nesting How deep blocks may nest inside one function.
limits.max_callback_nesting How deep callbacks may nest.
limits.max_cognitive_complexity The highest cognitive score one function may have; the score grows as the function gets harder to follow.
limits.max_cyclomatic_complexity The most paths that may run through one function.
limits.max_function_branches The most branches one function may have.
limits.max_function_returns The most return statements one function may have.
limits.max_function_assignments The most variable assignments one shell function may make.
limits.max_closure_lines The most lines one Swift closure may have.
limits.max_type_lines The most lines one Swift type body may have.
limits.max_index_exports The most re-exports one index file may hold.
limits.trivial_function_statements A function with this many statements or fewer, nested functions included, is reported when at most one place calls it.
limits.min_shared_prefix_files The number of siblings sharing a name prefix that makes a finding.
structure.reexports Where re-exports may go: none, or an index file that re-exports other files.
structure.min_duplicate_lines The fewest lines a copied block has before it counts.
structure.min_duplicate_tokens The fewest tokens a copied block has before it counts.
structure.environment_readers The files that may read environment variables; other files get the values passed in. Empty turns the check off.
import_rule Code may import its own files and the files of every import rule listed above it, never below. can_import replaces the rules above with an exact list. When import rules exist, every code file belongs to one, apart from tests, tool settings files, scripts, and generated files.
Tool Version
jscpd 5.4.1

.gspot/configurations/structure/jscpd.json