Skip to content

Commit

Permalink
Add parse-error to result
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuke Hirao committed Apr 23, 2020
1 parent e33f00c commit 2b211ce
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
17 changes: 17 additions & 0 deletions packages/markuplint/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,21 @@ describe('basic test', () => {
},
]);
});

it('is reported from 006.html', async () => {
const r = await markuplint.exec({
files: 'test/fixture/006.html',
locale: 'en',
});
expect(r[0].results).toEqual([
{
severity: 'error',
message: 'Invalid element',
line: 7,
col: 6,
raw: '<a>',
ruleId: 'parse-error',
},
]);
});
});
34 changes: 28 additions & 6 deletions packages/markuplint/src/lint-file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ConfigSet, MLFile } from '@markuplint/file-resolver';
import { MLCore, MLRule, convertRuleset } from '@markuplint/ml-core';
import { MLCore, MLParseError, MLRule, convertRuleset } from '@markuplint/ml-core';
import { RuleConfigValue, VerifiedResult } from '@markuplint/ml-config';
import { Document } from '@markuplint/ml-core';
import { MLMarkupLanguageParser } from '@markuplint/ml-ast';
import { MLResultInfo } from './types';
import { RuleConfigValue } from '@markuplint/ml-config';
import { i18n } from './i18n';
import { moduleAutoLoader } from './module-auto-loader';
import path from 'path';
Expand Down Expand Up @@ -45,16 +46,37 @@ export async function lintFile(
// create MLCore
const sourceCode = await file.getContext();
const i18nSettings = await i18n(locale);
const core = new MLCore(parser, sourceCode, ruleset, rules, i18nSettings);

const results = await core.verify(fix);
let results: VerifiedResult[] = [];
let fixedCode = sourceCode;
let document: Document<RuleConfigValue, unknown> | null = null;

try {
const core = new MLCore(parser, sourceCode, ruleset, rules, i18nSettings);
results = await core.verify(fix);
fixedCode = core.document.toString();
document = core.document;
} catch (err) {
if (err instanceof MLParseError) {
results = [
{
ruleId: 'parse-error',
severity: 'error',
message: err.message,
col: err.col,
line: err.line,
raw: err.raw,
},
];
}
}

return {
results,
filePath: file.path,
sourceCode,
fixedCode: core.document.toString(),
document: core.document,
fixedCode,
document,
parser: parserModName,
locale,
ruleset,
Expand Down
2 changes: 1 addition & 1 deletion packages/markuplint/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface MLResultInfo {
filePath: string;
sourceCode: string;
fixedCode: string;
document: Document<any, unknown>;
document: Document<any, unknown> | null;
parser: string;
locale?: string;
ruleset: Ruleset;
Expand Down
9 changes: 9 additions & 0 deletions test/fixture/006.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<p><a>non-close-tag</p>
</body>
</html>

0 comments on commit 2b211ce

Please sign in to comment.