Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* @license
* Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
'use strict';
import * as dom5 from 'dom5';
import * as url from 'url';
import * as estree from 'estree';
import * as docs from './ast-utils/docs';
import {FileLoader} from './loader/file-loader';
import {importParse, ParsedImport, LocNode} from './ast-utils/import-parse';
import {jsParse} from './ast-utils/js-parse';
import {Resolver} from './loader/resolver';
import {NoopResolver} from './loader/noop-resolver';
import {StringResolver} from './loader/string-resolver';
import {FSResolver} from './loader/fs-resolver';
import {XHRResolver} from './loader/xhr-resolver';
import {ErrorSwallowingFSResolver} from './loader/error-swallowing-fs-resolver';
import {Descriptor, ElementDescriptor, FeatureDescriptor, BehaviorDescriptor} from './ast-utils/descriptors';
function reduceMetadata(m1:DocumentDescriptor, m2:DocumentDescriptor): DocumentDescriptor {
return {
elements: m1.elements.concat(m2.elements),
features: m1.features.concat(m2.features),
behaviors: m1.behaviors.concat(m2.behaviors),
};
}
var EMPTY_METADATA: DocumentDescriptor = {elements: [], features: [], behaviors: []};
/**
* Package of a parsed JS script
*/
interface ParsedJS {
ast: estree.Program;
scriptElement: dom5.Node;
}
/**
* The metadata for all features and elements defined in one document
*/
interface DocumentDescriptor {
/**
* The elements from the document.
*/
elements: ElementDescriptor[];
/**
* The features from the document
*/
features: FeatureDescriptor[];
/**
* The behaviors from the document
*/
behaviors: BehaviorDescriptor[];
href?: string;
imports?: DocumentDescriptor[];
parsedScript?: estree.Program;
html?: ParsedImport;
}
/**
* The metadata of an entire HTML document, in promises.
*/
interface AnalyzedDocument {
/**
* The url of the document.
*/
href: string;
/**
* The parsed representation of the doc. Use the `ast` property to get
* the full `parse5` ast.
*/
htmlLoaded: Promise;
/**
* Resolves to the list of this Document's transitive import dependencies.
*/
depsLoaded: Promise;
/**
* The direct dependencies of the document.
*/
depHrefs: string[];
/**
* Resolves to the list of this Document's import dependencies
*/
metadataLoaded: Promise;
}
/**
* Options for `Analyzer.analzye`
*/
interface LoadOptions {
/**
* Whether `annotate()` should be skipped.
*/
noAnnotations?: boolean;
/**
* Content to resolve `href` to instead of loading from the file system.
*/
content?: string;
/**
* Whether the generated descriptors should be cleaned of redundant data.
*/
clean?: boolean;
/**
* `xhr` to use XMLHttpRequest.
* `fs` to use the local filesystem.
* `permissive` to use the local filesystem and return empty files when a
* path can't be found.
* Default is `fs` in node and `xhr` in the browser.
*/
resolver?: string;
/**
* A predicate function that indicates which files should be ignored by
* the loader. By default all files not located under the dirname
* of `href` will be ignored.
*/
filter?: (path:string)=> boolean;
}
/**
* An Error extended with location metadata.
*/
interface LocError extends Error{
location: {line: number; column: number};
ownerDocument: string;
}
/**
* A database of Polymer metadata defined in HTML
*/
export class Analyzer {
loader: FileLoader;
/**
* A list of all elements the `Analyzer` has metadata for.
*/
elements: ElementDescriptor[] = [];
/**
* A view into `elements`, keyed by tag name.
*/
elementsByTagName: {[tagName: string]: ElementDescriptor} = {};
/**
* A list of API features added to `Polymer.Base` encountered by the
* analyzer.
*/
features: FeatureDescriptor[] = [];
/**
* The behaviors collected by the analysis pass.
*/
behaviors: BehaviorDescriptor[] = [];
/**
* The behaviors collected by the analysis pass by name.
*/
behaviorsByName: {[name:string]: BehaviorDescriptor} = {};
/**
* A map, keyed by absolute path, of Document metadata.
*/
html: {[path: string]: AnalyzedDocument} = {};
/**
* A map, keyed by path, of HTML document ASTs.
*/
parsedDocuments: {[path:string]: dom5.Node} = {};
/**
* A map, keyed by path, of JS script ASTs.
*
* If the path is an HTML file with multiple scripts,
* the entry will be an array of scripts.
*/
parsedScripts: {[path:string]: ParsedJS[]} = {};
/**
* A map, keyed by path, of document content.
*/
_content: {[path:string]: string} = {};
/**
* @param {boolean} attachAST If true, attach a parse5 compliant AST
* @param {FileLoader=} loader An optional `FileLoader` used to load external
* resources
*/
constructor(attachAST:boolean, loader:FileLoader) {
this.loader = loader;
}
/**
* Shorthand for transitively loading and processing all imports beginning at
* `href`.
*
* In order to properly filter paths, `href` _must_ be an absolute URI.
*
* @param {string} href The root import to begin loading from.
* @param {LoadOptions=} options Any additional options for the load.
* @return {Promise} A promise that will resolve once `href` and its
* dependencies have been loaded and analyzed.
*/
static analyze = function analyze(href:string, options?:LoadOptions) {
options = options || {};
options.filter = options.filter || _defaultFilter(href);
var loader = new FileLoader();
var resolver = options.resolver;
if (resolver === undefined) {
if (typeof window === 'undefined') {
resolver = 'fs';
} else {
resolver = 'xhr';
}
}
let primaryResolver: Resolver;
if (resolver === 'fs') {
primaryResolver = new FSResolver(options);
} else if (resolver === 'xhr') {
primaryResolver = new XHRResolver(options);
} else if (resolver === 'permissive') {
primaryResolver = new ErrorSwallowingFSResolver(options);
} else {
throw new Error("Resolver must be one of 'fs', 'xhr', or 'permissive'");
}
loader.addResolver(primaryResolver);
if (options.content) {
loader.addResolver(new StringResolver({url: href, content: options.content}));
}
loader.addResolver(new NoopResolver({test: options.filter}));
var analyzer = new Analyzer(false, loader);
return analyzer.metadataTree(href).then((root) => {
if (!options.noAnnotations) {
analyzer.annotate();
}
if (options.clean) {
analyzer.clean();
}
return Promise.resolve(analyzer);
});
};
load(href: string):Promise {
return this.loader.request(href).then((content) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
this._content[href] = content;
resolve(this._parseHTML(content, href));
}, 0);
}).catch(function(err){
console.error("Error processing document at " + href);
throw err;
});
});
};
/**
* Returns an `AnalyzedDocument` representing the provided document
* @private
* @param {string} htmlImport Raw text of an HTML document
* @param {string} href The document's URL.
* @return {AnalyzedDocument} An `AnalyzedDocument`
*/
_parseHTML(htmlImport: string, href: string):AnalyzedDocument {
if (href in this.html) {
return this.html[href];
}
var depsLoaded: Promise