Skip to content

Commit f2a7d29

Browse files
committed
fix: exclude inline type-modifier imports from Octane module detection
import { type X } from 'octane' is erased by TypeScript just like import type { X } from 'octane', but the OCTANE_IMPORT regex only rejected the latter form. A .ts file using only inline type modifiers was incorrectly counted as an Octane module, causing it to appear as a Strong coverage exception or trigger CI regressions. Replace the bare regex .test() with a hasRuntimeOctaneImport() function that, after matching an octane import, verifies at least one named specifier lacks the type modifier.
1 parent 7df5baf commit f2a7d29

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎packages/cli/src/commands/analyze/strong-coverage.js‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,29 @@ export async function loadStrongPolicy(project, { required }) {
116116
}
117117

118118
/**
119-
* A runtime (not type-only) import of octane or one of its subpaths. The
120-
* clause may span lines but never reaches into the next `import`, so a
121-
* semicolon-free module cannot borrow a later type-only import's specifier.
119+
* A non-`import type` import of octane or one of its subpaths. The clause may
120+
* span lines but never reaches into the next `import`, so a semicolon-free
121+
* module cannot borrow a later type-only import's specifier.
122122
*/
123123
const OCTANE_IMPORT =
124-
/^\s*import\s+(?!type\b)(?:(?!\bimport\b)[^;])*?\bfrom\s*['"]octane(?:\/[^'"]*)?['"]/m;
124+
/^\s*import\s+(?!type\b)(?:(?!\bimport\b)[^;])*?\bfrom\s*['"]octane(?:\/[^'"]*)?['"]/gm;
125+
126+
/**
127+
* True when `source` contains at least one runtime (non-type) import of
128+
* octane. Both `import type { X }` and `import { type X }` are erased by
129+
* TypeScript and do not make the module something Octane compiles.
130+
*
131+
* @param {string} source
132+
*/
133+
function hasRuntimeOctaneImport(source) {
134+
for (const m of source.matchAll(OCTANE_IMPORT)) {
135+
const braces = m[0].match(/\{([^}]*)\}/s);
136+
if (!braces) return true;
137+
const specifiers = braces[1].split(',');
138+
if (specifiers.some((s) => s.trim() !== '' && !/^type\s/.test(s.trim()))) return true;
139+
}
140+
return false;
141+
}
125142

126143
/**
127144
* @param {string | undefined} source a JSX import source
@@ -153,7 +170,7 @@ export function isOctaneModule(absolute, source, jsxImportSource, policy) {
153170
if (pragma !== null) return isOctaneJsxSource(pragma);
154171
return jsxImportSource === undefined || isOctaneJsxSource(jsxImportSource);
155172
}
156-
if (extension === '.ts' || extension === '.js') return OCTANE_IMPORT.test(source);
173+
if (extension === '.ts' || extension === '.js') return hasRuntimeOctaneImport(source);
157174
return false;
158175
}
159176

‎packages/cli/tests/analyze.test.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ describe('octane analyze Strong coverage', () => {
271271
'src/strong-hook.ts':
272272
"'use strong';\nimport { useState } from 'octane';\nexport function useOne() { return useState(1); }\n",
273273
'src/types.ts': "import type { OctaneNode } from 'octane';\nexport type Node = OctaneNode;\n",
274+
'src/inline-types.ts':
275+
"import { type OctaneNode, type StrongStatus } from 'octane';\nexport type Node = OctaneNode;\nexport type Status = StrongStatus;\n",
274276
'src/no-semi.ts':
275277
"import { add } from './math'\nimport type { OctaneNode } from 'octane'\nexport type Sum = OctaneNode\n",
276278
'src/math.ts': 'export const add = (a: number, b: number) => a + b;\n',

0 commit comments

Comments
 (0)