summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/tests/globals.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /tools/lint/eslint/eslint-plugin-mozilla/tests/globals.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/lint/eslint/eslint-plugin-mozilla/tests/globals.js')
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/tests/globals.js161
1 files changed, 161 insertions, 0 deletions
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/tests/globals.js b/tools/lint/eslint/eslint-plugin-mozilla/tests/globals.js
new file mode 100644
index 0000000000..1098cb8a34
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/tests/globals.js
@@ -0,0 +1,161 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+var { getGlobalsForCode } = require("../lib/globals");
+var assert = require("assert");
+
+/* global describe, it */
+
+describe("globals", function () {
+ it("should reflect top-level this property assignment", function () {
+ const globals = getGlobalsForCode(`
+this.foo = 10;
+`);
+ assert.deepEqual(globals, [{ name: "foo", writable: true }]);
+ });
+
+ it("should reflect this property assignment inside block", function () {
+ const globals = getGlobalsForCode(`
+{
+ this.foo = 10;
+}
+`);
+ assert.deepEqual(globals, [{ name: "foo", writable: true }]);
+ });
+
+ it("should ignore this property assignment inside function declaration", function () {
+ const globals = getGlobalsForCode(`
+function f() {
+ this.foo = 10;
+}
+`);
+ assert.deepEqual(globals, [{ name: "f", writable: true }]);
+ });
+
+ it("should ignore this property assignment inside function expression", function () {
+ const globals = getGlobalsForCode(`
+(function f() {
+ this.foo = 10;
+});
+`);
+ assert.deepEqual(globals, []);
+ });
+
+ it("should ignore this property assignment inside method", function () {
+ const globals = getGlobalsForCode(`
+({
+ method() {
+ this.foo = 10;
+ }
+});
+`);
+ assert.deepEqual(globals, []);
+ });
+
+ it("should ignore this property assignment inside accessor", function () {
+ const globals = getGlobalsForCode(`
+({
+ get x() {
+ this.foo = 10;
+ },
+ set x(v) {
+ this.bar = 10;
+ }
+});
+`);
+ assert.deepEqual(globals, []);
+ });
+
+ it("should reflect this property assignment inside arrow function", function () {
+ const globals = getGlobalsForCode(`
+() => {
+ this.foo = 10;
+};
+`);
+ assert.deepEqual(globals, [{ name: "foo", writable: true }]);
+ });
+
+ it("should ignore this property assignment inside arrow function inside function expression", function () {
+ const globals = getGlobalsForCode(`
+(function f() {
+ () => {
+ this.foo = 10;
+ };
+});
+`);
+ assert.deepEqual(globals, []);
+ });
+
+ it("should ignore this property assignment inside class static", function () {
+ const globals = getGlobalsForCode(`
+class A {
+ static {
+ this.foo = 10;
+ (() => {
+ this.bar = 10;
+ })();
+ }
+}
+`);
+ assert.deepEqual(globals, [{ name: "A", writable: true }]);
+ });
+
+ it("should ignore this property assignment inside class property", function () {
+ const globals = getGlobalsForCode(`
+class A {
+ a = this.foo = 10;
+ b = (() => {
+ this.bar = 10;
+ })();
+}
+`);
+ assert.deepEqual(globals, [{ name: "A", writable: true }]);
+ });
+
+ it("should ignore this property assignment inside class static property", function () {
+ const globals = getGlobalsForCode(`
+class A {
+ static a = this.foo = 10;
+ static b = (() => {
+ this.bar = 10;
+ })();
+}
+`);
+ assert.deepEqual(globals, [{ name: "A", writable: true }]);
+ });
+
+ it("should ignore this property assignment inside class private property", function () {
+ const globals = getGlobalsForCode(`
+class A {
+ #a = this.foo = 10;
+ #b = (() => {
+ this.bar = 10;
+ })();
+}
+`);
+ assert.deepEqual(globals, [{ name: "A", writable: true }]);
+ });
+
+ it("should ignore this property assignment inside class static private property", function () {
+ const globals = getGlobalsForCode(`
+class A {
+ static #a = this.foo = 10;
+ static #b = (() => {
+ this.bar = 10;
+ })();
+}
+`);
+ assert.deepEqual(globals, [{ name: "A", writable: true }]);
+ });
+
+ it("should reflect lazy getters", function () {
+ const globals = getGlobalsForCode(`
+ChromeUtils.defineESModuleGetters(this, {
+ A: "B",
+});
+`);
+ assert.deepEqual(globals, [{ name: "A", writable: true, explicit: true }]);
+ });
+});