Skip to content
This repository was archived by the owner on Sep 24, 2021. It is now read-only.

Initial commit of Analyzer-based Linter. Ported over first linter 'unbalanced-delimiters' #1

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/

'use strict';

import {Analyzer} from 'polymer-analyzer';
import {Document} from 'polymer-analyzer/lib/model/document';
import {FSUrlLoader} from 'polymer-analyzer/lib/url-loader/fs-url-loader';
import {Warning} from 'polymer-analyzer/lib/warning/warning';
import {Warning, WarningCarryingException} from 'polymer-analyzer/lib/warning/warning';
import {Rule} from './rule';

/**
* The Linter is a simple class which groups together a set of Rules and applies
* them to a set of file urls which can be resolved and loaded by the provided
* Analyzer. A default Analyzer is prepared if one is not provided.
*/
export class Linter {
public analyzer: Analyzer;
public rules: Rule[];
Expand All @@ -36,9 +38,18 @@ export class Linter {
public async lint(files: string[]): Promise<Warning[]> {
let warnings: Warning[] = [];
for (const file of files) {
const document: Document = await this.analyzer.analyze(file);
for (const rule of this.rules) {
warnings = warnings.concat(await rule.check(document));
let document: Document;
try {
document = await this.analyzer.analyze(file);
for (const rule of this.rules) {
warnings = warnings.concat(await rule.check(document));
}
} catch (error) {
if (error instanceof WarningCarryingException) {
warnings.push(error.warning);
continue;
}
throw error;
}
}
return warnings;
Expand Down
52 changes: 52 additions & 0 deletions src/test/linter_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license
* Copyright (c) 2014 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
*/
import * as assert from 'assert';
import {Document} from 'polymer-analyzer/lib/model/document';
import {Severity, Warning, WarningCarryingException} from 'polymer-analyzer/lib/warning/warning';
import {Linter} from '../linter';
import {Rule} from '../rule';

suite('Linter', () => {

it('catches exceptions during Analyze and presents as Warnings', async() => {
const explodingLinter = class implements Rule {
public async check(document: Document): Promise<Warning[]> {
throw new WarningCarryingException({
code: 'exploding-linter-exploded',
message: 'The exploding linter exploded.',
severity: Severity.ERROR,
sourceRange: {
end: {column: 0, line: 0},
file: document.url,
start: {column: 0, line: 0}
}
});
}
};
const linter = new Linter([new explodingLinter()]);
const warnings =
await linter.lint(['test/sample/my-element-collection.html']);
assert.equal(warnings.length, 1);
assert.deepEqual(warnings[0], {
code: 'exploding-linter-exploded',
message: 'The exploding linter exploded.',
severity: Severity.ERROR,
sourceRange: {
end: {column: 0, line: 0},
file: 'test/sample/my-element-collection.html',
start: {column: 0, line: 0}
}
});
});
});
17 changes: 17 additions & 0 deletions test/sample/my-element-collection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<link rel="import" href="../../polymer/polymer.html">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used in a later commit?

<link rel="import" href="imports/a11y-attributes.html">
<link rel="import" href="imports/bound-variables-declared.html">
<link rel="import" href="imports/bind-to-class.html">
<link rel="import" href="imports/bind-to-data.html">
<link rel="import" href="imports/compound-binding.html">
<link rel="import" href="imports/computed-binding.html">
<link rel="import" href="imports/dom-module-after-polymer.html">
<link rel="import" href="imports/element-not-defined.html">
<link rel="import" href="imports/external-script-error.html">
<link rel="import" href="imports/implicit-properties.html">
<link rel="import" href="imports/missing-is.html">
<link rel="import" href="imports/number-literals.html">
<link rel="import" href="imports/observer-not-function.html">
<link rel="import" href="imports/string-literals.html">
<link rel="import" href="imports/unbalanced-delimiters.html">
<link rel="import" href="imports/whitespace.html">