Skip to content

Commit a59b362

Browse files
committed
feat(rework): new layout
1 parent 43fd255 commit a59b362

23 files changed

+300
-161
lines changed

source/ast.ts

-56
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,3 @@ export * from './ast/ast-element-node';
44
export * from './ast/ast-location';
55
export * from './ast/ast-node';
66
export * from './ast/ast-text-node';
7-
8-
import { ASTElementAttribute } from './ast/ast-element-attribute';
9-
import { ASTElementNode } from './ast/ast-element-node';
10-
import { ASTTextNode } from './ast/ast-text-node';
11-
import { ASTLocation } from './ast/ast-location';
12-
import { ASTNode } from './ast/ast-node';
13-
import { ParserTask } from './parser-task';
14-
import { Parser } from './parser';
15-
16-
export class ASTGen extends ParserTask {
17-
public root: ASTNode = null;
18-
19-
constructor() { super(); }
20-
21-
init(parser: Parser, path?: string) {
22-
23-
var current = this.root = new ASTNode();
24-
25-
parser.on("startTag", (tag, attrs, selfClosing, loc) => {
26-
let next = new ASTElementNode();
27-
next.tag = tag;
28-
next.parent = current;
29-
next.location = <ASTLocation>{ start: loc.startOffset, end: loc.endOffset, line: loc.line, column: loc.col, path: path };
30-
next.attrs = attrs.map((x, i) => {
31-
var attr = new ASTElementAttribute();
32-
33-
attr.name = (x.prefix !== undefined && x.prefix != "") ? `${x.prefix}:${x.name}` : x.name;
34-
35-
var attrLoc = loc.attrs[attr.name] || loc.attrs[attr.name.toLowerCase()];
36-
37-
if (attrLoc == undefined)
38-
attrLoc = { startOffset: -1, endOffset: -1, line: -1, col: -1 };
39-
40-
attr.location = <ASTLocation>{ start: attrLoc.startOffset, end: attrLoc.endOffset, line: attrLoc.line, column: attrLoc.col, path: path };
41-
42-
return attr;
43-
});
44-
45-
current.children.push(next);
46-
47-
if (!parser.isVoid(tag))
48-
current = next;
49-
});
50-
51-
parser.on("endTag", (tag, attrs, selfClosing, loc) => {
52-
current = current.parent;
53-
});
54-
55-
parser.on("text", (text, loc) => {
56-
let child = new ASTTextNode();
57-
child.parent = current;
58-
child.location = <ASTLocation>{ start: loc.startOffset, end: loc.endOffset, line: loc.line, column: loc.col, path: path };
59-
current.children.push(child);
60-
});
61-
}
62-
}

source/ast/ast-element-node.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { ASTNode } from './ast-node';
22
import { ASTElementAttribute } from './ast-element-attribute';
3+
import { ClassDeclaration } from 'typescript';
34

45
export class ASTElementNode extends ASTNode {
5-
public tag: string;
6+
public name: string;
67
public namespace?: string;
78
public attrs: ASTElementAttribute[];
8-
9+
public typeValue: HTMLElement;
10+
public typeDecl: ClassDeclaration;
11+
912
constructor() {
1013
super();
1114
}

source/file-analyser.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { File } from './file';
2+
import { FileTask } from './file-task';
3+
4+
export class FileAnalyser {
5+
private chain: FileTask[];
6+
7+
constructor() {
8+
this.chain = [];
9+
}
10+
11+
use(task: FileTask) {
12+
this.chain.push(task);
13+
}
14+
15+
async analyse(file: File): Promise<File> {
16+
file = {
17+
content: file.content,
18+
kind: file.kind,
19+
path: file.path
20+
};
21+
22+
for (var task of this.chain) {
23+
let final = await task.process(file);
24+
if (final)
25+
break;
26+
}
27+
return file;
28+
}
29+
}

source/file-kind.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum FileKind{
2+
Source,
3+
Html
4+
}

source/file-result.ts

-9
This file was deleted.

source/file-task-chain.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { File } from './file';
2+
import { FileTask } from './file-task';
3+
4+
export class FileTaskChain implements FileTask {
5+
private chain: FileTask[];
6+
public isFinal: boolean;
7+
8+
constructor() {
9+
this.chain = [];
10+
this.isFinal = false;
11+
}
12+
13+
use(task: FileTask) {
14+
this.chain.push(task);
15+
}
16+
17+
async process(file: File): Promise<boolean> {
18+
for (var task of this.chain) {
19+
let final = await task.process(file);
20+
if (final)
21+
break;
22+
}
23+
return this.isFinal;
24+
}
25+
}

source/file-task.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { File } from './file';
2+
3+
export interface FileTask {
4+
/**
5+
* process and argment the file
6+
* @returns whether the process finalised the file
7+
*/
8+
process(file: File): Promise<boolean>;
9+
}

source/file.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
export interface File {
1+
import { Issue } from "./issue";
2+
import { ASTNode } from "./ast";
3+
import { File } from './file';
4+
import { FileKind } from './file-kind';
5+
export { FileKind } from './file-kind';
6+
7+
export interface File {
28
content: string;
9+
kind: FileKind;
10+
311
path?: string;
12+
13+
ext?: string;
14+
issues?: Issue[];
15+
imports?: string[];
416
}

source/index.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export * from './ast';
22
export * from './issue';
3-
export * from './linter';
4-
export * from './parser';
5-
export * from './parser-state';
6-
export * from './parser-task';
7-
export * from './reflection';
3+
export * from './file';
4+
export * from './file-task';
5+
export * from './file-analyser';

source/issue-severity.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Issue Severity
3+
*/
4+
export enum IssueSeverity {
5+
/* information on a better way */
6+
Info = 0,
7+
/* an issue that may result in odd results */
8+
Warning = 1,
9+
/* an issue that will result in missing/broken content */
10+
Error = 2,
11+
/* an issue that breaks the entire document/template */
12+
Fatal = 3
13+
}

source/issue.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IssueSeverity } from './issue-severity';
2+
export { IssueSeverity } from './issue-severity';
13
/**
24
* information about an issue
35
*/
@@ -11,16 +13,3 @@ export interface Issue {
1113
detail?: string;
1214
}
1315

14-
/**
15-
* Issue Severity
16-
*/
17-
export enum IssueSeverity {
18-
/* information on a better way */
19-
Info = 0,
20-
/* an issue that may result in odd results */
21-
Warning = 1,
22-
/* an issue that will result in missing/broken content */
23-
Error = 2,
24-
/* an issue that breaks the entire document/template */
25-
Fatal = 3
26-
}

source/options.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Options
2+
{
3+
}

source/parser-task.ts

-39
This file was deleted.

source/project-builder.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {ParserFileTask} from './tasks/parser-file-task';
2+
import {FileTaskChain} from './file-task-chain';
3+
import {Project} from './project';
4+
import {Options} from './options';
5+
6+
export class ProjectBuilder{
7+
build(opts: Options): Project{
8+
opts = opts || {};
9+
10+
var project = new Project();
11+
12+
project.use(this.buildHtmlChain(opts));
13+
14+
return project;
15+
}
16+
17+
private buildHtmlChain(opts: Options){
18+
var chain = new FileTaskChain();
19+
20+
chain.use(this.buildHtmlParser(opts));
21+
22+
return chain;
23+
}
24+
25+
26+
private buildHtmlParser(opts: Options){
27+
var parser = new ParserFileTask(opts);
28+
return parser;
29+
}
30+
}

source/project.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { SourceReflection } from './reflection/source-reflection';
2+
import { AureliaReflection } from './reflection/aurelia-reflection';
3+
import { FileAnalyser } from './file-analyser';
4+
import { FileTask } from './file-task';
5+
import { File } from './file';
6+
import * as path from 'path';
7+
8+
export class Project {
9+
private aureliaReflection: AureliaReflection;
10+
private sourceReflection: SourceReflection;
11+
private analyser: FileAnalyser;
12+
13+
constructor() {
14+
this.aureliaReflection = new AureliaReflection();
15+
this.sourceReflection = new SourceReflection();
16+
let analyser = new FileAnalyser();
17+
}
18+
19+
use(task: FileTask) {
20+
this.analyser.use(task);
21+
}
22+
23+
async process(file: File): Promise<File> {
24+
return await this.analyser.analyse(file);
25+
}
26+
}

source/reflection.ts renamed to source/reflection/source-reflection.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import * as glob from "glob";
44
import * as fs from "fs";
55

66
/*
7-
* Manage Reflection information for available sources
7+
* Manage Source Reflection information for available sources
88
*/
9-
export class Reflection {
9+
export class SourceReflection {
1010
public sourceFiles: ts.SourceFile[] = [];
1111
public pathToSource = {};
1212

source/rule.ts

-3
This file was deleted.

source/tasks/parser-file-task.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { FileTask } from '../file-task';
2+
import { File } from '../file';
3+
import { Options } from '../options';
4+
import { Parser } from './parser/parser';
5+
import { ParserState } from './parser/parser-state';
6+
import { ParserHook } from './parser/parser-hook';
7+
import { ASTGenerator } from './parser/hooks/ast-generator';
8+
9+
export class ParserFileTask implements FileTask {
10+
constructor(private opts: Options){
11+
}
12+
13+
async process(file: File): Promise<boolean> {
14+
15+
var parserState = new ParserState();
16+
var parser = new Parser(parserState);
17+
var hooks = [new ASTGenerator(this.opts)];
18+
19+
await parser.process(file, hooks);
20+
21+
return false;
22+
}
23+
}

0 commit comments

Comments
 (0)