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

test: add tests for allowReserved parser option with flat config #15450

Merged
merged 1 commit into from Dec 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
117 changes: 117 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -7352,6 +7352,123 @@ describe("Linter with FlatConfigArray", () => {
assert.strictEqual(messages.length, 0);
});

it("should not allow the use of reserved words as variable names in ES3", () => {
const code = "var char;";
const messages = linter.verify(code, {
languageOptions: {
ecmaVersion: 3,
sourceType: "script"
}
}, filename);

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].severity, 2);
assert.isTrue(messages[0].fatal);
assert.match(messages[0].message, /^Parsing error:.*'char'/u);
});

it("should not allow the use of reserved words as property names in member expressions in ES3", () => {
const code = "obj.char;";
const messages = linter.verify(code, {
languageOptions: {
ecmaVersion: 3,
sourceType: "script"
}
}, filename);

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].severity, 2);
assert.isTrue(messages[0].fatal);
assert.match(messages[0].message, /^Parsing error:.*'char'/u);
});

it("should not allow the use of reserved words as property names in object literals in ES3", () => {
const code = "var obj = { char: 1 };";
const messages = linter.verify(code, {
languageOptions: {
ecmaVersion: 3,
sourceType: "script"
}
}, filename);

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].severity, 2);
assert.isTrue(messages[0].fatal);
assert.match(messages[0].message, /^Parsing error:.*'char'/u);
});

it("should allow the use of reserved words as variable and property names in ES3 when allowReserved is true", () => {
const code = "var char; obj.char; var obj = { char: 1 };";
const messages = linter.verify(code, {
languageOptions: {
ecmaVersion: 3,
sourceType: "script",
parserOptions: {
allowReserved: true
}
}
}, filename);

assert.strictEqual(messages.length, 0);
});

it("should not allow the use of reserved words as variable names in ES > 3", () => {
const ecmaVersions = [void 0, ...espree.supportedEcmaVersions.filter(ecmaVersion => ecmaVersion > 3)];

ecmaVersions.forEach(ecmaVersion => {
const code = "var enum;";
const messages = linter.verify(code, {
languageOptions: {
...ecmaVersion ? { ecmaVersion } : {},
sourceType: "script"
}
}, filename);

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].severity, 2);
assert.isTrue(messages[0].fatal);
assert.match(messages[0].message, /^Parsing error:.*'enum'/u);
});
});

it("should allow the use of reserved words as property names in ES > 3", () => {
const ecmaVersions = [void 0, ...espree.supportedEcmaVersions.filter(ecmaVersion => ecmaVersion > 3)];

ecmaVersions.forEach(ecmaVersion => {
const code = "obj.enum; obj.function; var obj = { enum: 1, function: 2 };";
const messages = linter.verify(code, {
languageOptions: {
...ecmaVersion ? { ecmaVersion } : {},
sourceType: "script"
}
}, filename);

assert.strictEqual(messages.length, 0);
});
});

it("should not allow `allowReserved: true` in ES > 3", () => {
const ecmaVersions = [void 0, ...espree.supportedEcmaVersions.filter(ecmaVersion => ecmaVersion > 3)];

ecmaVersions.forEach(ecmaVersion => {
const code = "";
const messages = linter.verify(code, {
languageOptions: {
...ecmaVersion ? { ecmaVersion } : {},
sourceType: "script",
parserOptions: {
allowReserved: true
}
}
}, filename);

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].severity, 2);
assert.isTrue(messages[0].fatal);
assert.match(messages[0].message, /^Parsing error:.*allowReserved/u);
});
});

});

});
Expand Down