@whaaaley/crush 0.0.0-stage → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +32 -0
- package/README.md +185 -2
- package/SKILL.md +187 -0
- package/THIRD_PARTY_NOTICES.md +277 -0
- package/dist/capture/capture.operations.d.ts +23 -0
- package/dist/capture/capture.operations.js +231 -0
- package/dist/capture/capture.schema.d.ts +73 -0
- package/dist/capture/capture.schema.js +28 -0
- package/dist/collapse/collapse.operations.d.ts +2 -0
- package/dist/collapse/collapse.operations.js +82 -0
- package/dist/collapse/collapse.schema.d.ts +40 -0
- package/dist/collapse/collapse.schema.js +35 -0
- package/dist/crush.tables.d.ts +3583 -0
- package/dist/crush.tables.js +456 -0
- package/dist/crushes/callback.crush.d.ts +2 -0
- package/dist/crushes/callback.crush.js +19 -0
- package/dist/crushes/crush.d.ts +36 -0
- package/dist/crushes/crush.js +12 -0
- package/dist/crushes/crush.names.d.ts +1 -0
- package/dist/crushes/crush.names.js +2 -0
- package/dist/crushes/crushes.d.ts +2 -0
- package/dist/crushes/crushes.js +9 -0
- package/dist/crushes/default-value.crush.d.ts +2 -0
- package/dist/crushes/default-value.crush.js +29 -0
- package/dist/crushes/drop-operand.crush.d.ts +2 -0
- package/dist/crushes/drop-operand.crush.js +37 -0
- package/dist/crushes/fold.d.ts +64 -0
- package/dist/crushes/fold.js +366 -0
- package/dist/crushes/force-fork.crush.d.ts +2 -0
- package/dist/crushes/force-fork.crush.js +79 -0
- package/dist/crushes/loop-once.crush.d.ts +2 -0
- package/dist/crushes/loop-once.crush.js +71 -0
- package/dist/crushes/optional-chain.crush.d.ts +2 -0
- package/dist/crushes/optional-chain.crush.js +30 -0
- package/dist/crushes/switch-case.crush.d.ts +2 -0
- package/dist/crushes/switch-case.crush.js +19 -0
- package/dist/decisions/decide.d.ts +61 -0
- package/dist/decisions/decide.js +161 -0
- package/dist/decisions/decisions.d.ts +24 -0
- package/dist/decisions/decisions.js +62 -0
- package/dist/decisions/forks.decision.d.ts +4 -0
- package/dist/decisions/forks.decision.js +32 -0
- package/dist/decisions/kinds.d.ts +101 -0
- package/dist/decisions/kinds.js +38 -0
- package/dist/decisions/loops.decision.d.ts +4 -0
- package/dist/decisions/loops.decision.js +39 -0
- package/dist/decisions/shorts.decision.d.ts +3 -0
- package/dist/decisions/shorts.decision.js +66 -0
- package/dist/decisions/switches.decision.d.ts +3 -0
- package/dist/decisions/switches.decision.js +20 -0
- package/dist/decisions/throws.decision.d.ts +3 -0
- package/dist/decisions/throws.decision.js +35 -0
- package/dist/decisions/values.decision.d.ts +3 -0
- package/dist/decisions/values.decision.js +67 -0
- package/dist/explore/edges/explore.d.ts +60 -0
- package/dist/explore/edges/explore.db.d.ts +22 -0
- package/dist/explore/edges/explore.db.js +55 -0
- package/dist/explore/edges/explore.js +305 -0
- package/dist/explore/edges/random.d.ts +2 -0
- package/dist/explore/edges/random.js +32 -0
- package/dist/explore/explore.operations.d.ts +30 -0
- package/dist/explore/explore.operations.js +117 -0
- package/dist/explore/limits.operations.d.ts +14 -0
- package/dist/explore/limits.operations.js +20 -0
- package/dist/explore/targets.operations.d.ts +36 -0
- package/dist/explore/targets.operations.js +32 -0
- package/dist/explore/utils/draws.d.ts +6 -0
- package/dist/explore/utils/draws.js +2 -0
- package/dist/explore/utils/edits.d.ts +15 -0
- package/dist/explore/utils/edits.js +151 -0
- package/dist/explore/utils/guide.d.ts +23 -0
- package/dist/explore/utils/guide.js +35 -0
- package/dist/explore/utils/keep.d.ts +10 -0
- package/dist/explore/utils/keep.js +61 -0
- package/dist/explore/utils/pools.d.ts +17 -0
- package/dist/explore/utils/pools.js +121 -0
- package/dist/explore/utils/reads.d.ts +19 -0
- package/dist/explore/utils/reads.js +118 -0
- package/dist/explore/utils/twin.d.ts +21 -0
- package/dist/explore/utils/twin.js +55 -0
- package/dist/forks/forks.operations.d.ts +32 -0
- package/dist/forks/forks.operations.js +134 -0
- package/dist/forks/forks.schema.d.ts +20 -0
- package/dist/forks/forks.schema.js +14 -0
- package/dist/graph/cfg/cfg.d.ts +28 -0
- package/dist/graph/cfg/cfg.js +31 -0
- package/dist/graph/cfg/code-paths/analyzer.d.ts +27 -0
- package/dist/graph/cfg/code-paths/analyzer.js +358 -0
- package/dist/graph/cfg/code-paths/segments.d.ts +44 -0
- package/dist/graph/cfg/code-paths/segments.js +105 -0
- package/dist/graph/cfg/code-paths/state.d.ts +121 -0
- package/dist/graph/cfg/code-paths/state.js +611 -0
- package/dist/graph/cfg/entries.d.ts +13 -0
- package/dist/graph/cfg/entries.js +120 -0
- package/dist/graph/cfg/functions.d.ts +6 -0
- package/dist/graph/cfg/functions.js +47 -0
- package/dist/graph/cfg/paths.d.ts +27 -0
- package/dist/graph/cfg/paths.js +91 -0
- package/dist/graph/cfg/scopes/scopes.d.ts +40 -0
- package/dist/graph/cfg/scopes/scopes.js +375 -0
- package/dist/graph/symbols/file.d.ts +90 -0
- package/dist/graph/symbols/file.js +427 -0
- package/dist/graph/symbols/keys.d.ts +13 -0
- package/dist/graph/symbols/keys.js +22 -0
- package/dist/graph/symbols/resolve.operations.d.ts +4 -0
- package/dist/graph/symbols/resolve.operations.js +33 -0
- package/dist/graph/symbols/symbols.d.ts +12 -0
- package/dist/graph/symbols/symbols.js +155 -0
- package/dist/graph/symbols/symbols.schema.d.ts +32 -0
- package/dist/graph/symbols/symbols.schema.js +19 -0
- package/dist/graph/unused/tests.d.ts +7 -0
- package/dist/graph/unused/tests.js +95 -0
- package/dist/graph/unused/unused.d.ts +53 -0
- package/dist/graph/unused/unused.js +319 -0
- package/dist/graph/walk/walk.d.ts +7 -0
- package/dist/graph/walk/walk.js +39 -0
- package/dist/instrument/instrument.operations.d.ts +72 -0
- package/dist/instrument/instrument.operations.js +786 -0
- package/dist/instrument/instrument.schema.d.ts +257 -0
- package/dist/instrument/instrument.schema.js +100 -0
- package/dist/judge/counters.d.ts +86 -0
- package/dist/judge/counters.js +402 -0
- package/dist/judge/edges/api.d.ts +14 -0
- package/dist/judge/edges/api.js +80 -0
- package/dist/judge/edges/completion.reporter.d.ts +9 -0
- package/dist/judge/edges/completion.reporter.js +51 -0
- package/dist/judge/edges/hints.reporter.d.ts +13 -0
- package/dist/judge/edges/hints.reporter.js +29 -0
- package/dist/judge/edges/judge.edges.d.ts +52 -0
- package/dist/judge/edges/judge.edges.js +59 -0
- package/dist/judge/edges/judge.fs.d.ts +8 -0
- package/dist/judge/edges/judge.fs.js +45 -0
- package/dist/judge/edges/merge.d.ts +11 -0
- package/dist/judge/edges/merge.js +60 -0
- package/dist/judge/edges/plugin.d.ts +132 -0
- package/dist/judge/edges/plugin.js +441 -0
- package/dist/judge/edges/reach.d.ts +68 -0
- package/dist/judge/edges/reach.js +132 -0
- package/dist/judge/edges/speed.d.ts +32 -0
- package/dist/judge/edges/speed.js +34 -0
- package/dist/judge/edges/strays.d.ts +1 -0
- package/dist/judge/edges/strays.js +14 -0
- package/dist/judge/edges/vitest.d.ts +20 -0
- package/dist/judge/edges/vitest.js +36 -0
- package/dist/judge/edges/vitest.schema.d.ts +14 -0
- package/dist/judge/edges/vitest.schema.js +16 -0
- package/dist/judge/explore.test.d.ts +1 -0
- package/dist/judge/explore.test.js +101 -0
- package/dist/judge/hints.d.ts +11 -0
- package/dist/judge/hints.js +44 -0
- package/dist/judge/judge.operations.d.ts +218 -0
- package/dist/judge/judge.operations.js +464 -0
- package/dist/judge/judge.schema.d.ts +135 -0
- package/dist/judge/judge.schema.js +129 -0
- package/dist/judge/judge.shard-1.test.d.ts +1 -0
- package/dist/judge/judge.shard-1.test.js +3 -0
- package/dist/judge/judge.shard-2.test.d.ts +1 -0
- package/dist/judge/judge.shard-2.test.js +3 -0
- package/dist/judge/judge.test.d.ts +1 -0
- package/dist/judge/judge.test.js +3 -0
- package/dist/judge/refusals.d.ts +14 -0
- package/dist/judge/refusals.js +10 -0
- package/dist/judge/report.test.d.ts +1 -0
- package/dist/judge/report.test.js +432 -0
- package/dist/judge/runner.d.ts +3 -0
- package/dist/judge/runner.js +1602 -0
- package/dist/judge/stresses.test.d.ts +1 -0
- package/dist/judge/stresses.test.js +12 -0
- package/dist/judge/task-meta.d.ts +51 -0
- package/dist/judge/task-meta.js +3 -0
- package/dist/justify/edges/load.d.ts +7 -0
- package/dist/justify/edges/load.js +40 -0
- package/dist/justify/edges/refresh.fs.d.ts +8 -0
- package/dist/justify/edges/refresh.fs.js +15 -0
- package/dist/justify/justify.operations.d.ts +46 -0
- package/dist/justify/justify.operations.js +80 -0
- package/dist/justify/justify.schema.d.ts +21 -0
- package/dist/justify/justify.schema.js +11 -0
- package/dist/mutators/arithmetic-operator.mutator.d.ts +2 -0
- package/dist/mutators/arithmetic-operator.mutator.js +22 -0
- package/dist/mutators/array-declaration.mutator.d.ts +2 -0
- package/dist/mutators/array-declaration.mutator.js +12 -0
- package/dist/mutators/arrow-function.mutator.d.ts +2 -0
- package/dist/mutators/arrow-function.mutator.js +10 -0
- package/dist/mutators/assignment-operator.mutator.d.ts +2 -0
- package/dist/mutators/assignment-operator.mutator.js +29 -0
- package/dist/mutators/await-expression.mutator.d.ts +2 -0
- package/dist/mutators/await-expression.mutator.js +7 -0
- package/dist/mutators/bitwise-operator.mutator.d.ts +2 -0
- package/dist/mutators/bitwise-operator.mutator.js +19 -0
- package/dist/mutators/block-statement.mutator.d.ts +2 -0
- package/dist/mutators/block-statement.mutator.js +30 -0
- package/dist/mutators/boolean-literal.mutator.d.ts +2 -0
- package/dist/mutators/boolean-literal.mutator.js +5 -0
- package/dist/mutators/coercion.mutator.d.ts +2 -0
- package/dist/mutators/coercion.mutator.js +28 -0
- package/dist/mutators/conditional-expression.mutator.d.ts +2 -0
- package/dist/mutators/conditional-expression.mutator.js +36 -0
- package/dist/mutators/disposal.mutator.d.ts +2 -0
- package/dist/mutators/disposal.mutator.js +13 -0
- package/dist/mutators/empty-destructuring.mutator.d.ts +2 -0
- package/dist/mutators/empty-destructuring.mutator.js +35 -0
- package/dist/mutators/empty-return.mutator.d.ts +2 -0
- package/dist/mutators/empty-return.mutator.js +35 -0
- package/dist/mutators/equality-operator.mutator.d.ts +2 -0
- package/dist/mutators/equality-operator.mutator.js +16 -0
- package/dist/mutators/loop-exit.mutator.d.ts +2 -0
- package/dist/mutators/loop-exit.mutator.js +5 -0
- package/dist/mutators/method-expression.mutator.d.ts +2 -0
- package/dist/mutators/method-expression.mutator.js +59 -0
- package/dist/mutators/missing-lookup.mutator.d.ts +2 -0
- package/dist/mutators/missing-lookup.mutator.js +25 -0
- package/dist/mutators/mutator.d.ts +40 -0
- package/dist/mutators/mutator.js +49 -0
- package/dist/mutators/mutator.names.d.ts +3 -0
- package/dist/mutators/mutator.names.js +43 -0
- package/dist/mutators/mutators.d.ts +8 -0
- package/dist/mutators/mutators.js +84 -0
- package/dist/mutators/nested-condition.mutator.d.ts +2 -0
- package/dist/mutators/nested-condition.mutator.js +16 -0
- package/dist/mutators/number-literal.mutator.d.ts +3 -0
- package/dist/mutators/number-literal.mutator.js +13 -0
- package/dist/mutators/object-literal.mutator.d.ts +2 -0
- package/dist/mutators/object-literal.mutator.js +6 -0
- package/dist/mutators/odd-number.mutator.d.ts +2 -0
- package/dist/mutators/odd-number.mutator.js +15 -0
- package/dist/mutators/operand-deletion.mutator.d.ts +2 -0
- package/dist/mutators/operand-deletion.mutator.js +9 -0
- package/dist/mutators/outcome.mutator.d.ts +3 -0
- package/dist/mutators/outcome.mutator.js +45 -0
- package/dist/mutators/plain-assignment.mutator.d.ts +2 -0
- package/dist/mutators/plain-assignment.mutator.js +7 -0
- package/dist/mutators/power-operator.mutator.d.ts +2 -0
- package/dist/mutators/power-operator.mutator.js +13 -0
- package/dist/mutators/private-brand.mutator.d.ts +2 -0
- package/dist/mutators/private-brand.mutator.js +7 -0
- package/dist/mutators/regex.mutator.d.ts +2 -0
- package/dist/mutators/regex.mutator.js +29 -0
- package/dist/mutators/short-iteration.mutator.d.ts +2 -0
- package/dist/mutators/short-iteration.mutator.js +8 -0
- package/dist/mutators/string-literal.mutator.d.ts +3 -0
- package/dist/mutators/string-literal.mutator.js +149 -0
- package/dist/mutators/switch-default.mutator.d.ts +2 -0
- package/dist/mutators/switch-default.mutator.js +13 -0
- package/dist/mutators/sync-iteration.mutator.d.ts +3 -0
- package/dist/mutators/sync-iteration.mutator.js +9 -0
- package/dist/mutators/thrown-call.mutator.d.ts +2 -0
- package/dist/mutators/thrown-call.mutator.js +7 -0
- package/dist/mutators/unary-operator.mutator.d.ts +2 -0
- package/dist/mutators/unary-operator.mutator.js +18 -0
- package/dist/mutators/unbound-call.mutator.d.ts +2 -0
- package/dist/mutators/unbound-call.mutator.js +14 -0
- package/dist/mutators/update-operator.mutator.d.ts +2 -0
- package/dist/mutators/update-operator.mutator.js +15 -0
- package/dist/mutators/yield-value.mutator.d.ts +2 -0
- package/dist/mutators/yield-value.mutator.js +7 -0
- package/dist/parity/compare.d.ts +27 -0
- package/dist/parity/compare.js +26 -0
- package/dist/parity/corpus.parity.d.ts +1 -0
- package/dist/parity/corpus.parity.js +97 -0
- package/dist/parity/edges/corpus.fs.d.ts +3 -0
- package/dist/parity/edges/corpus.fs.js +30 -0
- package/dist/parity/edges/ports.d.ts +2 -0
- package/dist/parity/edges/ports.js +21 -0
- package/dist/parity/edges/vitest.d.ts +2 -0
- package/dist/parity/edges/vitest.js +3 -0
- package/dist/parity/parity.operations.d.ts +33 -0
- package/dist/parity/parity.operations.js +60 -0
- package/dist/parity/parity.schema.d.ts +5 -0
- package/dist/parity/parity.schema.js +6 -0
- package/dist/parity/stryker/compare.d.ts +14 -0
- package/dist/parity/stryker/compare.js +52 -0
- package/dist/parity/stryker/edges/ports.d.ts +12 -0
- package/dist/parity/stryker/edges/ports.js +28 -0
- package/dist/parity/stryker/equivalence.d.ts +1 -0
- package/dist/parity/stryker/equivalence.js +42 -0
- package/dist/parity/stryker/intended.d.ts +29 -0
- package/dist/parity/stryker/intended.js +152 -0
- package/dist/parity/stryker/normalize.d.ts +27 -0
- package/dist/parity/stryker/normalize.js +181 -0
- package/dist/parity/stryker/rules.d.ts +6 -0
- package/dist/parity/stryker/rules.js +116 -0
- package/dist/parity/stryker/sides.d.ts +46 -0
- package/dist/parity/stryker/sides.js +199 -0
- package/dist/parity/stryker/stryker.parity.d.ts +1 -0
- package/dist/parity/stryker/stryker.parity.js +147 -0
- package/dist/parity/stryker/stryker.schema.d.ts +6 -0
- package/dist/parity/stryker/stryker.schema.js +7 -0
- package/dist/parity/views.d.ts +21 -0
- package/dist/parity/views.js +119 -0
- package/dist/questions/questions.operations.d.ts +110 -0
- package/dist/questions/questions.operations.js +446 -0
- package/dist/questions/questions.schema.d.ts +57 -0
- package/dist/questions/questions.schema.js +28 -0
- package/dist/record/edges/calls.db.d.ts +10 -0
- package/dist/record/edges/calls.db.js +91 -0
- package/dist/record/edges/hits.db.d.ts +3 -0
- package/dist/record/edges/hits.db.js +5 -0
- package/dist/record/edges/holds.db.d.ts +4 -0
- package/dist/record/edges/holds.db.js +31 -0
- package/dist/record/edges/judged-state.db.d.ts +5 -0
- package/dist/record/edges/judged-state.db.js +25 -0
- package/dist/record/edges/markers.db.d.ts +11 -0
- package/dist/record/edges/markers.db.js +55 -0
- package/dist/record/edges/merge.db.d.ts +7 -0
- package/dist/record/edges/merge.db.js +46 -0
- package/dist/record/edges/parts.db.d.ts +42 -0
- package/dist/record/edges/parts.db.js +389 -0
- package/dist/record/edges/parts.edges.d.ts +2 -0
- package/dist/record/edges/parts.edges.js +7 -0
- package/dist/record/edges/parts.fs.d.ts +1 -0
- package/dist/record/edges/parts.fs.js +11 -0
- package/dist/record/edges/parts.writer.d.ts +1 -0
- package/dist/record/edges/parts.writer.js +85 -0
- package/dist/record/edges/passes.db.d.ts +19 -0
- package/dist/record/edges/passes.db.js +31 -0
- package/dist/record/edges/plan.db.d.ts +21 -0
- package/dist/record/edges/plan.db.js +174 -0
- package/dist/record/edges/reach.db.d.ts +6 -0
- package/dist/record/edges/reach.db.js +62 -0
- package/dist/record/edges/record.db.d.ts +22 -0
- package/dist/record/edges/record.db.js +176 -0
- package/dist/record/edges/record.fs.d.ts +3 -0
- package/dist/record/edges/record.fs.js +7 -0
- package/dist/record/edges/site-verdicts.db.d.ts +11 -0
- package/dist/record/edges/site-verdicts.db.js +66 -0
- package/dist/record/edges/verified.db.d.ts +7 -0
- package/dist/record/edges/verified.db.js +71 -0
- package/dist/record/parts.operations.d.ts +26 -0
- package/dist/record/parts.operations.js +31 -0
- package/dist/record/path-checks.operations.d.ts +15 -0
- package/dist/record/path-checks.operations.js +84 -0
- package/dist/record/record.operations.d.ts +36 -0
- package/dist/record/record.operations.js +11 -0
- package/dist/stresses/edges/stresses.record.d.ts +4 -0
- package/dist/stresses/edges/stresses.record.js +37 -0
- package/dist/stresses/stresses.operations.d.ts +28 -0
- package/dist/stresses/stresses.operations.js +22 -0
- package/dist/utils/ast.d.ts +55 -0
- package/dist/utils/ast.js +181 -0
- package/dist/utils/errors.d.ts +8 -0
- package/dist/utils/errors.js +12 -0
- package/dist/utils/faults.d.ts +6 -0
- package/dist/utils/faults.js +10 -0
- package/dist/utils/lookup.d.ts +4 -0
- package/dist/utils/lookup.js +27 -0
- package/dist/utils/order.d.ts +2 -0
- package/dist/utils/order.js +17 -0
- package/dist/utils/retry.d.ts +14 -0
- package/dist/utils/retry.js +15 -0
- package/dist/utils/safe.d.ts +11 -0
- package/dist/utils/safe.js +31 -0
- package/migrations/main/20261129123324_initial/migration.sql +418 -0
- package/migrations/main/20261129123324_initial/snapshot.json +3641 -0
- package/package.json +89 -4
- package/references/consumer-setup.md +92 -0
- package/src/graph/cfg/code-paths/LICENSE +21 -0
- package/src/graph/cfg/scopes/LICENSE +24 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type Node, type Parsed } from '../utils/ast.ts';
|
|
2
|
+
import type { Dedent, Edit } from './crush.ts';
|
|
3
|
+
type Place = {
|
|
4
|
+
parent: Node;
|
|
5
|
+
key: string;
|
|
6
|
+
};
|
|
7
|
+
export type Shape = {
|
|
8
|
+
source: string;
|
|
9
|
+
comments: Parsed['comments'];
|
|
10
|
+
placeOf: (node: Node) => Place;
|
|
11
|
+
commentEnds: ReadonlyMap<number, number>;
|
|
12
|
+
commentStarts: ReadonlyMap<number, number>;
|
|
13
|
+
};
|
|
14
|
+
export type Fold = {
|
|
15
|
+
edits: Edit[];
|
|
16
|
+
dedents: Dedent[];
|
|
17
|
+
};
|
|
18
|
+
type Span = {
|
|
19
|
+
start: number;
|
|
20
|
+
end: number;
|
|
21
|
+
};
|
|
22
|
+
export declare const shapeOf: (source: string, parsed: Parsed) => Shape;
|
|
23
|
+
export declare const indentEndPattern: RegExp;
|
|
24
|
+
export declare const indentAt: (source: string, offset: number) => string;
|
|
25
|
+
export declare const lineRestPattern: RegExp;
|
|
26
|
+
export declare const blankPattern: RegExp;
|
|
27
|
+
export declare const opensListed: (shape: Shape, node: Node) => boolean;
|
|
28
|
+
export declare const removed: (shape: Shape, node: Node) => Fold;
|
|
29
|
+
export type HeadInput = {
|
|
30
|
+
source: string;
|
|
31
|
+
comments: readonly Span[];
|
|
32
|
+
testEnd: number;
|
|
33
|
+
bodyStart: number;
|
|
34
|
+
};
|
|
35
|
+
export declare const headCloseOf: ({ source, comments, testEnd, bodyStart }: HeadInput) => number;
|
|
36
|
+
export declare const kept: (shape: Shape, node: Node, child: Node) => Fold;
|
|
37
|
+
export declare const spacePattern: RegExp;
|
|
38
|
+
export declare const opensBlockPattern: RegExp;
|
|
39
|
+
export declare const keptBranch: (shape: Shape, node: Extract<Node, {
|
|
40
|
+
type: 'ConditionalExpression';
|
|
41
|
+
}>, branch: Node) => Fold;
|
|
42
|
+
type Logical = Extract<Node, {
|
|
43
|
+
type: 'LogicalExpression';
|
|
44
|
+
}>;
|
|
45
|
+
export declare const sidesOf: (shape: Shape, node: Logical) => {
|
|
46
|
+
left: Span;
|
|
47
|
+
right: Span;
|
|
48
|
+
};
|
|
49
|
+
export declare const keptRight: (shape: Shape, node: Logical) => Fold;
|
|
50
|
+
export declare const keptLeft: (shape: Shape, node: Logical) => Fold;
|
|
51
|
+
export declare const jumpsIn: (shape: Shape, loop: Node, body: Node, kinds: ReadonlySet<string>) => boolean;
|
|
52
|
+
export declare const takesElse: (shape: Shape, node: Node) => boolean;
|
|
53
|
+
export declare const labeledStartOf: (shape: Shape, node: Node) => number;
|
|
54
|
+
export declare const wordPattern: RegExp;
|
|
55
|
+
export declare const flagNameOf: (source: string) => string;
|
|
56
|
+
export declare const removedClause: (shape: Shape, clause: Node) => Fold;
|
|
57
|
+
export declare const isOnlyLabeled: (shape: Shape, clause: Extract<Node, {
|
|
58
|
+
type: 'SwitchCase';
|
|
59
|
+
}>) => boolean;
|
|
60
|
+
export declare const declaresForOthers: (shape: Shape, clause: Extract<Node, {
|
|
61
|
+
type: 'SwitchCase';
|
|
62
|
+
}>) => boolean;
|
|
63
|
+
export declare const isFirstClause: (shape: Shape, clause: Node) => boolean;
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { entryAt, isInComment, lineStartOf, namesProperty, ofType, walk } from '../utils/ast.js';
|
|
2
|
+
export const shapeOf = (source, parsed) => {
|
|
3
|
+
const places = new Map();
|
|
4
|
+
walk(parsed.program, ({ node, parent, key }) => {
|
|
5
|
+
places.set(node, { parent, key });
|
|
6
|
+
});
|
|
7
|
+
return {
|
|
8
|
+
source,
|
|
9
|
+
comments: parsed.comments,
|
|
10
|
+
placeOf: (node) => entryAt(places, node, `the place of ${node.type} at ${node.start}`),
|
|
11
|
+
commentEnds: new Map(parsed.comments.map(({ start, end }) => [start, end])),
|
|
12
|
+
commentStarts: new Map(parsed.comments.map(({ start, end }) => [end, start])),
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
const cut = (span) => ({ start: span.start, end: span.end, text: '' });
|
|
16
|
+
// Matches the first character past a line's opening spaces and tabs, or the end of the text.
|
|
17
|
+
export const indentEndPattern = /[^ \t]|$/;
|
|
18
|
+
export const indentAt = (source, offset) => {
|
|
19
|
+
const line = source.slice(lineStartOf(source, offset));
|
|
20
|
+
return line.slice(0, line.search(indentEndPattern));
|
|
21
|
+
};
|
|
22
|
+
// Matches what may follow a statement on its line when nothing else shares it: spaces, one semicolon, and the line terminator.
|
|
23
|
+
export const lineRestPattern = /^[ \t]*;?[ \t]*(?:\r\n|[\n\r\u2028\u2029]|$)/;
|
|
24
|
+
// Matches a span of nothing but spaces and tabs.
|
|
25
|
+
export const blankPattern = /^[ \t]*$/;
|
|
26
|
+
// A statement's own lines, indentation and line break included, when it shares them with nothing; else its span.
|
|
27
|
+
const ownLinesOf = (source, span) => {
|
|
28
|
+
const start = lineStartOf(source, span.start);
|
|
29
|
+
const rest = lineRestPattern.exec(source.slice(span.end));
|
|
30
|
+
if (!rest || !blankPattern.test(source.slice(start, span.start)))
|
|
31
|
+
return span;
|
|
32
|
+
return { start, end: span.end + rest[0].length };
|
|
33
|
+
};
|
|
34
|
+
// The statements a node sits among, when it sits in a list of them.
|
|
35
|
+
const listIn = (parent) => {
|
|
36
|
+
if (parent.type === 'Program' || parent.type === 'BlockStatement' || parent.type === 'StaticBlock')
|
|
37
|
+
return parent.body;
|
|
38
|
+
return parent.type === 'SwitchCase' ? parent.consequent : null;
|
|
39
|
+
};
|
|
40
|
+
// The characters that, opening a line, continue the statement above it rather than start one of their own.
|
|
41
|
+
const continuing = new Set(['(', '[', '`', '+', '-', '/', '<']);
|
|
42
|
+
// The semicolon a fold writes before text it leaves opening a statement in a list, where that text would otherwise continue the line above.
|
|
43
|
+
const guardAt = (source, at) => (continuing.has(source.charAt(at)) ? ';' : '');
|
|
44
|
+
// Whether an expression opens a statement sitting in a list, where what a fold leaves of it starts that statement.
|
|
45
|
+
export const opensListed = (shape, node) => {
|
|
46
|
+
const { parent } = shape.placeOf(node);
|
|
47
|
+
return parent.start === node.start && listIn(shape.placeOf(parent).parent) !== null;
|
|
48
|
+
};
|
|
49
|
+
// A statement folded to nothing: gone from a list, gone with its `else` as an alternate, and an empty block where the grammar needs a statement.
|
|
50
|
+
export const removed = (shape, node) => {
|
|
51
|
+
const { parent, key } = shape.placeOf(node);
|
|
52
|
+
if (listIn(parent))
|
|
53
|
+
return { edits: [cut(ownLinesOf(shape.source, node))], dedents: [] };
|
|
54
|
+
if (parent.type === 'IfStatement' && key === 'alternate')
|
|
55
|
+
return { edits: [cut({ start: parent.consequent.end, end: node.end })], dedents: [] };
|
|
56
|
+
return { edits: [{ start: node.start, end: node.end, text: '{}' }], dedents: [] };
|
|
57
|
+
};
|
|
58
|
+
// Whether a statement declares a name its whole function sees from the top: a function it declares itself, or a `var` anywhere in it outside a function of its own.
|
|
59
|
+
const hoists = (statement) => {
|
|
60
|
+
let found = statement.type === 'FunctionDeclaration' || (statement.type === 'VariableDeclaration' && statement.kind === 'var');
|
|
61
|
+
walk(statement, ({ node, inFunction }) => {
|
|
62
|
+
if (!inFunction && node.type === 'VariableDeclaration' && node.kind === 'var')
|
|
63
|
+
found = true;
|
|
64
|
+
});
|
|
65
|
+
return found;
|
|
66
|
+
};
|
|
67
|
+
const exitTypes = new Set(['ReturnStatement', 'ThrowStatement', 'BreakStatement', 'ContinueStatement']);
|
|
68
|
+
// Where a statement's fold ends: past the statements after it when what it keeps always leaves, since nothing reaches them.
|
|
69
|
+
// A function or `var` declared after it is hoisted above the exit, where earlier code may read its name, so its followers stay.
|
|
70
|
+
const endAfter = (list, node, child) => {
|
|
71
|
+
const followers = list.slice(list.indexOf(node) + 1);
|
|
72
|
+
const last = followers.at(-1);
|
|
73
|
+
const final = child.type === 'BlockStatement' ? child.body.at(-1) : child;
|
|
74
|
+
if (!last || !final || !exitTypes.has(final.type) || followers.some(hoists))
|
|
75
|
+
return node.end;
|
|
76
|
+
return last.end;
|
|
77
|
+
};
|
|
78
|
+
const functionTypes = new Set(['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression', 'Program']);
|
|
79
|
+
// The names a list of statements declares for its own scope, and with `withVar` the names any `var` in them declares for the whole function.
|
|
80
|
+
const declaredBy = (statements, withVar) => {
|
|
81
|
+
const names = new Set();
|
|
82
|
+
const add = (declaration) => {
|
|
83
|
+
for (const { id } of declaration.declarations) {
|
|
84
|
+
if (id.type === 'Identifier')
|
|
85
|
+
names.add(id.name);
|
|
86
|
+
walk(id, ({ node, parent, key }) => {
|
|
87
|
+
if (node.type === 'Identifier' && key !== 'right' && !namesProperty(parent, key))
|
|
88
|
+
names.add(node.name);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
for (const statement of statements) {
|
|
93
|
+
if ((statement.type === 'ClassDeclaration' || statement.type === 'FunctionDeclaration') && statement.id)
|
|
94
|
+
names.add(statement.id.name);
|
|
95
|
+
if (statement.type === 'VariableDeclaration' && (withVar || statement.kind !== 'var'))
|
|
96
|
+
add(statement);
|
|
97
|
+
if (!withVar)
|
|
98
|
+
continue;
|
|
99
|
+
walk(statement, ({ node, inFunction }) => {
|
|
100
|
+
if (!inFunction && node.type === 'VariableDeclaration' && node.kind === 'var')
|
|
101
|
+
add(node);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return names;
|
|
105
|
+
};
|
|
106
|
+
// Whether any of the names is written in the function holding a span, outside that span.
|
|
107
|
+
const isWrittenOutside = (shape, within, names) => {
|
|
108
|
+
if (!names.size)
|
|
109
|
+
return false;
|
|
110
|
+
let scope = within;
|
|
111
|
+
while (!functionTypes.has(scope.type))
|
|
112
|
+
scope = shape.placeOf(scope).parent;
|
|
113
|
+
let isWritten = false;
|
|
114
|
+
walk(scope, ({ node }) => {
|
|
115
|
+
const isOutside = node.start < within.start || node.end > within.end;
|
|
116
|
+
if (isOutside && node.type === 'Identifier' && names.has(node.name))
|
|
117
|
+
isWritten = true;
|
|
118
|
+
});
|
|
119
|
+
return isWritten;
|
|
120
|
+
};
|
|
121
|
+
const resourceKinds = new Set(['using', 'await using']);
|
|
122
|
+
// A block keeps its braces when a name it declares is written anywhere else in its function, where unwrapping could clash with or capture it.
|
|
123
|
+
// One declaring a resource keeps them too, since its closing brace is where the resource is disposed.
|
|
124
|
+
const keepsBraces = (shape, block) => {
|
|
125
|
+
if (block.body.some((statement) => statement.type === 'VariableDeclaration' && resourceKinds.has(statement.kind)))
|
|
126
|
+
return true;
|
|
127
|
+
return isWrittenOutside(shape, block, declaredBy(block.body, false));
|
|
128
|
+
};
|
|
129
|
+
// The dedents moving a kept block's lines out, leaving alone each line that starts inside a template literal, whose indentation is part of its string.
|
|
130
|
+
const dedentsOf = (block, start, end, by) => {
|
|
131
|
+
const dedents = [];
|
|
132
|
+
let from = start;
|
|
133
|
+
walk(block, ({ node }) => {
|
|
134
|
+
// The walk meets templates in source order, so one starting before `from` sits inside a template already stepped over.
|
|
135
|
+
if (node.type !== 'TemplateLiteral' || node.start < from)
|
|
136
|
+
return;
|
|
137
|
+
dedents.push({ start: from, end: node.start, by });
|
|
138
|
+
from = node.end;
|
|
139
|
+
});
|
|
140
|
+
dedents.push({ start: from, end, by });
|
|
141
|
+
return dedents;
|
|
142
|
+
};
|
|
143
|
+
// Where a statement's head closes, just past the last `)` between its test and its body that no comment holds.
|
|
144
|
+
export const headCloseOf = ({ source, comments, testEnd, bodyStart }) => {
|
|
145
|
+
let close = testEnd;
|
|
146
|
+
for (let at = source.indexOf(')', testEnd); at !== -1 && at < bodyStart; at = source.indexOf(')', at + 1)) {
|
|
147
|
+
if (!isInComment(comments, at))
|
|
148
|
+
close = at + 1;
|
|
149
|
+
}
|
|
150
|
+
return close;
|
|
151
|
+
};
|
|
152
|
+
// A statement folded to one statement it holds, as an `if` to the branch it always takes.
|
|
153
|
+
// A block kept in a list loses its braces, its lines moving out one level, and what follows an exit goes with the fold.
|
|
154
|
+
// What it leaves in a list opens with a semicolon where it would otherwise continue the line above.
|
|
155
|
+
export const kept = (shape, node, child) => {
|
|
156
|
+
const { source } = shape;
|
|
157
|
+
const list = listIn(shape.placeOf(node).parent);
|
|
158
|
+
const end = list ? endAfter(list, node, child) : node.end;
|
|
159
|
+
if (child.type === 'BlockStatement' && list && !keepsBraces(shape, child)) {
|
|
160
|
+
const [first] = child.body;
|
|
161
|
+
const last = child.body.at(-1);
|
|
162
|
+
if (!first || !last)
|
|
163
|
+
return removed(shape, node);
|
|
164
|
+
const by = indentAt(source, first.start).length - indentAt(source, node.start).length;
|
|
165
|
+
return {
|
|
166
|
+
edits: [{ start: node.start, end: first.start, text: guardAt(source, first.start) }, cut({ start: last.end, end })],
|
|
167
|
+
dedents: by > 0 ? dedentsOf(child, first.start, last.end, by) : [],
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
return { edits: [{ start: node.start, end: child.start, text: list ? guardAt(source, child.start) : '' }, cut({ start: child.end, end })], dedents: [] };
|
|
171
|
+
};
|
|
172
|
+
// Matches a space, tab or line break.
|
|
173
|
+
export const spacePattern = /\s/;
|
|
174
|
+
// A node with the parentheses written around it, which its own span leaves out, looked for no further than the bounds its parent gives.
|
|
175
|
+
// A comment between the node and a parenthesis is stepped over, so a pair holding one is still found whole.
|
|
176
|
+
const outerOf = (shape, node, min, max) => {
|
|
177
|
+
const { source, commentEnds, commentStarts } = shape;
|
|
178
|
+
let start = node.start;
|
|
179
|
+
let opened = 0;
|
|
180
|
+
let at = node.start - 1;
|
|
181
|
+
while (at >= min) {
|
|
182
|
+
const commentStart = commentStarts.get(at + 1);
|
|
183
|
+
const char = source.charAt(at);
|
|
184
|
+
if (commentStart !== undefined)
|
|
185
|
+
at = commentStart - 1;
|
|
186
|
+
else if (char === '(') {
|
|
187
|
+
opened++;
|
|
188
|
+
start = at--;
|
|
189
|
+
}
|
|
190
|
+
else if (spacePattern.test(char))
|
|
191
|
+
at--;
|
|
192
|
+
else
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
let end = node.end;
|
|
196
|
+
let closed = 0;
|
|
197
|
+
at = node.end;
|
|
198
|
+
while (at < max && closed < opened) {
|
|
199
|
+
const commentEnd = commentEnds.get(at);
|
|
200
|
+
const char = source.charAt(at);
|
|
201
|
+
if (commentEnd !== undefined)
|
|
202
|
+
at = commentEnd;
|
|
203
|
+
else if (char === ')') {
|
|
204
|
+
closed++;
|
|
205
|
+
end = ++at;
|
|
206
|
+
}
|
|
207
|
+
else if (spacePattern.test(char))
|
|
208
|
+
at++;
|
|
209
|
+
else
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
return { start, end };
|
|
213
|
+
};
|
|
214
|
+
// Matches text that, opening a statement or an arrow's body, would read as a block, a function or a class declaration rather than an expression.
|
|
215
|
+
export const opensBlockPattern = /^(?:\{|function\b|class\b|async\s+function\b)/;
|
|
216
|
+
const typeWrappers = new Set(['TSNonNullExpression', 'TSAsExpression', 'TSSatisfiesExpression', 'TSTypeAssertion']);
|
|
217
|
+
// The wrappers that leave the member they hold reading as that member: an optional chain, and the type-only `!`, `as`, `satisfies` and `<T>`.
|
|
218
|
+
const memberWrappers = new Set(['ChainExpression', ...typeWrappers]);
|
|
219
|
+
const readsMember = (node) => {
|
|
220
|
+
let inner = node;
|
|
221
|
+
while (memberWrappers.has(inner.type) && 'expression' in inner && typeof inner.expression === 'object' && inner.expression)
|
|
222
|
+
inner = inner.expression;
|
|
223
|
+
return inner.type === 'MemberExpression';
|
|
224
|
+
};
|
|
225
|
+
// The comma that keeps a kept member from binding `this` or deleting its property where the expression it replaces is called, tagged or deleted, through any type-only wrapper, as `(0, o.f)!()`.
|
|
226
|
+
const unbindOf = (shape, node, kept) => {
|
|
227
|
+
let place = shape.placeOf(node);
|
|
228
|
+
while (typeWrappers.has(place.parent.type))
|
|
229
|
+
place = shape.placeOf(place.parent);
|
|
230
|
+
const { parent, key } = place;
|
|
231
|
+
const isBinding = (parent.type === 'CallExpression' && key === 'callee') || parent.type === 'TaggedTemplateExpression' || (parent.type === 'UnaryExpression' && parent.operator === 'delete');
|
|
232
|
+
return isBinding && readsMember(kept) ? '0, ' : '';
|
|
233
|
+
};
|
|
234
|
+
// A ternary folded to the branch it always takes.
|
|
235
|
+
// A ternary stands bare only where any expression may, so its branch needs parentheses only where it would open a statement or an arrow's body as a block, function or class.
|
|
236
|
+
export const keptBranch = (shape, node, branch) => {
|
|
237
|
+
const { parent } = shape.placeOf(node);
|
|
238
|
+
const outer = outerOf(shape, branch, node.start, node.end);
|
|
239
|
+
const opensStatement = parent.type === 'ExpressionStatement' || parent.type === 'ArrowFunctionExpression';
|
|
240
|
+
const isBare = outer.start === branch.start && opensStatement && opensBlockPattern.test(shape.source.slice(branch.start));
|
|
241
|
+
const grouping = groupingOf(shape, node, branch);
|
|
242
|
+
const guard = opensListed(shape, node) ? (isBare ? ';' : guardAt(shape.source, outer.start)) : '';
|
|
243
|
+
const unbind = unbindOf(shape, node, branch);
|
|
244
|
+
return {
|
|
245
|
+
edits: [
|
|
246
|
+
{ start: grouping ? grouping.start : node.start, end: outer.start, text: `${guard}${isBare ? '(' : ''}${unbind}` },
|
|
247
|
+
{ start: outer.end, end: grouping ? grouping.end : node.end, text: isBare ? ')' : '' },
|
|
248
|
+
],
|
|
249
|
+
dedents: [],
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
// Each side of a logical expression with its own parentheses.
|
|
253
|
+
export const sidesOf = (shape, node) => ({
|
|
254
|
+
left: outerOf(shape, node.left, node.start, node.right.start),
|
|
255
|
+
right: outerOf(shape, node.right, node.left.end, node.end),
|
|
256
|
+
});
|
|
257
|
+
// A logical expression folded to its right side, its left and its operator gone.
|
|
258
|
+
export const keptRight = (shape, node) => {
|
|
259
|
+
const grouping = groupingOf(shape, node, node.right);
|
|
260
|
+
const { right } = sidesOf(shape, node);
|
|
261
|
+
if (!grouping)
|
|
262
|
+
return { edits: [{ start: node.start, end: right.start, text: `${opensListed(shape, node) ? guardAt(shape.source, right.start) : ''}${unbindOf(shape, node, node.right)}` }], dedents: [] };
|
|
263
|
+
return { edits: [cut({ start: grouping.start, end: right.start }), cut({ start: right.end, end: grouping.end })], dedents: [] };
|
|
264
|
+
};
|
|
265
|
+
// A logical expression folded to its left side, its operator and its right gone.
|
|
266
|
+
export const keptLeft = (shape, node) => {
|
|
267
|
+
const grouping = groupingOf(shape, node, node.left);
|
|
268
|
+
const { left } = sidesOf(shape, node);
|
|
269
|
+
if (grouping)
|
|
270
|
+
return { edits: [cut({ start: grouping.start, end: left.start }), cut({ start: left.end, end: grouping.end })], dedents: [] };
|
|
271
|
+
const edits = [cut({ start: left.end, end: node.end })];
|
|
272
|
+
const unbind = unbindOf(shape, node, node.left);
|
|
273
|
+
if (unbind)
|
|
274
|
+
edits.push({ start: left.start, end: left.start, text: unbind });
|
|
275
|
+
return { edits, dedents: [] };
|
|
276
|
+
};
|
|
277
|
+
const loopTypes = new Set(['ForStatement', 'ForInStatement', 'ForOfStatement', 'WhileStatement', 'DoWhileStatement']);
|
|
278
|
+
// The labels written on a statement, each a `LabeledStatement` around it.
|
|
279
|
+
const labelsOf = (shape, node) => {
|
|
280
|
+
const labels = new Set();
|
|
281
|
+
for (let { parent } = shape.placeOf(node); parent.type === 'LabeledStatement'; { parent } = shape.placeOf(parent))
|
|
282
|
+
labels.add(parent.label.name);
|
|
283
|
+
return labels;
|
|
284
|
+
};
|
|
285
|
+
// Whether a loop's body holds a `break` or `continue` of the kinds named that acts on this loop: one naming its label, or a bare one no loop inside it holds, nor a switch for a `break`.
|
|
286
|
+
export const jumpsIn = (shape, loop, body, kinds) => {
|
|
287
|
+
const labels = labelsOf(shape, loop);
|
|
288
|
+
const acts = (node, ancestors) => {
|
|
289
|
+
if ((node.type !== 'BreakStatement' && node.type !== 'ContinueStatement') || !kinds.has(node.type))
|
|
290
|
+
return false;
|
|
291
|
+
if (node.label)
|
|
292
|
+
return labels.has(node.label.name);
|
|
293
|
+
return !ancestors.some((ancestor) => loopTypes.has(ancestor.type) || (node.type === 'BreakStatement' && ancestor.type === 'SwitchStatement'));
|
|
294
|
+
};
|
|
295
|
+
let found = acts(body, []);
|
|
296
|
+
walk(body, ({ node, ancestors, inFunction }) => {
|
|
297
|
+
if (!found && !inFunction && acts(node, ancestors))
|
|
298
|
+
found = true;
|
|
299
|
+
});
|
|
300
|
+
return found;
|
|
301
|
+
};
|
|
302
|
+
// Whether an `if` with no `else`, put where a statement stands, would take the `else` written after it for another `if`.
|
|
303
|
+
// It does when the statement ends the consequent of an `if` that has an `else`, however many loops, labels and `if`s without one lie between.
|
|
304
|
+
export const takesElse = (shape, node) => {
|
|
305
|
+
let at = node;
|
|
306
|
+
let { parent, key } = shape.placeOf(at);
|
|
307
|
+
while ((parent.type === 'IfStatement' && !(key === 'consequent' && parent.alternate)) || (key === 'body' && (parent.type === 'LabeledStatement' || (loopTypes.has(parent.type) && parent.type !== 'DoWhileStatement')))) {
|
|
308
|
+
at = parent;
|
|
309
|
+
const place = shape.placeOf(at);
|
|
310
|
+
parent = place.parent;
|
|
311
|
+
key = place.key;
|
|
312
|
+
}
|
|
313
|
+
return parent.type === 'IfStatement';
|
|
314
|
+
};
|
|
315
|
+
// Where a statement starts with every label written on it, so a block put around it keeps `continue label` naming the loop.
|
|
316
|
+
export const labeledStartOf = (shape, node) => {
|
|
317
|
+
let start = node.start;
|
|
318
|
+
for (let { parent } = shape.placeOf(node); parent.type === 'LabeledStatement'; { parent } = shape.placeOf(parent))
|
|
319
|
+
start = parent.start;
|
|
320
|
+
return start;
|
|
321
|
+
};
|
|
322
|
+
// Matches a word the file writes, as a name or a key.
|
|
323
|
+
export const wordPattern = /[A-Za-z_$][\w$]*/g;
|
|
324
|
+
// A name for a loop's flag that the file never writes, `once` unless it does, then `once2`, `once3` and on.
|
|
325
|
+
export const flagNameOf = (source) => {
|
|
326
|
+
const words = new Set(source.match(wordPattern));
|
|
327
|
+
let name = 'once';
|
|
328
|
+
for (let count = 2; words.has(name); count++)
|
|
329
|
+
name = `once${count}`;
|
|
330
|
+
return name;
|
|
331
|
+
};
|
|
332
|
+
// The places where parentheses around an expression only group it, never belonging to the syntax, as an `if`'s test or a call's arguments do.
|
|
333
|
+
const groupingKeys = new Map([
|
|
334
|
+
['VariableDeclarator', 'init'],
|
|
335
|
+
['ReturnStatement', 'argument'],
|
|
336
|
+
['AssignmentExpression', 'right'],
|
|
337
|
+
['Property', 'value'],
|
|
338
|
+
['ArrayExpression', 'elements'],
|
|
339
|
+
['TemplateLiteral', 'expressions'],
|
|
340
|
+
]);
|
|
341
|
+
// What can stand where its parentheses stood with nothing changed: a literal, a name, `this`, a template or an array.
|
|
342
|
+
const plainTypes = new Set(['Literal', 'Identifier', 'ThisExpression', 'TemplateLiteral', 'ArrayExpression']);
|
|
343
|
+
// The parentheses around a folded expression, to drop with it where they could only group it and what stays is plain, as `(n < 0 ? 'a' : 'b')` folds to `'b'`.
|
|
344
|
+
const groupingOf = (shape, node, stays) => {
|
|
345
|
+
const { parent, key } = shape.placeOf(node);
|
|
346
|
+
const isGrouping = groupingKeys.get(parent.type) === key || parent.type === 'BinaryExpression' || parent.type === 'ArrowFunctionExpression';
|
|
347
|
+
if (!isGrouping || !plainTypes.has(stays.type))
|
|
348
|
+
return null;
|
|
349
|
+
const outer = outerOf(shape, node, parent.start, parent.end);
|
|
350
|
+
return outer.start === node.start ? null : outer;
|
|
351
|
+
};
|
|
352
|
+
// A switch clause folded to nothing, its own lines and all, when nothing can reach it once its label is gone.
|
|
353
|
+
export const removedClause = (shape, clause) => ({ edits: [cut(ownLinesOf(shape.source, clause))], dedents: [] });
|
|
354
|
+
// Whether a clause can only be reached by its own label: it is the first, or the clause before it always leaves, so nothing falls into it.
|
|
355
|
+
export const isOnlyLabeled = (shape, clause) => {
|
|
356
|
+
const { cases } = ofType(shape.placeOf(clause).parent, 'SwitchStatement');
|
|
357
|
+
const before = cases[cases.indexOf(clause) - 1];
|
|
358
|
+
if (!before)
|
|
359
|
+
return true;
|
|
360
|
+
const last = before.consequent.at(-1);
|
|
361
|
+
return last !== undefined && exitTypes.has(last.type);
|
|
362
|
+
};
|
|
363
|
+
// Whether a clause declares a name written outside it, which its statements must stay to declare.
|
|
364
|
+
export const declaresForOthers = (shape, clause) => isWrittenOutside(shape, clause, declaredBy(clause.consequent, true));
|
|
365
|
+
// Whether a clause is its switch's first, with no clause before it to take its statements once its label is gone.
|
|
366
|
+
export const isFirstClause = (shape, clause) => ofType(shape.placeOf(clause).parent, 'SwitchStatement').cases[0] === clause;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ofType, tokenAt } from '../utils/ast.js';
|
|
2
|
+
import { forksFor, replaced } from './crush.js';
|
|
3
|
+
import { kept, keptBranch, keptLeft, keptRight, opensListed, removed, sidesOf } from './fold.js';
|
|
4
|
+
import { readsPlainly } from '../decisions/decide.js';
|
|
5
|
+
const testKinds = new Set(['if', 'ternary', 'while', 'doWhile', 'for']);
|
|
6
|
+
const writesOnTrue = new Set(['&&', '&&=']);
|
|
7
|
+
// What a test always answering one value leaves of its statement or expression: the branch it takes, or nothing.
|
|
8
|
+
// A loop always passing keeps its shape with `true` for its test, and a `for` or `do` never passing keeps its head or its one pass, so each of those only has its test replaced.
|
|
9
|
+
const testFold = (shape, decision, value) => {
|
|
10
|
+
const { node } = decision;
|
|
11
|
+
if (node.type === 'IfStatement') {
|
|
12
|
+
const branch = value ? node.consequent : node.alternate;
|
|
13
|
+
return branch ? kept(shape, node, branch) : removed(shape, node);
|
|
14
|
+
}
|
|
15
|
+
if (node.type === 'ConditionalExpression')
|
|
16
|
+
return keptBranch(shape, node, value ? node.consequent : node.alternate);
|
|
17
|
+
return node.type === 'WhileStatement' && !value ? removed(shape, node) : null;
|
|
18
|
+
};
|
|
19
|
+
// What a short-circuit leaves once it always runs its right side: the right alone after a left that only reads, else both in sequence.
|
|
20
|
+
const runsRight = (shape, decision) => {
|
|
21
|
+
const node = ofType(decision.node, 'LogicalExpression');
|
|
22
|
+
if (readsPlainly(node.left) || node.left.type === 'Literal')
|
|
23
|
+
return keptRight(shape, node);
|
|
24
|
+
const { left, right } = sidesOf(shape, node);
|
|
25
|
+
// A sequence left as a statement in a list opens with a semicolon, so its parenthesis never calls the line above.
|
|
26
|
+
return {
|
|
27
|
+
edits: [
|
|
28
|
+
{ start: node.start, end: node.start, text: opensListed(shape, node) ? ';(' : '(' },
|
|
29
|
+
{ start: left.end, end: right.start, text: ', ' },
|
|
30
|
+
{ start: node.end, end: node.end, text: ')' },
|
|
31
|
+
],
|
|
32
|
+
dedents: [],
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
// What a logical assignment leaves once it never writes, its target alone, or nothing where that is a whole statement reading a name; and once it always writes, a plain assignment.
|
|
36
|
+
const assignFold = (shape, decision, writes) => {
|
|
37
|
+
const node = ofType(decision.node, 'AssignmentExpression');
|
|
38
|
+
const operatorAt = tokenAt({ source: shape.source, comments: shape.comments, token: node.operator, from: node.left.end, to: node.right.start });
|
|
39
|
+
if (writes)
|
|
40
|
+
return { edits: [{ start: operatorAt, end: operatorAt + node.operator.length, text: '=' }], dedents: [] };
|
|
41
|
+
const { parent } = shape.placeOf(node);
|
|
42
|
+
if (parent.type === 'ExpressionStatement' && readsPlainly(node.left))
|
|
43
|
+
return removed(shape, parent);
|
|
44
|
+
return { edits: [{ start: node.left.end, end: node.end, text: '' }], dedents: [] };
|
|
45
|
+
};
|
|
46
|
+
// Forces a decision to one side, removing the other, and folds what the forced decision leaves.
|
|
47
|
+
// A test is forced to the opposite value, a test already written as that value removing nothing.
|
|
48
|
+
// A short-circuit keeps its left value: removing the side that runs its right leaves the left alone, and removing the other always runs the right after it.
|
|
49
|
+
// A logical assignment removing the side that writes never writes, and removing the other always writes, which settles `??` and `??=` by presence as `&&` and `||` by truth.
|
|
50
|
+
export const forceFork = {
|
|
51
|
+
name: 'force-fork',
|
|
52
|
+
decisionKinds: ['if', 'ternary', 'while', 'doWhile', 'for', 'short', 'logicalAssign'],
|
|
53
|
+
sitesIn: ({ source, forks, shape }) => {
|
|
54
|
+
return forksFor(forceFork, forks).flatMap((decision) => {
|
|
55
|
+
const { probe } = decision;
|
|
56
|
+
if (testKinds.has(decision.kind)) {
|
|
57
|
+
const written = source.slice(probe.start, probe.end);
|
|
58
|
+
return [true, false].flatMap((value) => {
|
|
59
|
+
if (written === String(value))
|
|
60
|
+
return [];
|
|
61
|
+
const site = replaced(decision, String(!value), value, probe, String(value));
|
|
62
|
+
return [{ ...site, ...testFold(shape, decision, value) }];
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
// The true side is the left holding; under && and &&= it runs the right, under the rest it keeps the left.
|
|
66
|
+
const rightOnTrue = writesOnTrue.has(probe.operator);
|
|
67
|
+
return [true, false].map((value) => {
|
|
68
|
+
const takesRight = value === rightOnTrue;
|
|
69
|
+
if (decision.kind === 'logicalAssign')
|
|
70
|
+
return Object.assign({ decision: decision.id, start: probe.start, end: probe.end, side: String(!value), forced: value }, assignFold(shape, decision, takesRight));
|
|
71
|
+
// A short-circuit's site spans what its removal drops, so its question names it: the check on the left when the right always runs, the right when it never does.
|
|
72
|
+
const node = ofType(decision.node, 'LogicalExpression');
|
|
73
|
+
const dropped = takesRight ? node.left : node.right;
|
|
74
|
+
const fold = takesRight ? runsRight(shape, decision) : keptLeft(shape, node);
|
|
75
|
+
return Object.assign({ decision: decision.id, start: dropped.start, end: dropped.end, side: String(!value), forced: value }, fold);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { lineStartOf, ofType, tokenAt } from '../utils/ast.js';
|
|
2
|
+
import { forksFor, replaced } from './crush.js';
|
|
3
|
+
import { blankPattern, flagNameOf, headCloseOf, indentAt, jumpsIn, labeledStartOf, takesElse } from './fold.js';
|
|
4
|
+
const jumps = new Set(['BreakStatement', 'ContinueStatement']);
|
|
5
|
+
const continues = new Set(['ContinueStatement']);
|
|
6
|
+
const firstItem = 'function* (items) { for (const item of items) { yield item; return } }';
|
|
7
|
+
const firstKey = 'function (object) { for (const key in object) return { __proto__: null, [key]: 0 }; return { __proto__: null } }';
|
|
8
|
+
// Runs a loop at most once, keeping every break, continue and label it holds working.
|
|
9
|
+
// A while holding no break or continue of its own becomes an if; one holding either, or standing where an if would take an else written after it, becomes a for whose flag fails its test from the second pass on.
|
|
10
|
+
// A for keeps its head inside a block declaring the flag, its test failing from the second pass on, which a do...while never needs, since forcing its test false already runs it once.
|
|
11
|
+
// The flag takes a name the file never writes, so it shadows nothing of the program's own.
|
|
12
|
+
// A for...of or for...in whose braced body holds no continue of its own ends its body with a break.
|
|
13
|
+
// One that does walks a generator yielding its first item and closing the iterator, or an object holding only the first key it would see.
|
|
14
|
+
export const loopOnce = {
|
|
15
|
+
name: 'loop-once',
|
|
16
|
+
decisionKinds: ['while', 'for', 'forOf', 'forIn'],
|
|
17
|
+
sitesIn: ({ source, forks, shape }) => {
|
|
18
|
+
return forksFor(loopOnce, forks).map((decision) => {
|
|
19
|
+
const { node, probe } = decision;
|
|
20
|
+
const siteOf = (edits) => ({ decision: decision.id, start: probe.start, end: probe.end, side: 'repeat', forced: 'once', edits, dedents: [] });
|
|
21
|
+
const once = flagNameOf(source);
|
|
22
|
+
if (node.type === 'WhileStatement') {
|
|
23
|
+
if (!jumpsIn(shape, node, node.body, jumps) && !takesElse(shape, node))
|
|
24
|
+
return siteOf([{ start: node.start, end: node.start + 'while'.length, text: 'if' }]);
|
|
25
|
+
const close = headCloseOf({ source, comments: shape.comments, testEnd: probe.end, bodyStart: node.body.start });
|
|
26
|
+
return siteOf([
|
|
27
|
+
{ start: node.start, end: probe.start, text: `for (let ${once} = true; ${once} && (` },
|
|
28
|
+
{ start: probe.end, end: close, text: `); ${once} = false)` },
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
if (node.type === 'ForStatement') {
|
|
32
|
+
const start = labeledStartOf(shape, node);
|
|
33
|
+
// With no update of its own, the flag takes the place between the head's last `;` and its `)`.
|
|
34
|
+
const semicolon = tokenAt({ source, comments: shape.comments, token: ';', from: probe.end, to: node.body.start }) + 1;
|
|
35
|
+
const update = node.update
|
|
36
|
+
? { start: node.update.start, end: node.update.start, text: `${once} = false, ` }
|
|
37
|
+
: { start: semicolon, end: headCloseOf({ source, comments: shape.comments, testEnd: semicolon, bodyStart: node.body.start }) - 1, text: ` ${once} = false` };
|
|
38
|
+
return siteOf([
|
|
39
|
+
{ start, end: start, text: `{ let ${once} = true; ` },
|
|
40
|
+
{ start: probe.start, end: probe.start, text: `${once} && (` },
|
|
41
|
+
{ start: probe.end, end: probe.end, text: ')' },
|
|
42
|
+
update,
|
|
43
|
+
{ start: node.end, end: node.end, text: ' }' },
|
|
44
|
+
]);
|
|
45
|
+
}
|
|
46
|
+
const { body } = node.type === 'ForOfStatement' ? node : ofType(node, 'ForInStatement');
|
|
47
|
+
if (body.type === 'BlockStatement' && !jumpsIn(shape, node, body, continues)) {
|
|
48
|
+
const last = body.body.at(-1);
|
|
49
|
+
const closeLine = lineStartOf(source, body.end - 1);
|
|
50
|
+
// A body whose closing brace has a line to itself takes the break as a last line, ended by the line terminator before it, and any other takes it after its last statement.
|
|
51
|
+
const terminator = source.slice(closeLine - 2, closeLine) === '\r\n' ? '\r\n' : source.charAt(closeLine - 1);
|
|
52
|
+
if (closeLine > body.start && blankPattern.test(source.slice(closeLine, body.end - 1))) {
|
|
53
|
+
return siteOf([{ start: closeLine, end: closeLine, text: `${last ? indentAt(source, last.start) : `${indentAt(source, body.end)} `}break${terminator}` }]);
|
|
54
|
+
}
|
|
55
|
+
const at = last ? last.end : body.start + 1;
|
|
56
|
+
return siteOf([{ start: at, end: at, text: last ? `${source.charAt(at - 1) === ';' ? '' : ';'} break` : ' break ' }]);
|
|
57
|
+
}
|
|
58
|
+
// A body of one statement that is no continue takes braces with the break after it.
|
|
59
|
+
if (body.type !== 'BlockStatement' && body.type !== 'ContinueStatement' && !jumpsIn(shape, node, body, continues)) {
|
|
60
|
+
return siteOf([
|
|
61
|
+
{ start: body.start, end: body.start, text: '{ ' },
|
|
62
|
+
{ start: body.end, end: body.end, text: `${source.charAt(body.end - 1) === ';' ? '' : ';'} break }` },
|
|
63
|
+
]);
|
|
64
|
+
}
|
|
65
|
+
const iterable = source.slice(probe.start, probe.end);
|
|
66
|
+
const wrapper = decision.kind === 'forOf' ? firstItem : firstKey;
|
|
67
|
+
const firstOnly = `(${wrapper})((${iterable}))`;
|
|
68
|
+
return replaced(decision, 'repeat', 'once', probe, firstOnly);
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { readsPlainly } from '../decisions/decide.js';
|
|
2
|
+
import { present, tokenAt, walk } from '../utils/ast.js';
|
|
3
|
+
import { forksFor, replaced } from './crush.js';
|
|
4
|
+
import { opensListed } from './fold.js';
|
|
5
|
+
// Forces one optional link present, its `?.` read as a plain link, or absent, the whole chain it short-circuits read as undefined.
|
|
6
|
+
// An absent link's object still runs, as judging runs it, so one that does more than read stays behind a `void`.
|
|
7
|
+
const sitesIn = ({ source, parsed, forks, shape }) => {
|
|
8
|
+
const chains = [];
|
|
9
|
+
walk(parsed.program, ({ node }) => {
|
|
10
|
+
if (node.type === 'ChainExpression')
|
|
11
|
+
chains.push(node);
|
|
12
|
+
});
|
|
13
|
+
return forksFor({ decisionKinds: ['optional'] }, forks).flatMap((decision) => {
|
|
14
|
+
const { node } = decision;
|
|
15
|
+
if (node.type !== 'MemberExpression' && node.type !== 'CallExpression')
|
|
16
|
+
return [];
|
|
17
|
+
const object = node.type === 'MemberExpression' ? node.object : node.callee;
|
|
18
|
+
const at = tokenAt({ source, comments: shape.comments, token: '?.', from: object.end, to: node.end });
|
|
19
|
+
const after = source[at + 2];
|
|
20
|
+
const plain = after === '(' || after === '[' ? '' : '.';
|
|
21
|
+
// The innermost chain holding the link is what an absent object short-circuits to undefined.
|
|
22
|
+
// The walk lists chains by where they start, and none starts between the link's own chain and the link, so the last one starting at or before it holds it.
|
|
23
|
+
const chain = present(chains.findLast((each) => each.start <= node.start));
|
|
24
|
+
const voided = `void ${source.slice(object.start, object.end)}`;
|
|
25
|
+
// A `void` opening a statement stands bare, and anywhere else takes parentheses, which keep it whole beside any operator.
|
|
26
|
+
const absent = readsPlainly(object) ? 'undefined' : opensListed(shape, chain) ? voided : `(${voided})`;
|
|
27
|
+
return [replaced(decision, 'absent', false, { start: at, end: at + 2 }, plain), replaced(decision, 'present', true, chain, absent)];
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
export const optionalChain = { name: 'optional-chain', decisionKinds: ['optional'], sitesIn };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ofType, tokenAt } from '../utils/ast.js';
|
|
2
|
+
import { forksFor, replaced } from './crush.js';
|
|
3
|
+
import { declaresForOthers, isFirstClause, isOnlyLabeled, removedClause } from './fold.js';
|
|
4
|
+
// Drops one case's label, or its written default's: a clause only its label reaches goes whole, and one an earlier clause falls into keeps its statements, so that fall still runs them.
|
|
5
|
+
// A clause declaring a name written outside it keeps its statements too, and as the first clause, with none before it to take them, it is left standing.
|
|
6
|
+
export const switchCase = {
|
|
7
|
+
name: 'switch-case',
|
|
8
|
+
decisionKinds: ['case', 'switchDefault'],
|
|
9
|
+
sitesIn: ({ source, forks, shape }) => {
|
|
10
|
+
return forksFor(switchCase, forks).flatMap((decision) => {
|
|
11
|
+
const node = ofType(decision.node, 'SwitchCase');
|
|
12
|
+
const end = tokenAt({ source, comments: shape.comments, token: ':', from: node.test ? node.test.end : node.start, to: node.end }) + 1;
|
|
13
|
+
const site = replaced(decision, decision.kind === 'case' ? 'case' : 'default', false, { start: node.start, end }, '');
|
|
14
|
+
if (isOnlyLabeled(shape, node) && !declaresForOthers(shape, node))
|
|
15
|
+
return [{ ...site, ...removedClause(shape, node) }];
|
|
16
|
+
return isFirstClause(shape, node) ? [] : [site];
|
|
17
|
+
});
|
|
18
|
+
},
|
|
19
|
+
};
|