Skip to content

Commit

Permalink
hand constant with no type in 8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
genintho committed Dec 4, 2024
1 parent 929f8b9 commit b3bb86b
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/parser/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,18 @@ module.exports = {
this.next();
}

const [nullable, type] =
this.version >= 803 ? this.read_optional_type() : [false, null];
let type = null;
let nullable = false;
if (this.version >= 803) {
[nullable, type] = this.read_optional_type();
}
let name = null;

// read_optional_type can return a "type" even if the constant does not actually have a type.
if (type && type.kind === "name" && this.token === "=") {
name = type.name;
type = null;
}

const result = this.node("classconstant");
const items = this.read_list(
Expand All @@ -256,9 +266,14 @@ module.exports = {
(this.version >= 700 && this.is("IDENTIFIER"))
) {
constName = this.node("identifier");
const name = this.text();
name = this.text();
this.next();
constName = constName(name);
}
// read_optional_type is not always returning just a type but
else if (name) {
constName = this.node("identifier")(name);
name = null;
} else {
this.expect("IDENTIFIER");
}
Expand Down
112 changes: 112 additions & 0 deletions test/snapshot/__snapshots__/classconstant.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,69 @@ Program {
}
`;

exports[`classconstant multiple 8.3 1`] = `
Program {
"children": [
Class {
"attrGroups": [],
"body": [
ClassConstant {
"attrGroups": [],
"constants": [
Constant {
"kind": "constant",
"name": Identifier {
"kind": "identifier",
"name": "NAME_1",
},
"value": String {
"isDoubleQuote": true,
"kind": "string",
"raw": ""Hello world!"",
"unicode": false,
"value": "Hello world!",
},
},
Constant {
"kind": "constant",
"name": Identifier {
"kind": "identifier",
"name": "NAME_2",
},
"value": String {
"isDoubleQuote": true,
"kind": "string",
"raw": ""Other hello world!"",
"unicode": false,
"value": "Other hello world!",
},
},
],
"final": false,
"kind": "classconstant",
"nullable": false,
"type": null,
"visibility": "",
},
],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
"name": "Foo",
},
},
],
"errors": [],
"kind": "program",
}
`;

exports[`classconstant private 1`] = `
Program {
"children": [
Expand Down Expand Up @@ -308,6 +371,55 @@ Program {
}
`;

exports[`classconstant simple using 8.3 1`] = `
Program {
"children": [
Class {
"attrGroups": [],
"body": [
ClassConstant {
"attrGroups": [],
"constants": [
Constant {
"kind": "constant",
"name": Identifier {
"kind": "identifier",
"name": "CONSTANT",
},
"value": String {
"isDoubleQuote": true,
"kind": "string",
"raw": ""Hello world!"",
"unicode": false,
"value": "Hello world!",
},
},
],
"final": false,
"kind": "classconstant",
"nullable": false,
"type": null,
"visibility": "",
},
],
"extends": null,
"implements": null,
"isAbstract": false,
"isAnonymous": false,
"isFinal": false,
"isReadonly": false,
"kind": "class",
"name": Identifier {
"kind": "identifier",
"name": "Foo",
},
},
],
"errors": [],
"kind": "program",
}
`;

exports[`classconstant type hinted (supported) 1`] = `
Program {
"children": [
Expand Down
20 changes: 20 additions & 0 deletions test/snapshot/classconstant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,33 @@ describe("classconstant", () => {
parser.parseEval('class Foo { const CONSTANT = "Hello world!"; }'),
).toMatchSnapshot();
});
it("simple using 8.3", () => {
expect(
parser.parseEval(`class Foo { const CONSTANT = "Hello world!"; }`, {
parser: { version: 803 },
}),
).toMatchSnapshot();
});

it("multiple", () => {
expect(
parser.parseEval(
'class Foo { const CONSTANT = "Hello world!", OTHER_CONSTANT = "Other hello world!"; }',
),
).toMatchSnapshot();
});

it("multiple 8.3", () => {
expect(
parser.parseEval(
'class Foo { const NAME_1 = "Hello world!", NAME_2 = "Other hello world!"; }',
{
parser: { version: 803 },
},
),
).toMatchSnapshot();
});

it("public", () => {
expect(
parser.parseEval('class Foo { public const CONSTANT = "Hello world!"; }'),
Expand Down

0 comments on commit b3bb86b

Please sign in to comment.