From 2bd7554a765eb3d7a897c3abf26f822dff238e5c Mon Sep 17 00:00:00 2001 From: Dan Phillimore Date: Sun, 28 May 2023 09:16:52 +0100 Subject: [PATCH] Add "mixed" and "object" types --- src/grammar.js | 8 ++++ test/integration/types/mixedTypeTest.js | 57 ++++++++++++++++++++++++ test/integration/types/objectTypeTest.js | 57 ++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 test/integration/types/mixedTypeTest.js create mode 100644 test/integration/types/objectTypeTest.js diff --git a/src/grammar.js b/src/grammar.js index 5be34eb..6a0e0b3 100644 --- a/src/grammar.js +++ b/src/grammar.js @@ -2077,9 +2077,15 @@ module.exports = { 'N_ITERABLE_TYPE': { components: [{allowMerge: false, what: /iterable\b/i}] }, + 'N_MIXED_TYPE': { + components: [{allowMerge: false, what: /mixed\b/i}] + }, 'N_NULL_TYPE': { components: [{allowMerge: false, what: /null\b/i}] }, + 'N_OBJECT_TYPE': { + components: [{allowMerge: false, what: /object\b/i}] + }, 'N_TYPE': { components: [ {oneOf: ['N_UNION_TYPE', 'N_NON_UNION_TYPE']} @@ -2119,7 +2125,9 @@ module.exports = { 'N_ARRAY_TYPE', 'N_CALLABLE_TYPE', 'N_ITERABLE_TYPE', + 'N_MIXED_TYPE', 'N_NULL_TYPE', + 'N_OBJECT_TYPE', 'N_SCALAR_TYPE', 'N_VOID_TYPE', 'N_CLASS_TYPE' diff --git a/test/integration/types/mixedTypeTest.js b/test/integration/types/mixedTypeTest.js new file mode 100644 index 0000000..dc93c4f --- /dev/null +++ b/test/integration/types/mixedTypeTest.js @@ -0,0 +1,57 @@ +/* + * PHP-To-AST - PHP parser + * Copyright (c) Dan Phillimore (asmblah) + * http://uniter.github.com/phptoast/ + * + * Released under the MIT license + * https://github.com/uniter/phptoast/raw/master/MIT-LICENSE.txt + */ + +'use strict'; + +var _ = require('microdash'), + expect = require('chai').expect, + tools = require('../../tools'); + +describe('PHP Parser grammar mixed type integration', function () { + var parser; + + beforeEach(function () { + parser = tools.createParser(); + }); + + _.each({ + 'empty function definition with explicit mixed return type': { + code: 'function myFunc() : mixed {}', + expectedAST: { + name: 'N_PROGRAM', + statements: [{ + name: 'N_FUNCTION_STATEMENT', + func: { + name: 'N_STRING', + string: 'myFunc' + }, + args: [], + body: { + name: 'N_COMPOUND_STATEMENT', + statements: [] + }, + returnType: { + name: 'N_MIXED_TYPE' + } + }] + } + } + }, function (scenario, description) { + describe(description, function () { + var code = '', function () { + it('should return the expected AST', function () { + expect(parser.parse(code)).to.deep.equal(scenario.expectedAST); + }); + }); + }); + }); +}); diff --git a/test/integration/types/objectTypeTest.js b/test/integration/types/objectTypeTest.js new file mode 100644 index 0000000..e991b49 --- /dev/null +++ b/test/integration/types/objectTypeTest.js @@ -0,0 +1,57 @@ +/* + * PHP-To-AST - PHP parser + * Copyright (c) Dan Phillimore (asmblah) + * http://uniter.github.com/phptoast/ + * + * Released under the MIT license + * https://github.com/uniter/phptoast/raw/master/MIT-LICENSE.txt + */ + +'use strict'; + +var _ = require('microdash'), + expect = require('chai').expect, + tools = require('../../tools'); + +describe('PHP Parser grammar object type integration', function () { + var parser; + + beforeEach(function () { + parser = tools.createParser(); + }); + + _.each({ + 'empty function definition with explicit object return type': { + code: 'function myFunc() : object {}', + expectedAST: { + name: 'N_PROGRAM', + statements: [{ + name: 'N_FUNCTION_STATEMENT', + func: { + name: 'N_STRING', + string: 'myFunc' + }, + args: [], + body: { + name: 'N_COMPOUND_STATEMENT', + statements: [] + }, + returnType: { + name: 'N_OBJECT_TYPE' + } + }] + } + } + }, function (scenario, description) { + describe(description, function () { + var code = '', function () { + it('should return the expected AST', function () { + expect(parser.parse(code)).to.deep.equal(scenario.expectedAST); + }); + }); + }); + }); +});