fix(compiler): eliminate false-positive mixin warnings for barrel imports and useDefineForClassFields:false#6691
Open
fix(compiler): eliminate false-positive mixin warnings for barrel imports and useDefineForClassFields:false#6691
Conversation
…orts and useDefineForClassFields:false
Two unrelated conditions in `mergeExtendedClassMeta` / `resolveAndProcessExtendedClass`
caused spurious build warnings for valid mixin-factory patterns.
**Fix 1 — useDefineForClassFields: false**
`detectModernPropDeclarations` returns `false` whenever TypeScript has stripped
`PropertyDeclaration` nodes from the class body (which it always does when
`useDefineForClassFields: false`). Guard the warning with a check for that flag so
the "Component classes can only extend from other Stencil decorated base classes
when targeting more modern JavaScript" message is never emitted in that configuration.
**Fix 2 — barrel re-export support**
When a mixin factory is imported through a barrel index file
(`export * from './mixin'` / `export { X } from './mixin'`), the direct
`statements.find()` lookup in `resolveAndProcessExtendedClass` finds nothing,
triggering the "Please import class / mixin-factory declarations directly and not
via barrel files" warning even for a correct setup. A new `resolveFromBarrelExports`
helper follows re-exports one level deep so the real declaration is located before
the warning is emitted. The dependent-class entry now records the actual source file
and file name (not the barrel's) for correct downstream processing.
**Test infrastructure**
- `transpileModules()` added to `transpile.ts` — a multi-file variant of
`transpileModule` that pre-populates `compilerCtx.moduleMap` with auxiliary
source files so cross-file scenarios can be exercised in unit tests without
hitting the real file system.
- Three new test cases in `parse-mixin.spec.ts` covering:
- `export * from` barrel re-export → no warning
- named `export { X as Y } from` re-export → no warning
- explicit `useDefineForClassFields: false` → no warning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two separate conditions in
mergeExtendedClassMeta/resolveAndProcessExtendedClassproduced spurious build warnings for valid mixin-factory patterns. Both are fixed in a single commit.Fix 1 —
useDefineForClassFields: falsefalse positiveRoot cause:
detectModernPropDeclarationsreturnsfalsewhenever TypeScript has strippedPropertyDeclarationnodes from the class body. WithuseDefineForClassFields: falsethat stripping always happens, so the guard fired unconditionally and emitted:Fix: Guard the warning with
buildCtx.config.tsCompilerOptions?.useDefineForClassFields !== falseso the check is skipped entirely in that configuration (which is the recommended Stencil setup for"target": "es2022").Fix 2 — Barrel re-export support
Root cause: When a mixin factory is imported through a barrel index file:
the direct
statements.find()lookup inresolveAndProcessExtendedClassreturnsnullbecause the barrel has no own declarations. This triggered:even though the pattern is completely valid.
Fix: Added a
resolveFromBarrelExportshelper that followsexport * from '...'andexport { X } from '...'re-exports one level deep to locate the real declaration. The call site tries barrel traversal before emitting the warning. ThedependentClassesentry is updated to record the actual source file / file name (not the barrel's) for correct downstream processing.Test infrastructure
src/compiler/transformers/test/transpile.tstranspileModules(files, config?, tsConfig?)— multi-file variant oftranspileModulethat pre-populatescompilerCtx.moduleMapwith auxiliary source files so cross-file scenarios work in unit tests without hitting the real file systemsrc/compiler/transformers/test/parse-mixin.spec.tsdescribe('mixin factory imported from a barrel index file')with 3 test cases:export * frombarrel, namedexport { X as Y } frombarrel, and explicituseDefineForClassFields: falseTesting
npm run ts scripts/index.ts -- --prod --cicompletes successfully. The onlytsc.proderrors are pre-existing insrc/testing/jest/jest-28/jest-environment.ts(unrelated, present onmain)."useDefineForClassFields": false, "target": "es2022"— zero warnings after applying the fix.