Skip to content

Commit

Permalink
Ignore t attributes with interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
mxjp committed Feb 27, 2025
1 parent ad9fd11 commit 36530b6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/aurelia-template-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parseFragment } from "parse5";
import { AureliaI18nAttribute } from "./aurelia-i18n-attribute.js";
import { Config, ElementContentLocalizationType } from "./config.js";
import { Diagnostic } from "./diagnostics.js";
import { Source, SourceJustifyKeysOptions, SourceJustifyKeysResult } from "./source.js";
import { Source, SourceExtractKeysOptions, SourceJustifyKeysOptions, SourceJustifyKeysResult } from "./source.js";
import { analyzeElementContent, DocumentFragment, Element, getAttributeValue, traverseElements, treeDiagnostics } from "./utility/parse5-tree.js";

/**
Expand Down Expand Up @@ -38,13 +38,13 @@ export class AureliaTemplateFile implements Source {
return this.#source;
}

extractKeys(config: Config) {
extractKeys(config: Config, { diagnostics }: SourceExtractKeysOptions) {
const keys = new Map<string, string>();
for (const element of traverseElements(this.#root, config.ignoreElement)) {
const elementWhitespaceHandling = config.getElementWhitespaceHandling(element.tagName);

const attributeValue = getAttributeValue(element, "t");
if (attributeValue !== undefined) {
if (attributeValue !== undefined && !config.ignoreAttributeValue(attributeValue)) {
try {
const attribute = AureliaI18nAttribute.parse(attributeValue);
for (const [name, key] of attribute) {
Expand Down Expand Up @@ -74,7 +74,15 @@ export class AureliaTemplateFile implements Source {
}
}
}
} catch {}
} catch (error) {
diagnostics.report({
type: Diagnostic.Type.InvalidTAttribute,
details: { error },
filename: this.filename,
source: this.source,
...treeDiagnostics.attribute(element, "t"),
});
}
}
}
return keys;
Expand All @@ -100,7 +108,7 @@ export class AureliaTemplateFile implements Source {
}

let originalAttribute: AureliaI18nAttribute | undefined;
if (originalAttributeValue !== undefined) {
if (originalAttributeValue !== undefined && !config.ignoreAttributeValue(originalAttributeValue)) {
try {
originalAttribute = AureliaI18nAttribute.parse(originalAttributeValue);
for (const key of originalAttribute.keys()) {
Expand Down Expand Up @@ -128,7 +136,7 @@ export class AureliaTemplateFile implements Source {
...treeDiagnostics.content(element)
});
}
if (originalAttributeValue) {
if (originalAttributeValue !== undefined) {
diagnostics.report({
type: Diagnostic.Type.DisallowedTAttribute,
details: {},
Expand Down
58 changes: 57 additions & 1 deletion test/aurelia-template-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,59 @@ await suite("aurelia-template-file", async () => {
`));
});

await suite("ignore t attributes by value", async () => {
await test("extractKeys", () => {
const source = AureliaTemplateFile.parse(filename, code(`
<div t="[value]\${foo.bar}"></div>
<div foo="bar" t="[foo]test.foo"></div>
`));
const result = source.extractKeys(config, {
prefix: "test.",
diagnostics: expectNoDiagnostics(),
});
deepStrictEqual(result, new Map([
["test.foo", "bar"],
]));
});

await test("justifyKeys", () => {
const source = AureliaTemplateFile.parse(filename, code(`
<div t="[value]\${foo.bar}"></div>
<div foo="bar"></div>
`));
const result = source.justifyKeys(config, {
prefix: "test.",
diagnostics: expectNoDiagnostics(),
});
strictEqual(result.modified, true);
strictEqual(result.replacedKeys.size, 0);
strictEqual(source.source, code(`
<div t="[value]\${foo.bar}"></div>
<div foo="bar" t="[foo]test.t0"></div>
`));
});

await test("disallowed diagnostic", () => {
const source = AureliaTemplateFile.parse(filename, code(`
<test-element t="\${foo.bar}"></test-element>
<div foo="bar"></div>
`));
const diagnostics = captureDiagnostics();
const result = source.justifyKeys(config, {
prefix: "test.",
diagnostics: diagnostics.host,
});
strictEqual(result.modified, true);
strictEqual(result.replacedKeys.size, 0);
strictEqual(source.source, code(`
<test-element t="\${foo.bar}"></test-element>
<div foo="bar" t="[foo]test.t0"></div>
`));
strictEqual(diagnostics.all.length, 1);
strictEqual(diagnostics.all[0].type, Diagnostic.Type.DisallowedTAttribute);
});
});

await test("reuse existing keys", () => {
const source = AureliaTemplateFile.parse(filename, code(`
<div foo="bar" t="test.t7">content</div>
Expand Down Expand Up @@ -136,7 +189,10 @@ await suite("aurelia-template-file", async () => {
function assertWhitespaceHandling(whitespace: ConfigOptions["whitespace"], markup: string, expectedValues: string[]) {
const config = createConfig(testDir, { src: ".", whitespace });
const source = AureliaTemplateFile.parse(filename, code(markup));
deepStrictEqual(Array.from(source.extractKeys(config).values()), expectedValues);
deepStrictEqual(Array.from(source.extractKeys(config, {
prefix: "test.",
diagnostics: expectNoDiagnostics(),
}).values()), expectedValues);
}

await test("preserves whitespace by default", () => {
Expand Down

0 comments on commit 36530b6

Please sign in to comment.