Skip to content

Commit a8c7d75

Browse files
committed
Flagging imported definitions
1 parent f27d0d4 commit a8c7d75

File tree

6 files changed

+63
-57
lines changed

6 files changed

+63
-57
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wapc/widl",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"description": "waPC Interface Definition Language",
55
"keywords": [
66
"webassembly",

src/ast/definitions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface Definition {
1515
getKind(): Kind;
1616
isKind(kind: Kind): boolean;
1717
getLoc(): Location | undefined;
18+
imported: boolean;
1819
}
1920

2021
export interface Annotated {

src/ast/nodes.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface Node {
1313
export abstract class AbstractNode implements Node {
1414
kind: Kind;
1515
loc?: Location;
16+
imported: boolean = false;
1617

1718
constructor(kind: Kind, loc: Location | undefined) {
1819
this.kind = kind;

src/parser.ts

+56-53
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ class Parser {
165165
}
166166
const importDoc = parse(importSource, this._resolver, this._options);
167167
if (imp.all) {
168-
importDoc.definitions.map((def) => defs.push(def));
168+
importDoc.definitions.map((def) => {
169+
def.imported = true;
170+
defs.push(def);
171+
});
169172
} else {
170173
const allDefs = new Map<string, Definition>();
171174
importDoc.definitions.map((def) => {
@@ -205,65 +208,65 @@ class Parser {
205208
switch (def.getKind()) {
206209
case Kind.TypeDefinition:
207210
const type = def as TypeDefinition;
208-
defs.push(
209-
new TypeDefinition(
210-
name.loc,
211-
name,
212-
type.description,
213-
type.interfaces,
214-
type.annotations,
215-
type.fields
216-
)
211+
const renamedType = new TypeDefinition(
212+
name.loc,
213+
name,
214+
type.description,
215+
type.interfaces,
216+
type.annotations,
217+
type.fields
217218
);
219+
renamedType.imported = true;
220+
defs.push(renamedType);
218221
break;
219222
case Kind.EnumDefinition:
220223
const enumDef = def as EnumDefinition;
221-
defs.push(
222-
new EnumDefinition(
223-
name.loc,
224-
name,
225-
enumDef.description,
226-
enumDef.annotations,
227-
enumDef.values
228-
)
224+
const renamedEnum = new EnumDefinition(
225+
name.loc,
226+
name,
227+
enumDef.description,
228+
enumDef.annotations,
229+
enumDef.values
229230
);
231+
renamedEnum.imported = true;
232+
defs.push(renamedEnum);
230233
break;
231234
case Kind.UnionDefinition:
232235
const unionDef = def as UnionDefinition;
233-
defs.push(
234-
new UnionDefinition(
235-
name.loc,
236-
name,
237-
unionDef.description,
238-
unionDef.annotations,
239-
unionDef.types
240-
)
236+
const renamedUnion = new UnionDefinition(
237+
name.loc,
238+
name,
239+
unionDef.description,
240+
unionDef.annotations,
241+
unionDef.types
241242
);
243+
renamedUnion.imported = true;
244+
defs.push(renamedUnion);
242245
break;
243246
case Kind.DirectiveDefinition:
244247
const directive = def as DirectiveDefinition;
245-
defs.push(
246-
new DirectiveDefinition(
247-
name.loc,
248-
name,
249-
directive.description,
250-
directive.parameters,
251-
directive.locations,
252-
directive.requires
253-
)
248+
const renamedDirective = new DirectiveDefinition(
249+
name.loc,
250+
name,
251+
directive.description,
252+
directive.parameters,
253+
directive.locations,
254+
directive.requires
254255
);
256+
renamedDirective.imported = true;
257+
defs.push(renamedDirective);
255258
break;
256259
case Kind.AliasDefinition:
257260
const alias = def as AliasDefinition;
258-
defs.push(
259-
new AliasDefinition(
260-
name.loc,
261-
name,
262-
alias.description,
263-
alias.type,
264-
alias.annotations
265-
)
261+
const renamedAlias = new AliasDefinition(
262+
name.loc,
263+
name,
264+
alias.description,
265+
alias.type,
266+
alias.annotations
266267
);
268+
renamedAlias.imported = true;
269+
defs.push(renamedAlias);
267270
break;
268271
}
269272
});
@@ -565,7 +568,7 @@ class Parser {
565568
* - EnumTypeDefinition
566569
* - InputObjectTypeDefinition
567570
*/
568-
parseTypeSystemDefinition(): Node {
571+
parseTypeSystemDefinition(): Definition {
569572
// Many definitions begin with a description and require a lookahead.
570573
const keywordToken = this.peekDescription()
571574
? this._lexer.lookahead()
@@ -590,7 +593,7 @@ class Parser {
590593
case "interface":
591594
return this.parseInterfaceDefinition();
592595
case "role":
593-
return this.parseRoleTypeDefinition();
596+
return this.parseRoleDefinition();
594597
case "type":
595598
return this.parseTypeDefinition();
596599
case "union":
@@ -603,7 +606,7 @@ class Parser {
603606
throw this.unexpected(keywordToken);
604607
}
605608

606-
parseNamespaceDefinition(): Node {
609+
parseNamespaceDefinition(): NamespaceDefinition {
607610
let start = this._lexer.token;
608611
const description = this.parseDescription();
609612
this.expectKeyword("namespace");
@@ -629,7 +632,7 @@ class Parser {
629632
);
630633
}
631634

632-
parseImportDefinition(): Node {
635+
parseImportDefinition(): ImportDefinition {
633636
let start = this._lexer.token;
634637
const description = this.parseDescription();
635638
this.expectKeyword("import");
@@ -663,7 +666,7 @@ class Parser {
663666
);
664667
}
665668

666-
parseAliasDefinition(): Node {
669+
parseAliasDefinition(): AliasDefinition {
667670
let start = this._lexer.token;
668671
const description = this.parseDescription();
669672
this.expectKeyword("alias");
@@ -711,7 +714,7 @@ class Parser {
711714
* Description?
712715
* type Name ImplementsInterfaces? Annotations[Const]? FieldsDefinition?
713716
*/
714-
parseTypeDefinition(): Node {
717+
parseTypeDefinition(): TypeDefinition {
715718
const start = this._lexer.token;
716719
const description = this.parseDescription();
717720
this.expectKeyword("type");
@@ -857,7 +860,7 @@ class Parser {
857860
* InterfaceTypeDefinition :
858861
* - Description? interface Name Annotations[Const]? FieldsDefinition?
859862
*/
860-
parseInterfaceDefinition(): Node {
863+
parseInterfaceDefinition(): InterfaceDefinition {
861864
const start = this._lexer.token;
862865
const description = this.parseDescription();
863866
this.expectKeyword("interface"); // TODO
@@ -880,7 +883,7 @@ class Parser {
880883
* InterfaceTypeDefinition :
881884
* - Description? interface Name Annotations[Const]? FieldsDefinition?
882885
*/
883-
parseRoleTypeDefinition(): Node {
886+
parseRoleDefinition(): RoleDefinition {
884887
const start = this._lexer.token;
885888
const description = this.parseDescription();
886889
this.expectKeyword("role");
@@ -905,7 +908,7 @@ class Parser {
905908
* UnionTypeDefinition :
906909
* - Description? union Name Annotations[Const]? UnionMemberTypes?
907910
*/
908-
parseUnionDefinition(): Node {
911+
parseUnionDefinition(): UnionDefinition {
909912
const start = this._lexer.token;
910913
const description = this.parseDescription();
911914
this.expectKeyword("union");
@@ -940,7 +943,7 @@ class Parser {
940943
* EnumDefinition :
941944
* - Description? enum Name Annotations[Const]? EnumValuesDefinition?
942945
*/
943-
parseEnumDefinition(): Node {
946+
parseEnumDefinition(): EnumDefinition {
944947
const start = this._lexer.token;
945948
const description = this.parseDescription();
946949
this.expectKeyword("enum");

src/rules/namespace_first.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export class NamespaceFirst extends AbstractVisitor {
66
var firstNonImportPos = 0;
77
const definitions = context.document!.definitions;
88
for (let i = 0; i < definitions.length; i++) {
9-
if (!definitions[i].isKind(Kind.ImportDefinition)) {
9+
const def = definitions[i];
10+
if (!def.imported && !def.isKind(Kind.ImportDefinition)) {
1011
firstNonImportPos = i;
1112
break;
1213
}

0 commit comments

Comments
 (0)