Skip to content

Commit

Permalink
Run Prettier on JS code fences, part 13 (#21778)
Browse files Browse the repository at this point in the history
The PR focuses only on JS code fences.

Idea is to gradually prettify all the JS code fences before the full automation.
  • Loading branch information
OnkarRuikar committed Oct 24, 2022
1 parent 923883a commit 3db7965
Show file tree
Hide file tree
Showing 30 changed files with 270 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,27 @@ If the following invariants are violated, the proxy will throw a
The following code traps the {{jsxref("Operators/delete", "delete")}} operator.

```js
const p = new Proxy({}, {
deleteProperty(target, prop) {
if (!(prop in target)) {
console.log(`property not found: ${prop}`);
return false;
}
delete target[prop];
console.log(`property removed: ${prop}`);
return true;
},
});
const p = new Proxy(
{},
{
deleteProperty(target, prop) {
if (!(prop in target)) {
console.log(`property not found: ${prop}`);
return false;
}
delete target[prop];
console.log(`property removed: ${prop}`);
return true;
},
}
);

p.a = 10;
console.log('a' in p); // true
console.log("a" in p); // true

const result1 = delete p.a; // "property removed: a"
console.log(result1); // true
console.log('a' in p); // false
console.log("a" in p); // false

const result2 = delete p.a; // "property not found: a"
console.log(result2); // false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,26 @@ If the following invariants are violated, the proxy will throw a
The following code traps getting a property value.

```js
const p = new Proxy({}, {
get(target, property, receiver) {
console.log(`called: ${property}`);
return 10;
},
});
const p = new Proxy(
{},
{
get(target, property, receiver) {
console.log(`called: ${property}`);
return 10;
},
}
);

console.log(p.a); // "called: a"
// 10
console.log(p.a);
// "called: a"
// 10
```

The following code violates an invariant.

```js
const obj = {};
Object.defineProperty(obj, 'a', {
Object.defineProperty(obj, "a", {
configurable: false,
enumerable: false,
value: 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ If the following invariants are violated, the proxy will throw a {{jsxref("TypeE
The following code traps {{jsxref("Object.getOwnPropertyDescriptor()")}}.

```js
const p = new Proxy({ a: 20}, {
getOwnPropertyDescriptor(target, prop) {
console.log(`called: ${prop}`);
return { configurable: true, enumerable: true, value: 10 };
},
});
const p = new Proxy(
{ a: 20 },
{
getOwnPropertyDescriptor(target, prop) {
console.log(`called: ${prop}`);
return { configurable: true, enumerable: true, value: 10 };
},
}
);

console.log(Object.getOwnPropertyDescriptor(p, 'a').value); // "called: a"
// 10
console.log(Object.getOwnPropertyDescriptor(p, "a").value);
// "called: a"
// 10
```

The following code violates an invariant.
Expand All @@ -88,7 +92,7 @@ const p = new Proxy(obj, {
},
});

Object.getOwnPropertyDescriptor(p, 'a'); // TypeError is thrown
Object.getOwnPropertyDescriptor(p, "a"); // TypeError is thrown
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ const obj = {};
const proto = {};
const handler = {
getPrototypeOf(target) {
console.log(target === obj); // true
console.log(target === obj); // true
console.log(this === handler); // true
return proto;
},
};

const p = new Proxy(obj, handler);
console.log(Object.getPrototypeOf(p) === proto); // true
console.log(Object.getPrototypeOf(p) === proto); // true
```

### Five ways to trigger the getPrototypeOf trap
Expand All @@ -89,11 +89,11 @@ const p = new Proxy(obj, {
},
});
console.log(
Object.getPrototypeOf(p) === Array.prototype, // true
Object.getPrototypeOf(p) === Array.prototype, // true
Reflect.getPrototypeOf(p) === Array.prototype, // true
p.__proto__ === Array.prototype, // true
Array.prototype.isPrototypeOf(p), // true
p instanceof Array, // true
p.__proto__ === Array.prototype, // true
Array.prototype.isPrototypeOf(p), // true
p instanceof Array // true
);
```

Expand All @@ -103,8 +103,8 @@ console.log(
const obj = {};
const p = new Proxy(obj, {
getPrototypeOf(target) {
return 'foo';
}
return "foo";
},
});
Object.getPrototypeOf(p); // TypeError: "foo" is not an object or null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@ If the following invariants are violated, the proxy will throw a
The following code traps the {{jsxref("Operators/in", "in")}} operator.

```js
const p = new Proxy({}, {
has(target, prop) {
console.log(`called: ${prop}`);
return true;
},
});
const p = new Proxy(
{},
{
has(target, prop) {
console.log(`called: ${prop}`);
return true;
},
}
);

console.log('a' in p); // "called: a"
// true
console.log("a" in p);
// "called: a"
// true
```

The following code violates an invariant.
Expand All @@ -93,7 +97,7 @@ const p = new Proxy(obj, {
},
});

'a' in p; // TypeError is thrown
"a" in p; // TypeError is thrown
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ In this example the target has two properties, `notProxied` and
```js
const target = {
notProxied: "original value",
proxied: "original value"
proxied: "original value",
};

const handler = {
Expand All @@ -101,13 +101,13 @@ const handler = {
return "replaced value";
}
return Reflect.get(...arguments);
}
},
};

const proxy = new Proxy(target, handler);

console.log(proxy.notProxied); // "original value"
console.log(proxy.proxied); // "replaced value"
console.log(proxy.proxied); // "replaced value"
```

## Specifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,32 @@ If the following invariants are violated, the proxy will throw a
The following code traps {{jsxref("Object.isExtensible()")}}.

```js
const p = new Proxy({}, {
isExtensible(target) {
console.log('called');
return true;
const p = new Proxy(
{},
{
isExtensible(target) {
console.log("called");
return true;
},
}
});
);

console.log(Object.isExtensible(p)); // "called"
// true
console.log(Object.isExtensible(p));
// "called"
// true
```

The following code violates the invariant.

```js example-bad
const p = new Proxy({}, {
isExtensible(target) {
return false;
const p = new Proxy(
{},
{
isExtensible(target) {
return false;
},
}
});
);

Object.isExtensible(p); // TypeError is thrown
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,35 @@ If the following invariants are violated, the proxy will throw a
The following code traps {{jsxref("Object.getOwnPropertyNames()")}}.

```js
const p = new Proxy({}, {
ownKeys(target) {
console.log('called');
return ['a', 'b', 'c'];
const p = new Proxy(
{},
{
ownKeys(target) {
console.log("called");
return ["a", "b", "c"];
},
}
});
);

console.log(Object.getOwnPropertyNames(p)); // "called"
// [ 'a', 'b', 'c' ]
console.log(Object.getOwnPropertyNames(p));
// "called"
// [ 'a', 'b', 'c' ]
```

The following code violates an invariant.

```js example-bad
const obj = {};
Object.defineProperty(obj, 'a', {
Object.defineProperty(obj, "a", {
configurable: false,
enumerable: true,
value: 10 }
);
value: 10,
});

const p = new Proxy(obj, {
ownKeys(target) {
return [123, 12.5, true, false, undefined, null, {}, []];
}
},
});

console.log(Object.getOwnPropertyNames(p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,33 @@ If the following invariants are violated, the proxy will throw a {{jsxref("TypeE
The following code traps {{jsxref("Object.preventExtensions()")}}.

```js
const p = new Proxy({}, {
preventExtensions(target) {
console.log('called');
Object.preventExtensions(target);
return true;
const p = new Proxy(
{},
{
preventExtensions(target) {
console.log("called");
Object.preventExtensions(target);
return true;
},
}
});
);

console.log(Object.preventExtensions(p)); // "called"
// false
console.log(Object.preventExtensions(p));
// "called"
// false
```

The following code violates the invariant.

```js example-bad
const p = new Proxy({}, {
preventExtensions(target) {
return true;
const p = new Proxy(
{},
{
preventExtensions(target) {
return true;
},
}
});
);

Object.preventExtensions(p); // TypeError is thrown
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,22 @@ If the following invariants are violated, the proxy will throw a
The following code traps setting a property value.

```js
const p = new Proxy({}, {
set(target, prop, value, receiver) {
target[prop] = value;
console.log(`property set: ${prop} = ${value}`);
return true;
const p = new Proxy(
{},
{
set(target, prop, value, receiver) {
target[prop] = value;
console.log(`property set: ${prop} = ${value}`);
return true;
},
}
})
);

console.log('a' in p); // false
console.log("a" in p); // false

p.a = 10; // "property set: a = 10"
console.log('a' in p); // true
console.log(p.a); // 10
p.a = 10; // "property set: a = 10"
console.log("a" in p); // true
console.log(p.a); // 10
```

## Specifications
Expand Down
Loading

0 comments on commit 3db7965

Please sign in to comment.