Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

refactor: emit warning for invalid & in XML #1059

Merged
merged 1 commit into from
Oct 2, 2019
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
39 changes: 37 additions & 2 deletions xml-namespace-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ interface TestSetup {
expectedRegs: { name: string, path: string }[],
ignore?: RegExp,
assureNoDeps?: boolean,
expectError?: boolean
expectError?: boolean,
expectWarnings?: number
}

function getContext(
done: DoneFn,
{ resolveMap, expectedDeps, expectedRegs, assureNoDeps, ignore, expectError }: TestSetup) {
{ resolveMap, expectedDeps, expectedRegs, assureNoDeps, ignore, expectError, expectWarnings }: TestSetup) {
const actualDeps: string[] = [];
const actualWarnings: Error[] =[]
let callbackCalled = false;

const loaderContext = {
Expand All @@ -50,6 +52,10 @@ function getContext(
expect(source).not.toContain("global.registerModule");
}

if(expectWarnings){
expect(actualWarnings.length).toEqual(expectWarnings);
}

if (error && !expectError) {
done.fail(error)
} else if (!error && expectError) {
Expand All @@ -69,6 +75,9 @@ function getContext(
addDependency: (dep: string) => {
actualDeps.push(dep);
},
emitWarning: (err: Error) => {
actualWarnings.push(err);
},
query: { ignore }
}

Expand Down Expand Up @@ -277,4 +286,30 @@ describe("XmlNamespaceLoader", () => {

xmlNsLoader.call(loaderContext, testXml);
})


it("with '&&', '||', '<=' and '>=' in binding expression, emits warnings, but does not fail", (done) => {
const resolveMap = {
"nativescript-ui-chart": "node_module/nativescript-ui-chart/ui-chart.js",
}

const expectedDeps = [];

const expectedRegs = [
{ name: "nativescript-ui-chart", path: "nativescript-ui-chart" },
{ name: "nativescript-ui-chart/RadCartesianChart", path: "nativescript-ui-chart" },
];

const testXml = `
<Page xmlns="http://www.nativescript.org/tns.xsd">
<StackLayout xmlns:chart="nativescript-ui-chart">
<TextField text="{{ var1 && var2 || var1 >= var2 || var2 <= var1 }}" />
<chart:RadCartesianChart></chart:RadCartesianChart>
</StackLayout>
</Page>`;

const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs, expectWarnings: 1 });

xmlNsLoader.call(loaderContext, testXml);
})
});
11 changes: 10 additions & 1 deletion xml-namespace-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,17 @@ const loader: loader.Loader = function (source: string, map) {

saxParser.onopentag = (node: QualifiedTag) => { handleOpenTag(node.uri, node.local); };
saxParser.onerror = (err) => {
// Do only warning about invalid character "&"" for back-compatibility
// as it is common to use it in a binding expression
if (err &&
err.message.indexOf("Invalid character") >= 0 &&
err.message.indexOf("Char: &") >= 0) {
this.emitWarning(err)
} else {
callbackWrapper(err);
}

saxParser.error = null;
callbackWrapper(err);
};
saxParser.write(source).close();

Expand Down