Skip to content

Commit

Permalink
Add a logic to process interface and partial interface (#222)
Browse files Browse the repository at this point in the history
After this patch, `DefinitionInfoMap` can process `interface` and
`partial interface`. Other definitions will be ignored for now.

ISSUE=#207
  • Loading branch information
romandev committed Dec 20, 2017
1 parent 122c7ca commit a74fce9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
38 changes: 36 additions & 2 deletions generator/new_parser/definition_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,40 @@
* limitations under the License.
*/

export interface DefinitionInfo {
name: string;
export interface TypeInfo {
readonly sequence: boolean;
readonly generic?: string;
readonly idlType: string;
readonly nullable: boolean;
readonly union: boolean;
}

export interface ArgumentInfo {
readonly optional: boolean;
readonly variadic: boolean;
readonly idlType: TypeInfo;
readonly name: string;
}

export interface InterfaceInfo {
readonly type: 'interface';
readonly name: string;
partial: boolean;
members: InterfaceMemberInfo[];
inheritance: string;
}

export interface OperationMemberInfo {
readonly type: 'operation';
readonly getter: boolean;
readonly setter: boolean;
readonly deleter: boolean;
readonly static: boolean;
readonly stringifier: boolean;
readonly idlType: TypeInfo;
readonly name: string;
readonly arguments: ArgumentInfo[];
}

export type InterfaceMemberInfo = OperationMemberInfo;
export type DefinitionInfo = InterfaceInfo;
34 changes: 31 additions & 3 deletions generator/new_parser/definition_info_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,49 @@
* limitations under the License.
*/

import {DefinitionInfo} from 'generator/new_parser/definition_info';
import {
DefinitionInfo, InterfaceInfo
} from 'generator/new_parser/definition_info';

interface DefinitionInfoStore {
[index: string]: DefinitionInfo;
}

const store: DefinitionInfoStore = {};

function updateInterfaceInfo(info: InterfaceInfo): void {
const storedInfo: DefinitionInfo = store[info.name];

if (storedInfo === undefined) {
store[info.name] = info;

return;
}

if (storedInfo.type !== info.type) {
throw new SyntaxError('IDL defintions are duplicated');
}

// In this case, one of thing is partial interface. So, we should merge them.
storedInfo.partial = false;
storedInfo.inheritance = storedInfo.inheritance || info.inheritance;
storedInfo.members = storedInfo.members.concat(info.members);
}

function updateDefinitionInfo(info: DefinitionInfo): void {
switch (info.type) {
case 'interface': updateInterfaceInfo(info as InterfaceInfo); break;
default:
}
}

/**
* Raw definition information mapping table (exposed to global scope)
*/
export class DefinitionInfoMap {
public static update(definitionInfos: DefinitionInfo[]): void {
definitionInfos.forEach((definitionInfo: DefinitionInfo) => {
store[definitionInfo.name] = definitionInfo;
definitionInfos.forEach((info: DefinitionInfo) => {
updateDefinitionInfo(info);
});
}

Expand Down
9 changes: 4 additions & 5 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
"interface-name": false,
"match-default-export-name": false,
"max-line-length": [true, 80],
"no-banned-terms": false,
"no-reserved-keywords": false,
"no-stateless-class": false,
"no-suspicious-comment": false,
"no-unsafe-any": false,
"strict-boolean-expressions": [
true,
"allow-null-union",
"allow-undefined-union"
],
"prefer-type-cast": false,
"strict-boolean-expressions": false,
"typedef": [
true,
"call-signature",
Expand Down

0 comments on commit a74fce9

Please sign in to comment.