Socket
Socket
Sign inDemoInstall

automutate

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

automutate - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

8

package.json
{
"name": "automutate",
"version": "0.1.0",
"version": "0.2.0",
"description": "Generic framework to fix linting errors in code.",

@@ -39,3 +39,3 @@ "index": "lib/index.js",

"type": "git",
"url": "git+https://github.com/autolint/automutate.git"
"url": "git+https://github.com/automutate/automutate.git"
},

@@ -55,5 +55,5 @@ "keywords": [

"bugs": {
"url": "https://github.com/autolint/automutate/issues"
"url": "https://github.com/automutate/automutate/issues"
},
"homepage": "https://github.com/autolint/automutate#readme"
"homepage": "https://github.com/automutate/automutate#readme"
}
# automutate
*[Rough Draft]* Applies waves of mutations provided by other tools, such as linters.
Applies waves of mutations provided by other tools, such as linters.

@@ -5,0 +5,0 @@ There are [various](https://github.com/eslint/eslint) [linters](https://github.com/palantir/tslint) [in](https://github.com/stylelint/stylelint) [the](https://github.com/lesshint/lesshint) [world](https://github.com/sasstools/sass-lint) and most are adding or have added ways to `--fix` rule failures automatically.

@@ -18,2 +18,3 @@ import * as fs from "fs";

* @param fileName Name of the file.
* @param fileSettings Settings for manipulating local files.
*/

@@ -50,3 +51,2 @@ public constructor(fileName: string) {

}
}
}

@@ -5,2 +5,9 @@ import { IMutation } from "./mutation";

/**
* Mutations applied to each file, keyed by file name.
*/
export interface IFileMutations {
[i: string]: IMutation[];
}
/**
* Generates output messages for significant operations.

@@ -10,2 +17,15 @@ */

/**
* Logs that mutations have completed.
*/
onComplete(): void;
/**
* Logs that a mutation was applied.
*
* @param fileName Name of the file to be mutated.
* @param mutation The requesting mutation.
*/
onMutation(fileName: string, mutation: IMutation): void;
/**
* Logs that an unknown mutator was requested.

@@ -31,1 +51,72 @@ *

}
/**
* Default no-op class to generate output messages for significant operations.
*/
export class Logger implements ILogger {
/**
* Mutations applied to each file, keyed by file name.
*/
private fileMutations: IFileMutations = {};
/**
* Waves of file mutations.
*/
private mutationsWaves: IMutationsWave[] = [];
/**
* Logs that mutations have completed.
*/
public onComplete(): void { }
/**
* Logs that a mutation was applied.
*
* @param fileName Name of the file to be mutated.
* @param mutation The requesting mutation.
*/
public onMutation(fileName: string, mutation: IMutation): void {
if (this.fileMutations[fileName]) {
this.fileMutations[fileName].push(mutation);
} else {
this.fileMutations[fileName] = [mutation];
}
}
/**
* Logs that an unknown mutator was requested.
*
* @param mutation The requesting mutation.
*/
public onUnknownMutationType(mutation: IMutation): void { }
/**
* Logs that a muations wave is about to start.
*
* @param mutationsWave A wave of file mutations.
*/
public onWaveBegin(mutationsWave: IMutationsWave): void {
this.mutationsWaves.push(mutationsWave);
}
/**
* Logs that a muations wave finished.
*
* @param mutationsWave A wave of file mutations.
*/
public onWaveEnd(mutationsWave: IMutationsWave): void { }
/**
* @returns Mutations applied to each file, keyed by file name.
*/
protected getFileMutations(): IFileMutations {
return this.fileMutations;
}
/**
* @returns Waves of file mutations.
*/
protected getMutationsWaves(): IMutationsWave[] {
return this.mutationsWaves;
}
}

@@ -1,4 +0,3 @@

import { ILogger } from "../logger";
import { IFileMutations, Logger } from "../logger";
import { IMutation } from "../mutation";
import { IMutationsWave } from "../mutationsProvider";

@@ -8,3 +7,3 @@ /**

*/
export class ConsoleLogger implements ILogger {
export class ConsoleLogger extends Logger {
/**

@@ -15,3 +14,5 @@ * Logs that an unknown mutator was requested.

*/
public onUnknownMutationType(mutation: IMutation): void {
onUnknownMutationType(mutation: IMutation): void {
super.onUnknownMutationType(mutation);
console.error(`Unknown mutator type: '${mutation.type}'`);

@@ -21,18 +22,36 @@ }

/**
* Logs that a muations wave is about to start.
*
* @param mutationsWave A wave of file mutations.
* Logs that mutations have completed.
*/
public onWaveBegin(mutationsWave: IMutationsWave): void {
console.log(`Applying wave...`);
public onComplete(): void {
super.onComplete();
const fileMutations: IFileMutations = this.getFileMutations();
const filesCount: number = Object.keys(fileMutations).length;
const mutationsCount: number = Object.keys(fileMutations)
.map((fileName: string): number => fileMutations[fileName].length)
.reduce((a: number, b: number): number => a + b);
const wavesCount: number = this.getMutationsWaves().length;
console.log([
"Completed ",
this.pluralize(mutationsCount, "mutation"),
" across ",
this.pluralize(filesCount, "file"),
" in ",
this.pluralize(wavesCount, "wave"),
"."
].join(""));
}
/**
* Logs that a muations wave finished.
* Displays a word and number, accounting for pluralization.
*
* @param mutationsWave A wave of file mutations.
* @param count How many of the word there are.
* @param word A word to display.
*/
public onWaveEnd(mutationsWave: IMutationsWave): void {
console.log(`Applied wave.`);
private pluralize(count: number, word: string) {
return count === 1
? `${count} ${word}`
: `${count} ${word}s`;
}
}
}
import { IFileProvider } from "./fileProvider";
import { IFileProviderFactory } from "./fileProviderFactory";
import { ILogger } from "./logger";
import { IMutation } from "./mutation";

@@ -34,2 +35,7 @@ import { IFileMutations } from "./mutationsProvider";

/**
* Generates output messages for significant operations.
*/
private readonly logger: ILogger;
/**
* Creates file providers for files.

@@ -47,6 +53,8 @@ */

*
* @param logger Generates output messages for significant operations.
* @param fileProviderFactory Creates file providers for files.
* @param mutatorFactory Creates mutators for mutations.
*/
public constructor(fileProviderFactory: IFileProviderFactory, mutatorFactory: IMutatorFactory) {
public constructor(logger: ILogger, fileProviderFactory: IFileProviderFactory, mutatorFactory: IMutatorFactory) {
this.logger = logger;
this.fileProviderFactory = fileProviderFactory;

@@ -68,2 +76,4 @@ this.mutatorFactory = mutatorFactory;

}));
this.logger.onComplete();
}

@@ -85,2 +95,3 @@

fileContents = this.mutatorFactory.generateAndApply(fileContents, mutation);
this.logger.onMutation(fileName, mutation);
}

@@ -87,0 +98,0 @@

@@ -5,2 +5,3 @@ import * as path from "path";

import { FileProviderFactory } from "../fileProviderFactory";
import { IFileProvider } from "../fileProvider";
import { LocalFileProvider } from "../fileProviders/localFileProvider";

@@ -12,2 +13,12 @@ import { MutationsApplier } from "../mutationsApplier";

/**
* Settings to apply individual waves of file mutations to local files.
*/
export interface IFileMutationSettings {
/**
* Additional directories to search for mutators within.
*/
mutatorDirectories?: string[];
}
/**
* Applies individual waves of file mutations to local files.

@@ -20,12 +31,16 @@ */

* @param logger Generates output messages for significant operations.
* @param fileSettings Settings for manipulating local files.
*/
public constructor(logger: ILogger) {
public constructor(logger: ILogger, settings: IFileMutationSettings = {}) {
super(
new FileProviderFactory(fileName => new LocalFileProvider(fileName)),
logger,
new FileProviderFactory(
(fileName: string): IFileProvider => new LocalFileProvider(fileName)),
new MutatorFactory(
new MutatorSearcher([
path.join(__dirname, "../../lib/mutators")
path.join(__dirname, "../../lib/mutators"),
...(settings.mutatorDirectories || [])
]),
logger));
}
}
}

@@ -29,3 +29,3 @@ import { ILogger } from "./logger";

*
* @param fileName Name of the file.
* @param fileContents Contents of the file.
* @param mutation Mutation to be applied to the file.

@@ -90,3 +90,3 @@ * @returns The mutated file contents.

*
* @param fileName Name of the file.
* @param fileContents Contents of the file.
* @param mutation Mutation to be applied to the file.

@@ -93,0 +93,0 @@ * @returns The mutated file contents.

import * as path from "path";
import { IMutationsApplier, MutationsApplier } from "../../lib/mutationsApplier";
import { ConsoleLogger } from "../../lib/loggers/consoleLogger";
import { IFileProvider } from "../../lib/fileProvider";
import { FileProviderFactory } from "../../lib/fileProviderFactory";
import { StubFileProvider } from "../../lib/fileProviders/stubFileProvider";
import { ILogger, Logger } from "../../lib/logger";
import { MutatorFactory } from "../../lib/mutatorFactory";

@@ -51,6 +51,8 @@ import { IMutatorSearcher, MutatorSearcher } from "../../lib/mutatorSearcher";

]);
const stubLogger: ILogger = new Logger();
const stubFileProvider: IFileProvider = new StubFileProvider(testCase.before);
const mutationsApplier: IMutationsApplier = new MutationsApplier(
stubLogger,
new FileProviderFactory((): IFileProvider => stubFileProvider),
new MutatorFactory(mutatorSearcher, new ConsoleLogger()));
new MutatorFactory(mutatorSearcher, stubLogger));

@@ -57,0 +59,0 @@ // Act

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc