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(language-plugin-pug): compute offset correctly of pug class #4652

Merged
merged 9 commits into from
Aug 25, 2024
Merged
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
46 changes: 32 additions & 14 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,22 +614,40 @@ function* generateReferencesForScopedCssClasses(
&& prop.name === 'class'
&& prop.value
) {
let startOffset = prop.value.loc.start.offset;
let content = prop.value.loc.source;
let isWrapped = false;
if (
(content.startsWith(`'`) && content.endsWith(`'`))
|| (content.startsWith(`"`) && content.endsWith(`"`))
) {
content = content.slice(1, -1);
isWrapped = true;
}
if (content) {
const classes = collectClasses(content, startOffset + (isWrapped ? 1 : 0));
ctx.scopedClasses.push(...classes);
if (options.template.lang === 'pug') {
const getClassOffset = Reflect.get(prop.value.loc.start, 'getClassOffset') as (offset: number) => number;
const content = prop.value.loc.source.slice(1, -1);

let startOffset = 1;
for (const className of content.split(' ')) {
if (className) {
ctx.scopedClasses.push({
source: 'template',
className,
offset: getClassOffset(startOffset),
});
}
startOffset += className.length + 1;
}
}
else {
ctx.emptyClassOffsets.push(startOffset);
let startOffset = prop.value.loc.start.offset;
let content = prop.value.loc.source;
let isWrapped = false;
if (
(content.startsWith(`'`) && content.endsWith(`'`))
|| (content.startsWith(`"`) && content.endsWith(`"`))
) {
content = content.slice(1, -1);
isWrapped = true;
}
if (content) {
const classes = collectClasses(content, startOffset + (isWrapped ? 1 : 0));
ctx.scopedClasses.push(...classes);
}
else {
ctx.emptyClassOffsets.push(startOffset);
}
}
}
else if (
Expand Down
26 changes: 20 additions & 6 deletions packages/language-plugin-pug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ const plugin: VueLanguagePlugin = ({ modules }) => {
const proxys = new WeakMap();
return new Proxy(target, {
get(target, prop, receiver) {
if (prop === 'getClassOffset') {
// div.foo#baz.bar
// ^^^ ^^^
// class=" foo bar"
// ^^^ ^^^
// NOTE: we need to expose source offset getter
return function(startOffset: number) {
return getOffset(target.offset + startOffset);
};
}
if (prop === 'offset') {
const htmlOffset = target.offset;
const nums: number[] = [];
for (const mapped of map.toSourceLocation(htmlOffset)) {
nums.push(mapped[0]);
}
return Math.max(-1, ...nums);
return getOffset(target.offset);
}
const value = Reflect.get(target, prop, receiver);
if (typeof value === 'object' && value !== null) {
Expand All @@ -59,6 +64,15 @@ const plugin: VueLanguagePlugin = ({ modules }) => {
}
});
}

function getOffset(offset: number) {
const htmlOffset = offset;
const nums: number[] = [];
for (const mapped of map.toSourceLocation(htmlOffset)) {
nums.push(mapped[0]);
}
return Math.max(-1, ...nums);
}
}
}
},
Expand Down