Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ Keep these details in mind when changing behavior:
- Normalize stored paths to POSIX-style separators.
- Resolver order matters.
- The scanner only records string-literal dynamic imports.
- The scanner ignores type-only imports and re-exports (`import type`, `export type ... from`, all-inline-`type` specifier lists), and drops inline `type` specifiers from entity selections.
- `src/scanner/scan-file.js` is a committed compiled copy of `scan-file.ts` that tests import; regenerate it from the `.ts` source when the scanner changes.
- Cache entries are an optimization, not a source of truth.
- Changing the config or scanner version should invalidate cache reuse when hashes or versions no longer match.
- `run` appends impacted tests to the supplied runner command and exits with that command's exit code.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Sniffler currently supports:

- **Git-based workflows**: Diff-based, uncommitted, combined, and explicit file-based impact selection.
- **JavaScript and TypeScript**: Source graphs built from JS/TS files.
- **Type-only imports**: `import type`, `export type ... from`, and imports whose specifiers are all inline `type` are ignored, since they are erased at build time and cannot change runtime behavior.
- **Monorepos**: Workspace package discovery through `package.json` and `pnpm-workspace.yaml`.
- **TypeScript path aliases**: Resolution through configured TSConfig path mappings.
- **React Native platforms**: Optional runtime platform probing for extensionless relative imports and TSConfig aliases.
Expand Down
2 changes: 1 addition & 1 deletion src/cache/cache-key.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createHash } from "node:crypto";
import type { SnifflerConfig } from "../config/config-schema.js";

export const SCANNER_VERSION = "scan-file-v1";
export const SCANNER_VERSION = "scan-file-v2";

const hashValue = (value: string): string => {
return createHash("sha256").update(value).digest("hex");
Expand Down
87 changes: 64 additions & 23 deletions src/scanner/scan-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const createWarningMessage = (filePath, line, kind) => {
const prefix = filePath === undefined ? `${line}` : `${filePath}:${line}`;
return `${prefix} dynamic ${kind} target is not statically resolvable`;
};
const valueBindings = (specifiers) => {
return specifiers.filter((specifier) => !specifier.typeOnly).map((specifier) => specifier.binding);
};
const createNamedSelection = (entities) => {
return {
type: "named",
Expand Down Expand Up @@ -342,23 +345,42 @@ export const scanFileText = (input) => {
advance();
}
};
const parseNamedBindings = () => {
const isTypeModifierInSpecifier = () => {
const afterType = snapshotState();
advance(4);
skipWhitespaceAndComments();
if (currentChar() === "," || currentChar() === "}") {
restoreState(afterType);
return false;
}
if (startsWithWord("as")) {
advance(2);
skipWhitespaceAndComments();
const isAliasOfTypeBinding = !startsWithWord("as") && isIdentifierStart(currentChar());
restoreState(afterType);
return !isAliasOfTypeBinding;
}
restoreState(afterType);
return true;
};
const parseNamedSpecifiers = () => {
if (currentChar() !== "{") {
return null;
}
advance();
const bindings = [];
const specifiers = [];
while (state.index < text.length) {
skipWhitespaceAndComments();
if (currentChar() === "}") {
advance();
return bindings;
return specifiers;
}
if (currentChar() === ",") {
advance();
continue;
}
if (startsWithWord("type")) {
const typeOnly = startsWithWord("type") && isTypeModifierInSpecifier();
if (typeOnly) {
advance(4);
skipWhitespaceAndComments();
}
Expand All @@ -377,15 +399,15 @@ export const scanFileText = (input) => {
}
local = alias;
}
bindings.push(local === imported ? { imported } : { imported, local });
specifiers.push({ binding: local === imported ? { imported } : { imported, local }, typeOnly });
skipWhitespaceAndComments();
if (currentChar() === ",") {
advance();
continue;
}
if (currentChar() === "}") {
advance();
return bindings;
return specifiers;
}
}
return null;
Expand Down Expand Up @@ -453,6 +475,19 @@ export const scanFileText = (input) => {
advance();
}
};
const isTypeOnlyImportClause = () => {
const beforeType = snapshotState();
advance(4);
skipWhitespaceAndComments();
let typeIsDefaultBinding = currentChar() === ",";
if (startsWithWord("from")) {
advance(4);
skipWhitespaceAndComments();
typeIsDefaultBinding = currentChar() === "'" || currentChar() === '"' || currentChar() === "`";
}
restoreState(beforeType);
return !typeIsDefaultBinding;
};
const parseImportStatement = () => {
skipWhitespaceAndComments();
if (currentChar() === "'" || currentChar() === '"' || currentChar() === "`") {
Expand All @@ -463,9 +498,9 @@ export const scanFileText = (input) => {
finishStatement();
return;
}
if (startsWithWord("type")) {
advance(4);
skipWhitespaceAndComments();
if (startsWithWord("type") && isTypeOnlyImportClause()) {
finishStatement();
return;
}
if (currentChar() === "*") {
advance();
Expand Down Expand Up @@ -493,11 +528,12 @@ export const scanFileText = (input) => {
return;
}
if (currentChar() === "{") {
const named = parseNamedBindings();
if (named === null) {
const specifiers = parseNamedSpecifiers();
if (specifiers === null) {
finishStatement();
return;
}
const named = valueBindings(specifiers);
const afterNamed = snapshotState();
skipWhitespaceAndComments();
if (!startsWithWord("from")) {
Expand All @@ -508,7 +544,8 @@ export const scanFileText = (input) => {
advance(4);
skipWhitespaceAndComments();
const parsed = consumeLiteral();
if (parsed !== null) {
const allSpecifiersAreTypeOnly = named.length === 0 && specifiers.length > 0;
if (parsed !== null && !allSpecifiersAreTypeOnly) {
emitImport(parsed.specifier, "import", parsed.loc, createNamedSelection(named));
}
finishStatement();
Expand All @@ -526,12 +563,12 @@ export const scanFileText = (input) => {
advance();
skipWhitespaceAndComments();
if (currentChar() === "{") {
const named = parseNamedBindings();
if (named === null) {
const specifiers = parseNamedSpecifiers();
if (specifiers === null) {
finishStatement();
return;
}
namedBindings.push(...named);
namedBindings.push(...valueBindings(specifiers));
}
else {
finishStatement();
Expand All @@ -554,9 +591,9 @@ export const scanFileText = (input) => {
finishStatement();
}
};
const parseExportNamedList = (keywordLoc) => {
const named = parseNamedBindings();
if (named === null) {
const parseExportNamedList = (keywordLoc, typeOnlyStatement) => {
const specifiers = parseNamedSpecifiers();
if (specifiers === null) {
finishStatement();
return;
}
Expand All @@ -566,16 +603,16 @@ export const scanFileText = (input) => {
advance(4);
skipWhitespaceAndComments();
const parsed = consumeLiteral();
if (parsed !== null) {
for (const binding of named) {
if (parsed !== null && !typeOnlyStatement) {
for (const binding of valueBindings(specifiers)) {
emitReExport(parsed.specifier, binding.imported, binding.local ?? binding.imported, parsed.loc);
}
}
finishStatement();
return;
}
restoreState(afterNamed);
for (const binding of named) {
for (const { binding } of specifiers) {
emitLocalExport(binding.local ?? binding.imported, binding.imported === (binding.local ?? binding.imported) ? undefined : binding.imported, keywordLoc);
}
finishStatement();
Expand Down Expand Up @@ -665,7 +702,7 @@ export const scanFileText = (input) => {
return;
}
if (currentChar() === "{") {
parseExportNamedList(keywordLoc);
parseExportNamedList(keywordLoc, false);
return;
}
if (startsWithWord("default")) {
Expand All @@ -683,7 +720,11 @@ export const scanFileText = (input) => {
advance(4);
skipWhitespaceAndComments();
if (currentChar() === "{") {
parseExportNamedList(keywordLoc);
parseExportNamedList(keywordLoc, true);
return;
}
if (currentChar() === "*") {
finishStatement();
return;
}
state.index = saved.index;
Expand Down
Loading