Skip to content

Commit

Permalink
feat: markuplint-angular-parser supports v2 (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: yusukehirao <yusukehirao@me.com>
Co-authored-by: JounQin <admin@1stg.me>
  • Loading branch information
3 people authored Jan 19, 2022
1 parent 415ecd9 commit 5da0de0
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 73 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-garlics-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"markuplint-angular-parser": major
---

feat: markuplint-angular-parser supports v2
9 changes: 6 additions & 3 deletions packages/angular-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
"angular-linter"
],
"dependencies": {
"@markuplint/html-parser": "^1.7.3",
"@markuplint/ml-ast": "^1.6.0",
"@markuplint/parser-utils": "^1.6.3",
"@markuplint/html-parser": "^2.0.0-rc.3",
"@markuplint/ml-ast": "^2.0.0-rc.3",
"@markuplint/parser-utils": "^2.0.0-rc.3",
"angular-html-parser": "^1.8.0",
"tslib": "^2.3.1"
},
"devDependencies": {
"markuplint": "^2.0.0-rc.3"
},
"publishConfig": {
"access": "public"
}
Expand Down
25 changes: 14 additions & 11 deletions packages/angular-parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
MLASTElement,
MLASTElementCloseTag,
MLASTNode,
MLASTNodeType,
MLASTParentNode,
MLASTText,
MLToken,
Expand Down Expand Up @@ -149,7 +148,7 @@ const visitor = {

const startTag: MLASTElement = {
...partialStartTag,
type: MLASTNodeType.StartTag,
type: 'starttag',
nodeName,
namespace,
attributes,
Expand Down Expand Up @@ -184,7 +183,7 @@ const visitor = {
} else if (endTagText) {
startTag.pearNode = endTag = {
...nodeMapper(endSourceSpan!, options),
type: MLASTNodeType.EndTag,
type: 'endtag',
nodeName,
namespace,
attributes: [],
Expand Down Expand Up @@ -249,7 +248,8 @@ const visitor = {
const potentialValue = _value.replace(/(^{\s*{)|(}\s*}$)/g, '')

node.potentialName = potentialName
node.isInvalid =

const isInvalid =
![
plainName,
`#${plainName}`,
Expand All @@ -261,12 +261,16 @@ const visitor = {
![potentialValue, `{{${potentialValue}}}`].includes(_value) ||
(dynamicName && dynamicValue)

if (isInvalid) {
throw new Error('Parse error: It has invalid attribute')
}

nodeList.push(node)
},
visitText(text: Text, { nodeList, ...options }: VisitorContext) {
const node: MLASTText = {
...nodeMapper(text, options),
type: MLASTNodeType.Text,
type: 'text',
nodeName: '#text',
}
nodeList.push(node)
Expand All @@ -275,15 +279,15 @@ const visitor = {
// mark cdata as comment
const node: MLASTComment = {
...nodeMapper(cdata, options),
type: MLASTNodeType.Comment,
type: 'comment',
nodeName: '#comment',
}
nodeList.push(node)
},
visitComment(comment: Comment, { nodeList, ...options }: VisitorContext) {
const node: MLASTComment = {
...nodeMapper(comment, options),
type: MLASTNodeType.Comment,
type: 'comment',
nodeName: '#comment',
}
nodeList.push(node)
Expand All @@ -293,7 +297,7 @@ const visitor = {
const matched = DOCTYPE_REGEXP.exec(partialDocType.raw)
const node: MLASTDoctype = {
...partialDocType,
type: MLASTNodeType.Doctype,
type: 'doctype',
name: docType.value!.split(/\s/)[0],
nodeName: '#doctype',
publicId: matched?.[2] ?? '',
Expand Down Expand Up @@ -328,9 +332,8 @@ export const parse: Parse = text => {
nodeList: flattenNodes(nodeList, text),
isFragment: !nodeList.some(
node =>
node.type === MLASTNodeType.Doctype ||
(node.type === MLASTNodeType.StartTag &&
node.nodeName.toLowerCase() === 'html'),
node.type === 'doctype' ||
(node.type === 'starttag' && node.nodeName.toLowerCase() === 'html'),
),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Array [
Object {
"col": 19,
"line": 1,
"message": "Duplicate attribute name",
"message": "The attribute name is duplicated",
"raw": "[type]",
"ruleId": "attr-duplication",
"severity": "error",
},
Object {
"col": 39,
"line": 1,
"message": "Duplicate attribute name",
"message": "The attribute name is duplicated",
"raw": "[attr.type]",
"ruleId": "attr-duplication",
"severity": "error",
Expand All @@ -28,7 +28,7 @@ Array [
Object {
"col": 1,
"line": 1,
"message": "Element is deprecated",
"message": "The \\"font\\" element is deprecated",
"raw": "<font color=\\"red\\">",
"ruleId": "deprecated-element",
"severity": "error",
Expand Down
11 changes: 0 additions & 11 deletions packages/angular-parser/test/__snapshots__/parser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ Object {
},
"isDuplicatable": false,
"isDynamicValue": true,
"isInvalid": true,
"name": Object {
"endCol": 10,
"endLine": 1,
Expand Down Expand Up @@ -236,7 +235,6 @@ Object {
"startOffset": 28,
},
"isDuplicatable": false,
"isInvalid": true,
"name": Object {
"endCol": 28,
"endLine": 1,
Expand Down Expand Up @@ -321,7 +319,6 @@ Object {
"startOffset": 43,
},
"isDuplicatable": false,
"isInvalid": true,
"name": Object {
"endCol": 43,
"endLine": 1,
Expand Down Expand Up @@ -406,7 +403,6 @@ Object {
"startOffset": 57,
},
"isDuplicatable": false,
"isInvalid": true,
"name": Object {
"endCol": 57,
"endLine": 1,
Expand Down Expand Up @@ -640,7 +636,6 @@ Object {
"startOffset": 457,
},
"isDuplicatable": false,
"isInvalid": false,
"name": Object {
"endCol": 18,
"endLine": 8,
Expand Down Expand Up @@ -856,7 +851,6 @@ Object {
},
"isDuplicatable": false,
"isDynamicValue": true,
"isInvalid": false,
"name": Object {
"endCol": 25,
"endLine": 11,
Expand Down Expand Up @@ -993,7 +987,6 @@ Object {
"startOffset": 576,
},
"isDuplicatable": false,
"isInvalid": false,
"name": Object {
"endCol": 23,
"endLine": 12,
Expand Down Expand Up @@ -1079,7 +1072,6 @@ Object {
},
"isDuplicatable": false,
"isDynamicValue": true,
"isInvalid": false,
"name": Object {
"endCol": 41,
"endLine": 12,
Expand Down Expand Up @@ -1216,7 +1208,6 @@ Object {
"startOffset": 628,
},
"isDuplicatable": false,
"isInvalid": false,
"name": Object {
"endCol": 23,
"endLine": 13,
Expand Down Expand Up @@ -1302,7 +1293,6 @@ Object {
},
"isDuplicatable": false,
"isDynamicValue": true,
"isInvalid": false,
"name": Object {
"endCol": 40,
"endLine": 13,
Expand Down Expand Up @@ -1491,7 +1481,6 @@ Object {
},
"isDuplicatable": false,
"isDynamicValue": true,
"isInvalid": false,
"name": Object {
"endCol": 30,
"endLine": 15,
Expand Down
15 changes: 9 additions & 6 deletions packages/angular-parser/test/fixtures.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import path from 'path'
import { promisify } from 'util'

import { exec } from 'markuplint'
import glob from 'glob'
import { mlTestFile } from 'markuplint'

const asyncGlob = promisify(glob)

test('fixtures', async () => {
const resultInfos = await exec({
files: path.resolve(__dirname, 'fixtures/*.html'),
})
for (const { filePath, results } of resultInfos) {
expect(results).toMatchSnapshot(path.basename(filePath))
const files = await asyncGlob(path.resolve(__dirname, 'fixtures/*.html'))
for (const filePath of files) {
const { violations } = await mlTestFile(filePath)
expect(violations).toMatchSnapshot(path.basename(filePath))
}
})
Loading

0 comments on commit 5da0de0

Please sign in to comment.