Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): don't match property access in dev mode import usage check #8897

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,21 @@ return { get FooBaz() { return FooBaz }, get Last() { return Last } }
})"
`;

exports[`SFC compile <script setup> > dev mode import usage check > property access 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
import { Foo, Bar, Baz } from './foo'

export default /*#__PURE__*/_defineComponent({
setup(__props, { expose: __expose }) {
__expose();


return { get Foo() { return Foo } }
}

})"
`;

exports[`SFC compile <script setup> > dev mode import usage check > template ref 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
import { foo, bar, Baz } from './foo'
Expand Down
13 changes: 13 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,19 @@ describe('SFC compile <script setup>', () => {
)
assertCode(content)
})

test('property access', () => {
const { content } = compile(`
<script setup lang="ts">
import { Foo, Bar, Baz } from './foo'
</script>
<template>
<div>{{ Foo.Bar.Baz }}</div>
</template>
`)
expect(content).toMatch('return { get Foo() { return Foo } }')
assertCode(content)
})
})

describe('inlineTemplate mode', () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/compiler-sfc/src/script/importUsageCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function isImportUsed(local: string, sfc: SFCDescriptor): boolean {
return new RegExp(
// #4274 escape $ since it's a special char in regex
// (and is the only regex special char that is valid in identifiers)
`[^\\w$_]${local.replace(/\$/g, '\\$')}[^\\w$_]`
`[^\\w$.]${local.replace(/\$/g, '\\$')}[^\\w$]`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

).test(resolveTemplateUsageCheckString(sfc))
}

Expand Down Expand Up @@ -63,7 +63,11 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
)}`
}
}
if (prop.type === NodeTypes.ATTRIBUTE && prop.name === 'ref' && prop.value?.content) {
if (
prop.type === NodeTypes.ATTRIBUTE &&
prop.name === 'ref' &&
prop.value?.content
) {
code += `,${prop.value.content}`
}
}
Expand Down