Skip to content

Commit 1ae40b4

Browse files
authored
style: apply prettier (#564)
1 parent 2201fd7 commit 1ae40b4

37 files changed

+1165
-550
lines changed

.eslintrc.json

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
2-
"env": {
3-
"browser": true,
4-
"commonjs": true,
5-
"es2020": true,
6-
"node": true,
7-
"mocha": true
8-
},
9-
"extends": "eslint:recommended",
10-
"plugins": ["import"],
11-
"rules": {
12-
"eol-last": "error",
13-
"import/extensions": ["error", "always"],
14-
"no-console": "error",
15-
"no-constant-condition": ["error", { "checkLoops": false }],
16-
"no-eval": "error",
17-
"no-implied-eval": "error",
18-
"no-trailing-spaces": "error",
19-
"prefer-const": "error",
20-
"semi": "error"
21-
},
22-
"parserOptions": {
23-
"sourceType": "module"
24-
}
2+
"env": {
3+
"browser": true,
4+
"commonjs": true,
5+
"es2020": true,
6+
"node": true,
7+
"mocha": true
8+
},
9+
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
10+
"plugins": ["import"],
11+
"rules": {
12+
"eol-last": "error",
13+
"import/extensions": ["error", "always"],
14+
"no-console": "error",
15+
"no-constant-condition": ["error", { "checkLoops": false }],
16+
"no-eval": "error",
17+
"no-implied-eval": "error",
18+
"no-trailing-spaces": "error",
19+
"prefer-const": "error",
20+
"semi": "error"
21+
},
22+
"parserOptions": {
23+
"sourceType": "module"
24+
}
2525
}

lib/error.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function contextAsText(node) {
2121
hierarchy.unshift(parent);
2222
node = parent;
2323
}
24-
return hierarchy.map(n => appendIfExist(n.type, n.name)).join(" -> ");
24+
return hierarchy.map((n) => appendIfExist(n.type, n.name)).join(" -> ");
2525
}
2626

2727
/**
@@ -33,18 +33,25 @@ function contextAsText(node) {
3333
* @param {"Syntax" | "Validation"} kind error type
3434
* @param {WebIDL2ErrorOptions} [options]
3535
*/
36-
function error(source, position, current, message, kind, { level = "error", autofix, ruleName } = {}) {
36+
function error(
37+
source,
38+
position,
39+
current,
40+
message,
41+
kind,
42+
{ level = "error", autofix, ruleName } = {}
43+
) {
3744
/**
3845
* @param {number} count
3946
*/
4047
function sliceTokens(count) {
41-
return count > 0 ?
42-
source.slice(position, position + count) :
43-
source.slice(Math.max(position + count, 0), position);
48+
return count > 0
49+
? source.slice(position, position + count)
50+
: source.slice(Math.max(position + count, 0), position);
4451
}
4552

4653
function tokensToText(inputs, { precedes } = {}) {
47-
const text = inputs.map(t => t.trivia + t.value).join("");
54+
const text = inputs.map((t) => t.trivia + t.value).join("");
4855
const nextToken = source[position];
4956
if (nextToken.type === "eof") {
5057
return text;
@@ -57,9 +64,11 @@ function error(source, position, current, message, kind, { level = "error", auto
5764

5865
const maxTokens = 5; // arbitrary but works well enough
5966
const line =
60-
source[position].type !== "eof" ? source[position].line :
61-
source.length > 1 ? source[position - 1].line :
62-
1;
67+
source[position].type !== "eof"
68+
? source[position].line
69+
: source.length > 1
70+
? source[position - 1].line
71+
: 1;
6372

6473
const precedingLastLine = lastLine(
6574
tokensToText(sliceTokens(-maxTokens), { precedes: true })
@@ -74,7 +83,12 @@ function error(source, position, current, message, kind, { level = "error", auto
7483

7584
const contextType = kind === "Syntax" ? "since" : "inside";
7685
const inSourceName = source.name ? ` in ${source.name}` : "";
77-
const grammaticalContext = (current && current.name) ? `, ${contextType} \`${current.partial ? "partial " : ""}${contextAsText(current)}\`` : "";
86+
const grammaticalContext =
87+
current && current.name
88+
? `, ${contextType} \`${current.partial ? "partial " : ""}${contextAsText(
89+
current
90+
)}\``
91+
: "";
7892
const context = `${kind} error at line ${line}${inSourceName}${grammaticalContext}:\n${sourceContext}`;
7993
return {
8094
message: `${context} ${message}`,
@@ -86,7 +100,7 @@ function error(source, position, current, message, kind, { level = "error", auto
86100
ruleName,
87101
autofix,
88102
input: subsequentText,
89-
tokens: subsequentTokens
103+
tokens: subsequentTokens,
90104
};
91105
}
92106

@@ -101,7 +115,20 @@ export function syntaxError(source, position, current, message) {
101115
* @param {string} message error message
102116
* @param {WebIDL2ErrorOptions} [options]
103117
*/
104-
export function validationError(token, current, ruleName, message, options = {}) {
118+
export function validationError(
119+
token,
120+
current,
121+
ruleName,
122+
message,
123+
options = {}
124+
) {
105125
options.ruleName = ruleName;
106-
return error(current.source, token.index, current, message, "Validation", options);
126+
return error(
127+
current.source,
128+
token.index,
129+
current,
130+
message,
131+
"Validation",
132+
options
133+
);
107134
}

lib/productions/argument.js

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
import { Base } from "./base.js";
44
import { Default } from "./default.js";
55
import { ExtendedAttributes } from "./extended-attributes.js";
6-
import { unescape, type_with_extended_attributes, autoParenter, getFirstToken } from "./helpers.js";
6+
import {
7+
unescape,
8+
type_with_extended_attributes,
9+
autoParenter,
10+
getFirstToken,
11+
} from "./helpers.js";
712
import { argumentNameKeywords, Tokeniser } from "../tokeniser.js";
813
import { validationError } from "../error.js";
9-
import { idlTypeIncludesDictionary, dictionaryIncludesRequiredField } from "../validators/helpers.js";
14+
import {
15+
idlTypeIncludesDictionary,
16+
dictionaryIncludesRequiredField,
17+
} from "../validators/helpers.js";
1018

1119
export class Argument extends Base {
1220
/**
@@ -16,7 +24,9 @@ export class Argument extends Base {
1624
const start_position = tokeniser.position;
1725
/** @type {Base["tokens"]} */
1826
const tokens = {};
19-
const ret = autoParenter(new Argument({ source: tokeniser.source, tokens }));
27+
const ret = autoParenter(
28+
new Argument({ source: tokeniser.source, tokens })
29+
);
2030
ret.extAttrs = ExtendedAttributes.parse(tokeniser);
2131
tokens.optional = tokeniser.consume("optional");
2232
ret.idlType = type_with_extended_attributes(tokeniser, "argument-type");
@@ -52,23 +62,46 @@ export class Argument extends Base {
5262
*/
5363
*validate(defs) {
5464
yield* this.idlType.validate(defs);
55-
const result = idlTypeIncludesDictionary(this.idlType, defs, { useNullableInner: true });
65+
const result = idlTypeIncludesDictionary(this.idlType, defs, {
66+
useNullableInner: true,
67+
});
5668
if (result) {
5769
if (this.idlType.nullable) {
5870
const message = `Dictionary arguments cannot be nullable.`;
59-
yield validationError(this.tokens.name, this, "no-nullable-dict-arg", message);
71+
yield validationError(
72+
this.tokens.name,
73+
this,
74+
"no-nullable-dict-arg",
75+
message
76+
);
6077
} else if (!this.optional) {
61-
if (this.parent && !dictionaryIncludesRequiredField(result.dictionary, defs) && isLastRequiredArgument(this)) {
78+
if (
79+
this.parent &&
80+
!dictionaryIncludesRequiredField(result.dictionary, defs) &&
81+
isLastRequiredArgument(this)
82+
) {
6283
const message = `Dictionary argument must be optional if it has no required fields`;
63-
yield validationError(this.tokens.name, this, "dict-arg-optional", message, {
64-
autofix: autofixDictionaryArgumentOptionality(this)
65-
});
84+
yield validationError(
85+
this.tokens.name,
86+
this,
87+
"dict-arg-optional",
88+
message,
89+
{
90+
autofix: autofixDictionaryArgumentOptionality(this),
91+
}
92+
);
6693
}
6794
} else if (!this.default) {
6895
const message = `Optional dictionary arguments must have a default value of \`{}\`.`;
69-
yield validationError(this.tokens.name, this, "dict-arg-default", message, {
70-
autofix: autofixOptionalDictionaryDefaultValue(this)
71-
});
96+
yield validationError(
97+
this.tokens.name,
98+
this,
99+
"dict-arg-default",
100+
message,
101+
{
102+
autofix: autofixOptionalDictionaryDefaultValue(this),
103+
}
104+
);
72105
}
73106
}
74107
}
@@ -80,7 +113,7 @@ export class Argument extends Base {
80113
function isLastRequiredArgument(arg) {
81114
const list = arg.parent.arguments || arg.parent.list;
82115
const index = list.indexOf(arg);
83-
const requiredExists = list.slice(index + 1).some(a => !a.optional);
116+
const requiredExists = list.slice(index + 1).some((a) => !a.optional);
84117
return !requiredExists;
85118
}
86119

@@ -90,7 +123,11 @@ function isLastRequiredArgument(arg) {
90123
function autofixDictionaryArgumentOptionality(arg) {
91124
return () => {
92125
const firstToken = getFirstToken(arg.idlType);
93-
arg.tokens.optional = { type: "optional", value: "optional", trivia: firstToken.trivia };
126+
arg.tokens.optional = {
127+
type: "optional",
128+
value: "optional",
129+
trivia: firstToken.trivia,
130+
};
94131
firstToken.trivia = " ";
95132
autofixOptionalDictionaryDefaultValue(arg)();
96133
};

lib/productions/array-base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class ArrayBase extends Array {
66
Object.defineProperties(this, {
77
source: { value: source },
88
tokens: { value: tokens },
9-
parent: { value: null, writable: true }
9+
parent: { value: null, writable: true },
1010
});
1111
}
1212
}

lib/productions/attribute.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import { validationError } from "../error.js";
22
import { idlTypeIncludesDictionary } from "../validators/helpers.js";
33
import { Base } from "./base.js";
4-
import { type_with_extended_attributes, unescape, autoParenter } from "./helpers.js";
4+
import {
5+
type_with_extended_attributes,
6+
unescape,
7+
autoParenter,
8+
} from "./helpers.js";
59

610
export class Attribute extends Base {
711
/**
812
* @param {import("../tokeniser.js").Tokeniser} tokeniser
913
*/
10-
static parse(tokeniser, { special, noInherit = false, readonly = false } = {}) {
14+
static parse(
15+
tokeniser,
16+
{ special, noInherit = false, readonly = false } = {}
17+
) {
1118
const start_position = tokeniser.position;
1219
const tokens = { special };
13-
const ret = autoParenter(new Attribute({ source: tokeniser.source, tokens }));
20+
const ret = autoParenter(
21+
new Attribute({ source: tokeniser.source, tokens })
22+
);
1423
if (!special && !noInherit) {
1524
tokens.special = tokeniser.consume("inherit");
1625
}
@@ -26,9 +35,15 @@ export class Attribute extends Base {
2635
tokeniser.unconsume(start_position);
2736
return;
2837
}
29-
ret.idlType = type_with_extended_attributes(tokeniser, "attribute-type") || tokeniser.error("Attribute lacks a type");
30-
tokens.name = tokeniser.consume("identifier", "async", "required") || tokeniser.error("Attribute lacks a name");
31-
tokens.termination = tokeniser.consume(";") || tokeniser.error("Unterminated attribute, expected `;`");
38+
ret.idlType =
39+
type_with_extended_attributes(tokeniser, "attribute-type") ||
40+
tokeniser.error("Attribute lacks a type");
41+
tokens.name =
42+
tokeniser.consume("identifier", "async", "required") ||
43+
tokeniser.error("Attribute lacks a name");
44+
tokens.termination =
45+
tokeniser.consume(";") ||
46+
tokeniser.error("Unterminated attribute, expected `;`");
3247
return ret.this;
3348
}
3449

@@ -56,15 +71,26 @@ export class Attribute extends Base {
5671
case "sequence":
5772
case "record": {
5873
const message = `Attributes cannot accept ${this.idlType.generic} types.`;
59-
yield validationError(this.tokens.name, this, "attr-invalid-type", message);
74+
yield validationError(
75+
this.tokens.name,
76+
this,
77+
"attr-invalid-type",
78+
message
79+
);
6080
break;
6181
}
6282
default: {
63-
const { reference } = idlTypeIncludesDictionary(this.idlType, defs) || {};
83+
const { reference } =
84+
idlTypeIncludesDictionary(this.idlType, defs) || {};
6485
if (reference) {
6586
const targetToken = (this.union ? reference : this).tokens.base;
6687
const message = "Attributes cannot accept dictionary types.";
67-
yield validationError(targetToken, this, "attr-invalid-type", message);
88+
yield validationError(
89+
targetToken,
90+
this,
91+
"attr-invalid-type",
92+
message
93+
);
6894
}
6995
}
7096
}

lib/productions/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class Base {
1111
source: { value: source },
1212
tokens: { value: tokens, writable: true },
1313
parent: { value: null, writable: true },
14-
this: { value: this } // useful when escaping from proxy
14+
this: { value: this }, // useful when escaping from proxy
1515
});
1616
}
1717

lib/productions/callback-interface.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ export class CallbackInterface extends Container {
1414
if (!tokens.base) {
1515
return;
1616
}
17-
return Container.parse(tokeniser, new CallbackInterface({ source: tokeniser.source, tokens }), {
18-
type: "callback interface",
19-
inheritable: !partial,
20-
allowedMembers: [
21-
[Constant.parse],
22-
[Operation.parse, { regular: true }]
23-
]
24-
});
17+
return Container.parse(
18+
tokeniser,
19+
new CallbackInterface({ source: tokeniser.source, tokens }),
20+
{
21+
type: "callback interface",
22+
inheritable: !partial,
23+
allowedMembers: [
24+
[Constant.parse],
25+
[Operation.parse, { regular: true }],
26+
],
27+
}
28+
);
2529
}
2630

2731
get type() {

0 commit comments

Comments
 (0)