targeted-mutation 0.1.0 → 0.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "targeted-mutation",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Find the tests a JavaScript or TypeScript suite is missing by trying small, targeted source edits against it.",
5
5
  "keywords": [
6
6
  "mutation-testing",
@@ -43,9 +43,9 @@
43
43
  "NOTICE"
44
44
  ],
45
45
  "optionalDependencies": {
46
- "@targeted-mutation/tmt-darwin-arm64": "0.1.0",
47
- "@targeted-mutation/tmt-darwin-x64": "0.1.0",
48
- "@targeted-mutation/tmt-linux-arm64": "0.1.0",
49
- "@targeted-mutation/tmt-linux-x64": "0.1.0"
46
+ "@targeted-mutation/tmt-darwin-arm64": "0.1.1",
47
+ "@targeted-mutation/tmt-darwin-x64": "0.1.1",
48
+ "@targeted-mutation/tmt-linux-arm64": "0.1.1",
49
+ "@targeted-mutation/tmt-linux-x64": "0.1.1"
50
50
  }
51
51
  }
@@ -136,7 +136,7 @@ export async function analyzeWitnesses(project, files, options) {
136
136
  snapshots.set(file, source);
137
137
  const rules = discoverWitnessRules(tree, constraints.filter(entry => entry.file === file));
138
138
  for (const rule of rules) for (const entry of rule.constraints ?? []) boundConstraints.add(entry);
139
- sources.push(discoverySources(tree, selected));
139
+ sources.push(discoverySources(tree, selected, file));
140
140
  extraction.push({ file, path: tree.parseDiagnostics.length ? 'diagnostic' : 'typescript' });
141
141
  for (const rule of rules) {
142
142
  const start = rule.node.getStart(tree);
@@ -23,14 +23,14 @@ function construct(node, name, count) {
23
23
  return !!node && ts.isNewExpression(node) && named(node.expression, name) && (node.arguments?.length ?? 0) === count;
24
24
  }
25
25
 
26
- function sourceEdit(tree, node, replacement, start = node.getStart(tree)) {
26
+ function sourceEdit(tree, file, node, replacement, start = node.getStart(tree)) {
27
27
  const original = tree.text.slice(start, node.getEnd());
28
28
  if (/[\r\n\u2028\u2029]/.test(original)) return null;
29
29
  const { line } = tree.getLineAndCharacterOfPosition(start);
30
- return { file: tree.fileName, line: line + 1, start, original, replacement };
30
+ return { file, line: line + 1, start, original, replacement };
31
31
  }
32
32
 
33
- function endianEndpoint(tree, name, fn) {
33
+ function endianEndpoint(tree, file, name, fn) {
34
34
  if (!fn.body || !ts.isBlock(fn.body) || fn.parameters.length !== 1 ||
35
35
  !named(fn.parameters[0].name) || fn.parameters[0].initializer || fn.parameters[0].dotDotDotToken ||
36
36
  fn.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.AsyncKeyword) || fn.asteriskToken) return null;
@@ -51,7 +51,7 @@ function endianEndpoint(tree, name, fn) {
51
51
  if (![ts.SyntaxKind.TrueKeyword, ts.SyntaxKind.FalseKeyword].includes(flag.kind)) return null;
52
52
  const little = flag.kind === ts.SyntaxKind.TrueKeyword;
53
53
  const start = method.getStart(tree);
54
- const edit = sourceEdit(tree, call, tree.text.slice(start, flag.getStart(tree)) +
54
+ const edit = sourceEdit(tree, file, call, tree.text.slice(start, flag.getStart(tree)) +
55
55
  String(!little) + tree.text.slice(flag.getEnd(), call.getEnd()), start);
56
56
  return edit && { name, kind: producer ? 'encoder' : 'decoder', bits: Number(bits), little, edit };
57
57
  }
@@ -62,7 +62,7 @@ function methodName(node) {
62
62
  ts.SyntaxKind.AsyncKeyword, ts.SyntaxKind.AbstractKeyword].includes(modifier.kind)) ? node.name.text : null;
63
63
  }
64
64
 
65
- function storeEndpoint(tree, node) {
65
+ function storeEndpoint(tree, file, node) {
66
66
  if (!ts.isClassDeclaration(node) || !node.name || !modifiers(node, ts.SyntaxKind.ExportKeyword) ||
67
67
  modifiers(node, ts.SyntaxKind.DefaultKeyword) || node.heritageClauses?.length ||
68
68
  node.members.some(member => ts.isConstructorDeclaration(member))) return null;
@@ -82,21 +82,21 @@ function storeEndpoint(tree, node) {
82
82
  const check = bare(first.expression);
83
83
  if (!invoked(check) || !property(check.expression) || check.expression.name.text !== 'some' ||
84
84
  !named(check.expression.expression, parameter) || check.arguments.length !== 1) continue;
85
- const edit = sourceEdit(tree, first, '');
85
+ const edit = sourceEdit(tree, file, first, '');
86
86
  if (edit) writes.set(name, edit);
87
87
  }
88
- return writes.size && reads.size ? { file: tree.fileName, name: node.name.text, writes, reads } : null;
88
+ return writes.size && reads.size ? { file, name: node.name.text, writes, reads } : null;
89
89
  }
90
90
 
91
- export function discoverySources(tree, selected) {
91
+ export function discoverySources(tree, selected, file = tree.fileName) {
92
92
  if (tree.parseDiagnostics.length) return { endpoints: [], stores: [] };
93
93
  const endpoints = [], stores = [];
94
94
  for (const statement of tree.statements) {
95
95
  for (const [name, fn] of functionParts(statement)) {
96
- const endpoint = endianEndpoint(tree, name.text, fn);
96
+ const endpoint = endianEndpoint(tree, file, name.text, fn);
97
97
  if (endpoint && selected(endpoint.edit.line)) endpoints.push(endpoint);
98
98
  }
99
- const store = storeEndpoint(tree, statement);
99
+ const store = storeEndpoint(tree, file, statement);
100
100
  if (store) {
101
101
  for (const [name, edit] of store.writes) if (!selected(edit.line)) store.writes.delete(name);
102
102
  if (store.writes.size) stores.push(store);
@@ -55,17 +55,18 @@ export function importMatches(file, specifier, subject, project) {
55
55
  const root = project ?? '.';
56
56
  const path = resolve(root, dirname(file), specifier);
57
57
  const source = resolve(root, subject);
58
- if (project && statSync(path, { throwIfNoEntry: false })?.isFile()) return realpathSync(path) === source;
58
+ const canonicalSource = project && statSync(source, { throwIfNoEntry: false })?.isFile() ? realpathSync(source) : source;
59
+ if (project && statSync(path, { throwIfNoEntry: false })?.isFile()) return realpathSync(path) === canonicalSource;
59
60
  const extension = extname(path);
60
61
  const substitutes = { '.js': ['.ts', '.tsx'], '.jsx': ['.tsx'], '.mjs': ['.mts'], '.cjs': ['.cts'] };
61
62
  const candidates = Object.hasOwn(substitutes, extension)
62
63
  ? substitutes[extension].map(ext => path.slice(0, -extension.length) + ext)
63
64
  : moduleExtensions.includes(extension) ? []
64
65
  : [...moduleExtensions.map(ext => path + ext), ...moduleExtensions.map(ext => resolve(path, 'index' + ext))];
65
- if (!project) return source === path || candidates.includes(source);
66
+ if (!project) return canonicalSource === path || candidates.includes(canonicalSource);
66
67
  const existing = new Set(candidates.filter(candidate => statSync(candidate, { throwIfNoEntry: false })?.isFile())
67
68
  .map(candidate => realpathSync(candidate)));
68
- return existing.size === 1 && existing.has(source);
69
+ return existing.size === 1 && existing.has(canonicalSource);
69
70
  }
70
71
 
71
72
  function callbackImports(callback, imports) {