Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

properly set sourceRange and astNode on inline documents #480

Merged
merged 3 commits into from Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/core/analysis-context.ts
Expand Up @@ -385,6 +385,11 @@ export class AnalysisContext {
const scannedDoc =
await this._scanDocument(parsedDoc, feature.attachedComment);

// Set these inline-only properties here manually since _scanDocument
// doesn't set them directly.
scannedDoc.sourceRange = feature.sourceRange;
scannedDoc.astNode = feature.astNode;

feature.scannedDocument = scannedDoc;
} catch (err) {
if (err instanceof WarningCarryingException) {
Expand Down
25 changes: 17 additions & 8 deletions src/model/document.ts
Expand Up @@ -11,6 +11,7 @@
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import * as dom5 from 'dom5';

import {AnalysisContext} from '../core/analysis-context';
import {ParsedDocument} from '../parser/document';
Expand All @@ -36,9 +37,15 @@ import {SourceRange} from './source-range';
export class ScannedDocument {
document: ParsedDocument<any, any>;
features: ScannedFeature[];
isInline = false;
sourceRange: SourceRange|undefined = undefined; // TODO(rictic): track this
warnings: Warning[];
isInline = false;

// A scanned document has no `sourceRange` or `astNode` by default. However,
// they can be set later if the ScannedDocument represents an inline doc.
// TODO(fks): Inherit these two properties from ParsedDocument so that they do
// not have to be set awkwardly after the fact.
sourceRange: SourceRange|undefined = undefined;
astNode: dom5.Node|undefined = undefined;

constructor(
document: ParsedDocument<any, any>, features: ScannedFeature[],
Expand Down Expand Up @@ -123,8 +130,6 @@ export class Document implements Feature, Queryable {
private _localFeatures = new Set<Feature>();
private _scannedDocument: ScannedDocument;

/** See parsedDocument. */
astNode: null = null;

/**
* To handle recursive dependency graphs we must track whether we've started
Expand Down Expand Up @@ -166,14 +171,18 @@ export class Document implements Feature, Queryable {
return this._scannedDocument.isInline;
}

get parsedDocument(): ParsedDocument<any, any> {
return this._scannedDocument.document;
}

get sourceRange(): SourceRange|undefined {
return this._scannedDocument.sourceRange;
}

get astNode(): dom5.Node|undefined {
return this._scannedDocument.astNode;
}

get parsedDocument(): ParsedDocument<any, any> {
return this._scannedDocument.document;
}

get resolved(): boolean {
return this._doneResolving;
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/analyzer_test.ts
Expand Up @@ -87,6 +87,44 @@ suite('Analyzer', () => {
assert.deepEqual(elements.map((e) => e.tagName), ['my-element']);
});

test('analyzes inline scripts correctly', async() => {
const document = await analyzer.analyze(
'static/inline-documents/inline-documents.html');
const jsDocuments = document.getByKind('js-document');
assert.equal(jsDocuments.size, 1);
const jsDocument = jsDocuments.values().next().value;
assert.isObject(jsDocument.astNode);
assert.equal(jsDocument.astNode!.tagName, 'script');
assert.deepEqual(await underliner.underline(jsDocument.sourceRange), `
<script>
~~~~~~~~
console.log(\'hi\');
~~~~~~~~~~~~~~~~~~~~~~
</script>
~~~~~~~~~~~`);
});

test('analyzes inline styles correctly', async() => {
const document = await analyzer.analyze(
'static/inline-documents/inline-documents.html');
const cssDocuments = document.getByKind('css-document');
assert.equal(cssDocuments.size, 1);
const cssDocument = cssDocuments.values().next().value;
assert.isObject(cssDocument.astNode);
assert.equal(cssDocument.astNode!.tagName, 'style');
assert.deepEqual(await underliner.underline(cssDocument.sourceRange), `
<style>
~~~~~~~
body {
~~~~~~~~~~
color: red;
~~~~~~~~~~~~~~~~~
}
~~~~~
</style>
~~~~~~~~~~`);
});

test('analyzes a document with an import', async() => {
const document =
await analyzer.analyze('static/analysis/behaviors/behavior.html');
Expand Down
14 changes: 14 additions & 0 deletions src/test/static/inline-documents/inline-documents.html
@@ -0,0 +1,14 @@
<html>

<head>
<script>
console.log('hi');
</script>
<style>
body {
color: red;
}
</style>
</head>

</html>