Skip to content

Commit

Permalink
Add caching based on file content and external references (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored Feb 12, 2021
1 parent 1b121e3 commit 265546e
Show file tree
Hide file tree
Showing 46 changed files with 3,035 additions and 65 deletions.
9 changes: 9 additions & 0 deletions .dependency-cruiser.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
"path": "^packages/[^/]+/test/"
}
},
{
"name": "not-to-spec",
"comment": "Don't allow dependencies to spec files",
"severity": "error",
"from": {},
"to": {
"path": "\\.spec\\.[jt]s$"
}
},
{
"name": "not-outside-package",
"comment": "Don't allow packages to anything outside of packages",
Expand Down
3 changes: 3 additions & 0 deletions baselines/packages/wotan/api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ export * from './src/services/default/line-switches';
export * from './src/services/default/message-handler';
export * from './src/services/default/resolver';
export * from './src/services/default/rule-loader-host';
export * from './src/services/default/state-persistence';
export * from './src/services/cached-file-system';
export * from './src/services/configuration-manager';
export * from './src/services/dependency-resolver';
export * from './src/services/formatter-loader';
export * from './src/services/processor-loader';
export * from './src/services/program-state';
export * from './src/services/rule-loader';
export { parseGlobalOptions, ParsedGlobalOptions, GLOBAL_OPTIONS_SPEC } from './src/argparse';
export * from './src/baseline';
Expand Down
9 changes: 9 additions & 0 deletions baselines/packages/wotan/api/packlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,21 @@ src/services/default/resolver.js.map
src/services/default/rule-loader-host.d.ts
src/services/default/rule-loader-host.js
src/services/default/rule-loader-host.js.map
src/services/default/state-persistence.d.ts
src/services/default/state-persistence.js
src/services/default/state-persistence.js.map
src/services/dependency-resolver.d.ts
src/services/dependency-resolver.js
src/services/dependency-resolver.js.map
src/services/formatter-loader.d.ts
src/services/formatter-loader.js
src/services/formatter-loader.js.map
src/services/processor-loader.d.ts
src/services/processor-loader.js
src/services/processor-loader.js.map
src/services/program-state.d.ts
src/services/program-state.js
src/services/program-state.js.map
src/services/rule-loader.d.ts
src/services/rule-loader.js
src/services/rule-loader.js.map
Expand Down
1 change: 1 addition & 0 deletions baselines/packages/wotan/api/src/argparse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export declare const GLOBAL_OPTIONS_SPEC: {
exclude: OptionParser.ParseFunction<readonly string[]>;
project: OptionParser.ParseFunction<readonly string[]>;
references: OptionParser.ParseFunction<boolean>;
cache: OptionParser.ParseFunction<boolean>;
formatter: OptionParser.ParseFunction<string | undefined>;
fix: OptionParser.ParseFunction<number | boolean>;
extensions: OptionParser.ParseFunction<string[] | undefined>;
Expand Down
4 changes: 3 additions & 1 deletion baselines/packages/wotan/api/src/linter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ export declare type UpdateFileCallback = (content: string, range: ts.TextChangeR
export declare class Linter {
constructor(ruleLoader: RuleLoader, logger: MessageHandler, deprecationHandler: DeprecationHandler, filterFactory: FindingFilterFactory);
lintFile(file: ts.SourceFile, config: EffectiveConfiguration, programOrFactory?: ProgramFactory | ts.Program, options?: LinterOptions): ReadonlyArray<Finding>;
lintAndFix(file: ts.SourceFile, content: string, config: EffectiveConfiguration, updateFile: UpdateFileCallback, iterations?: number, programFactory?: ProgramFactory, processor?: AbstractProcessor, options?: LinterOptions): LintAndFixFileResult;
lintAndFix(file: ts.SourceFile, content: string, config: EffectiveConfiguration, updateFile: UpdateFileCallback, iterations?: number, programFactory?: ProgramFactory, processor?: AbstractProcessor, options?: LinterOptions,
/** Initial set of findings from a cache. If provided, the initial linting is skipped and these findings are used for fixing. */
findings?: readonly Finding[]): LintAndFixFileResult;
}
12 changes: 3 additions & 9 deletions baselines/packages/wotan/api/src/runner.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Linter } from './linter';
import { LintResult, DirectoryService, MessageHandler, FileFilterFactory, Severity } from '@fimbul/ymir';
import * as ts from 'typescript';
import { ProcessorLoader } from './services/processor-loader';
import { CachedFileSystem } from './services/cached-file-system';
import { ConfigurationManager } from './services/configuration-manager';
import { ProgramStateFactory } from './services/program-state';
export interface LintOptions {
config: string | undefined;
files: ReadonlyArray<string>;
Expand All @@ -13,15 +13,9 @@ export interface LintOptions {
fix: boolean | number;
extensions: ReadonlyArray<string> | undefined;
reportUselessDirectives: Severity | boolean | undefined;
cache: boolean;
}
export declare class Runner {
constructor(fs: CachedFileSystem, configManager: ConfigurationManager, linter: Linter, processorLoader: ProcessorLoader, directories: DirectoryService, logger: MessageHandler, filterFactory: FileFilterFactory);
constructor(fs: CachedFileSystem, configManager: ConfigurationManager, linter: Linter, processorLoader: ProcessorLoader, directories: DirectoryService, logger: MessageHandler, filterFactory: FileFilterFactory, programStateFactory: ProgramStateFactory);
lintCollection(options: LintOptions): LintResult;
}
declare module 'typescript' {
function matchFiles(path: string, extensions: ReadonlyArray<string>, excludes: ReadonlyArray<string> | undefined, includes: ReadonlyArray<string>, useCaseSensitiveFileNames: boolean, currentDirectory: string, depth: number | undefined, getFileSystemEntries: (path: string) => ts.FileSystemEntries, realpath: (path: string) => string): string[];
interface FileSystemEntries {
readonly files: ReadonlyArray<string>;
readonly directories: ReadonlyArray<string>;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { CachedFileSystem } from '../cached-file-system';
import { StatePersistence, StaticProgramState } from '@fimbul/ymir';
export declare class DefaultStatePersistence implements StatePersistence {
constructor(fs: CachedFileSystem);
loadState(project: string): StaticProgramState | undefined;
saveState(project: string, state: StaticProgramState): void;
}
12 changes: 12 additions & 0 deletions baselines/packages/wotan/api/src/services/dependency-resolver.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as ts from 'typescript';
export interface DependencyResolver {
update(program: ts.Program, updatedFile: string): void;
getDependencies(fileName: string): ReadonlyMap<string, null | readonly string[]>;
getFilesAffectingGlobalScope(): readonly string[];
}
export declare type DependencyResolverHost = Required<Pick<ts.CompilerHost, 'resolveModuleNames'>> & {
useSourceOfProjectReferenceRedirect?(): boolean;
};
export declare class DependencyResolverFactory {
create(host: DependencyResolverHost, program: ts.Program): DependencyResolver;
}
23 changes: 23 additions & 0 deletions baselines/packages/wotan/api/src/services/program-state.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as ts from 'typescript';
import { DependencyResolver, DependencyResolverFactory, DependencyResolverHost } from './dependency-resolver';
import { Finding, StatePersistence } from '@fimbul/ymir';
export interface ProgramState {
update(program: ts.Program, updatedFile: string): void;
getUpToDateResult(fileName: string, configHash: string): readonly Finding[] | undefined;
setFileResult(fileName: string, configHash: string, result: readonly Finding[]): void;
save(): void;
}
export declare class ProgramStateFactory {
constructor(resolverFactory: DependencyResolverFactory, statePersistence: StatePersistence);
create(program: ts.Program, host: ProgramStateHost & DependencyResolverHost, tsconfigPath: string): ProgramStateImpl;
}
export declare type ProgramStateHost = Pick<ts.CompilerHost, 'getCanonicalFileName' | 'useCaseSensitiveFileNames'>;
declare const oldStateSymbol: unique symbol;
declare class ProgramStateImpl implements ProgramState {
constructor(host: ProgramStateHost, program: ts.Program, resolver: DependencyResolver, statePersistence: StatePersistence, project: string);
update(program: ts.Program, updatedFile: string): void;
getUpToDateResult(fileName: string, configHash: string): readonly Finding[] | undefined;
setFileResult(fileName: string, configHash: string, result: ReadonlyArray<Finding>): void;
save(): void;
}
export {};
215 changes: 215 additions & 0 deletions baselines/packages/wotan/test/program-state.spec.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
# Snapshot report for `packages/wotan/test/program-state.spec.ts`

The actual snapshot is saved in `program-state.spec.ts.snap`.

Generated by [AVA](https://avajs.dev).

## saves old state

> Snapshot 1
`cs: false␊
files:␊
- config: '1234'␊
hash: '-2704852577'␊
result: []␊
- config: '1234'␊
dependencies:␊
./c:␊
- 0␊
hash: '-5185547329'␊
result: []␊
- dependencies:␊
./c:␊
- 0␊
hash: '-5185547329'␊
- dependencies:␊
./b:␊
- 1␊
./d:␊
- 2␊
hash: '-2126001415'␊
global: []␊
lookup:␊
a.ts: 3␊
b.ts: 1␊
c.ts: 0␊
d.ts: 2␊
options: '-1350339532'␊
v: 1␊
`

> Snapshot 2
`cs: false␊
files:␊
- config: '1234'␊
hash: '-2704852577'␊
result: []␊
- config: '1234'␊
dependencies:␊
./c:␊
- 0␊
hash: '-5185547329'␊
result: []␊
- dependencies:␊
"\\0":␊
- 3␊
hash: '5381'␊
- dependencies:␊
./e:␊
- 2␊
- 3␊
hash: '910822549'␊
- dependencies:␊
./b:␊
- 1␊
./d:␊
- 3␊
hash: '-2126001415'␊
global: []␊
lookup:␊
a.ts: 4␊
b.ts: 1␊
c.ts: 0␊
d.ts: 3␊
e.ts: 2␊
options: '-1350339532'␊
v: 1␊
`

> Snapshot 3
`cs: false␊
files:␊
- hash: '-2704852577'␊
- dependencies:␊
./c:␊
- 0␊
hash: '-5185547329'␊
- dependencies:␊
"\\0":␊
- 3␊
e: null␊
hash: '8844149038'␊
- dependencies:␊
./e:␊
- 2␊
- 3␊
hash: '910822549'␊
- dependencies:␊
./b:␊
- 1␊
./d:␊
- 3␊
hash: '-2126001415'␊
global:␊
- 2␊
lookup:␊
a.ts: 4␊
b.ts: 1␊
c.ts: 0␊
d.ts: 3␊
e.ts: 2␊
options: '-1350339532'␊
v: 1␊
`

## doesn't discard results from old state

> Snapshot 1
`cs: false␊
files:␊
- config: '1234'␊
hash: '-3360789062'␊
result: []␊
- config: '1234'␊
hash: '574235295'␊
result: []␊
- dependencies:␊
./c:␊
- 1␊
hash: '-5185547329'␊
global:␊
- 1␊
lookup:␊
a.ts: 0␊
b.ts: 2␊
c.ts: 1␊
options: '5864093'␊
v: 1␊
`

> Snapshot 2
`cs: false␊
files:␊
- hash: '-3360789062'␊
- hash: '4830905933'␊
- dependencies:␊
./c:␊
- 1␊
hash: '-5185547329'␊
global:␊
- 1␊
lookup:␊
a.ts: 0␊
b.ts: 2␊
c.ts: 1␊
options: '5864093'␊
v: 1␊
`

## handles circular dependencies

> Snapshot 1
`cs: false␊
files:␊
- config: '1234'␊
hash: '-3360789062'␊
result: []␊
- dependencies:␊
./d:␊
- 3␊
hash: '-2335134369'␊
- dependencies:␊
./a:␊
- 4␊
./c:␊
- 1␊
./d:␊
- 3␊
hash: '-10011708814'␊
- dependencies:␊
./b:␊
- 2␊
./e:␊
- 0␊
hash: '970287226'␊
- dependencies:␊
./b:␊
- 2␊
./c:␊
- 1␊
./d:␊
- 3␊
./e:␊
- 0␊
hash: '3028017583'␊
- dependencies:␊
./a:␊
- 4␊
hash: '-5185549507'␊
global: []␊
lookup:␊
a.ts: 4␊
b.ts: 2␊
c.ts: 1␊
d.ts: 3␊
e.ts: 0␊
root.ts: 5␊
options: '5864093'␊
v: 1␊
`
Binary file not shown.
Loading

0 comments on commit 265546e

Please sign in to comment.