Skip to content

Commit d4cae6e

Browse files
Fernando G. Vilarfkling
Fernando G. Vilar
authored andcommitted
* feat(parsers): Pug support * bug(pug): fixed `getNodeName` logic * Fix eslint issues
1 parent afb16cd commit d4cae6e

File tree

6 files changed

+137
-1
lines changed

6 files changed

+137
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ The AST explorer provides following code parsers:
5252
- [mdxhast][]
5353
- PHP
5454
- [php-parser][]
55+
- [Pug][]
5556
- Regular Expressions:
5657
- [regexp-tree][]
5758
- [regjsparser][]
@@ -152,6 +153,7 @@ node.
152153
[regexp-tree]: https://github.com/DmitrySoshnikov/regexp-tree
153154
[regjsparser]: https://github.com/jviereck/regjsparser
154155
[php-parser]: https://github.com/glayzzle/php-parser
156+
[pug]: https://github.com/pugjs/pug
155157
[glimmer]: https://github.com/glimmerjs/glimmer-vm
156158
[handlebars]: http://handlebarsjs.com/
157159
[icu]: https://github.com/formatjs/formatjs/tree/master/packages/intl-messageformat-parser

website/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@
117117
"prop-type": "^0.0.1",
118118
"prop-types": "^15.5.10",
119119
"pubsub-js": "^1.4.2",
120+
"pug-lexer": "^4.1.0",
121+
"pug-parser": "^5.0.1",
120122
"query-string": "5",
121123
"react": "16",
122124
"react-dom": "^16.0.1",
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
doctype html
2+
html(lang='en')
3+
head
4+
title Pug
5+
script(type='text/javascript').
6+
const foo = true;
7+
let bar = function() {};
8+
if (foo) {
9+
bar(1 + 5)
10+
}
11+
body
12+
h1 Pug - node template engine
13+
#container.col
14+
p You are amazing
15+
p
16+
| Pug is a terse and simple
17+
| templating language with a
18+
| strong focus on performance
19+
| and powerful features.

website/src/parsers/pug/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import 'codemirror/mode/pug/pug';
2+
3+
export const id = 'pug';
4+
export const displayName = 'Pug';
5+
export const mimeTypes = [];
6+
export const fileExtension = 'pug';

website/src/parsers/pug/pug.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import defaultParserInterface from '../utils/defaultParserInterface';
2+
import pkg from 'pug-parser/package.json';
3+
4+
const ID = 'pug';
5+
6+
export default {
7+
...defaultParserInterface,
8+
9+
id: ID,
10+
displayName: ID,
11+
version: pkg.version,
12+
homepage: pkg.homepage || 'https://github.com/pugjs/pug',
13+
typeProps: new Set(['type', 'name']),
14+
locationProps: new Set(['line', 'column']),
15+
16+
loadParser(callback) {
17+
require(['pug-lexer', 'pug-parser'], (lex, parse) => {
18+
callback({ lex, parse });
19+
});
20+
},
21+
22+
parse({ lex, parse }, code, options) {
23+
return parse(lex(code, {}), { src: code });
24+
},
25+
26+
opensByDefault(node, key) {
27+
switch (key) {
28+
case 'block':
29+
case 'nodes':
30+
return true;
31+
}
32+
},
33+
34+
getNodeName(node) {
35+
let { type } = node;
36+
/* eslint-disable no-fallthrough */
37+
switch (type) {
38+
case 'Block': return '';
39+
case 'Doctype': return `Doctype(${node.val})`;
40+
case 'Comment': if (node.buffer) return 'Comment(buffer)';
41+
case 'NamedBlock': return `Block:${node.mode}(${node.name})`;
42+
case 'Code': if (node.val === 'break') return 'Code(break)';
43+
case 'When': if (node.expr === 'default') return 'When(default)';
44+
case 'Include':
45+
case 'RawInclude':
46+
case 'Extends':
47+
case 'Each':
48+
case 'While':
49+
case 'Conditional':
50+
case 'Case':
51+
case 'AttributeBlock':
52+
case 'Text': return type;
53+
default: type = 'Attribute';
54+
case 'Filter':
55+
case 'Mixin': if (node.call) type = 'Mixin:call';
56+
case 'Tag': return `${type}(${node.name})`;
57+
}
58+
/* eslint-enable no-fallthrough */
59+
},
60+
};

website/yarn.lock

+48-1
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,11 @@ acorn@^7.1.0:
14151415
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.1.0.tgz#949d36f2c292535da602283586c2477c57eb2d6c"
14161416
integrity sha512-kL5CuoXA/dgxlBbVrflsflzQ3PAas7RYZB52NOm/6839iVYJgKMJ3cQJD+t2i5+qFa8h3MDpEOJiS64E8JLnSQ==
14171417

1418+
acorn@~4.0.2:
1419+
version "4.0.13"
1420+
resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.13.tgz#105495ae5361d697bd195c825192e1ad7f253787"
1421+
integrity sha1-EFSVrlNh1pe9GVyCUZLhrX8lN4c=
1422+
14181423
ajv-errors@^1.0.0:
14191424
version "1.0.1"
14201425
resolved "https://registry.yarnpkg.com/ajv-errors/-/ajv-errors-1.0.1.tgz#f35986aceb91afadec4102fbd85014950cefa64d"
@@ -2998,6 +3003,13 @@ character-entities@^1.0.0:
29983003
resolved "https://registry.yarnpkg.com/character-entities/-/character-entities-1.2.3.tgz#bbed4a52fe7ef98cc713c6d80d9faa26916d54e6"
29993004
integrity sha512-yB4oYSAa9yLcGyTbB4ItFwHw43QHdH129IJ5R+WvxOkWlyFnR5FAaBNnUq4mcxsTVZGh28bHoeTHMKXH1wZf3w==
30003005

3006+
character-parser@^2.1.1:
3007+
version "2.2.0"
3008+
resolved "https://registry.yarnpkg.com/character-parser/-/character-parser-2.2.0.tgz#c7ce28f36d4bcd9744e5ffc2c5fcde1c73261fc0"
3009+
integrity sha1-x84o821LzZdE5f/CxfzeHHMmH8A=
3010+
dependencies:
3011+
is-regex "^1.0.3"
3012+
30013013
character-reference-invalid@^1.0.0:
30023014
version "1.1.3"
30033015
resolved "https://registry.yarnpkg.com/character-reference-invalid/-/character-reference-invalid-1.1.3.tgz#1647f4f726638d3ea4a750cf5d1975c1c7919a85"
@@ -5857,6 +5869,14 @@ is-directory@^0.3.1:
58575869
resolved "https://registry.yarnpkg.com/is-directory/-/is-directory-0.3.1.tgz#61339b6f2475fc772fd9c9d83f5c8575dc154ae1"
58585870
integrity sha1-YTObbyR1/Hcv2cnYP1yFddwVSuE=
58595871

5872+
is-expression@^3.0.0:
5873+
version "3.0.0"
5874+
resolved "https://registry.yarnpkg.com/is-expression/-/is-expression-3.0.0.tgz#39acaa6be7fd1f3471dc42c7416e61c24317ac9f"
5875+
integrity sha1-Oayqa+f9HzRx3ELHQW5hwkMXrJ8=
5876+
dependencies:
5877+
acorn "~4.0.2"
5878+
object-assign "^4.0.1"
5879+
58605880
is-extendable@^0.1.0, is-extendable@^0.1.1:
58615881
version "0.1.1"
58625882
resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89"
@@ -5984,7 +6004,7 @@ is-property@^1.0.0, is-property@^1.0.2:
59846004
resolved "https://registry.yarnpkg.com/is-property/-/is-property-1.0.2.tgz#57fe1c4e48474edd65b09911f26b1cd4095dda84"
59856005
integrity sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=
59866006

5987-
is-regex@^1.0.4:
6007+
is-regex@^1.0.3, is-regex@^1.0.4:
59886008
version "1.0.4"
59896009
resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.0.4.tgz#5517489b547091b0930e095654ced25ee97e9491"
59906010
integrity sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=
@@ -7951,6 +7971,28 @@ pubsub-js@^1.4.2:
79517971
resolved "https://registry.yarnpkg.com/pubsub-js/-/pubsub-js-1.7.0.tgz#eca97f9a4217bef62b2d3aaa1552005260cc2e49"
79527972
integrity sha512-Pb68P9qFZxnvDipHMuj9oT1FoIgBcXJ9C9eWdHCLZAnulaUoJ3+Y87RhGMYilWpun6DMWVmvK70T4RP4drZMSA==
79537973

7974+
pug-error@^1.3.3:
7975+
version "1.3.3"
7976+
resolved "https://registry.yarnpkg.com/pug-error/-/pug-error-1.3.3.tgz#f342fb008752d58034c185de03602dd9ffe15fa6"
7977+
integrity sha512-qE3YhESP2mRAWMFJgKdtT5D7ckThRScXRwkfo+Erqga7dyJdY3ZquspprMCj/9sJ2ijm5hXFWQE/A3l4poMWiQ==
7978+
7979+
pug-lexer@^4.1.0:
7980+
version "4.1.0"
7981+
resolved "https://registry.yarnpkg.com/pug-lexer/-/pug-lexer-4.1.0.tgz#531cde48c7c0b1fcbbc2b85485c8665e31489cfd"
7982+
integrity sha512-i55yzEBtjm0mlplW4LoANq7k3S8gDdfC6+LThGEvsK4FuobcKfDAwt6V4jKPH9RtiE3a2Akfg5UpafZ1OksaPA==
7983+
dependencies:
7984+
character-parser "^2.1.1"
7985+
is-expression "^3.0.0"
7986+
pug-error "^1.3.3"
7987+
7988+
pug-parser@^5.0.1:
7989+
version "5.0.1"
7990+
resolved "https://registry.yarnpkg.com/pug-parser/-/pug-parser-5.0.1.tgz#03e7ada48b6840bd3822f867d7d90f842d0ffdc9"
7991+
integrity sha512-nGHqK+w07p5/PsPIyzkTQfzlYfuqoiGjaoqHv1LjOv2ZLXmGX1O+4Vcvps+P4LhxZ3drYSljjq4b+Naid126wA==
7992+
dependencies:
7993+
pug-error "^1.3.3"
7994+
token-stream "0.0.1"
7995+
79547996
pump@^2.0.0:
79557997
version "2.0.1"
79567998
resolved "https://registry.yarnpkg.com/pump/-/pump-2.0.1.tgz#12399add6e4cf7526d973cbc8b5ce2e2908b3909"
@@ -9550,6 +9592,11 @@ to-regex@^3.0.1, to-regex@^3.0.2:
95509592
regex-not "^1.0.2"
95519593
safe-regex "^1.1.0"
95529594

9595+
9596+
version "0.0.1"
9597+
resolved "https://registry.yarnpkg.com/token-stream/-/token-stream-0.0.1.tgz#ceeefc717a76c4316f126d0b9dbaa55d7e7df01a"
9598+
integrity sha1-zu78cXp2xDFvEm0LnbqlXX598Bo=
9599+
95539600
toposort@^1.0.0:
95549601
version "1.0.7"
95559602
resolved "https://registry.yarnpkg.com/toposort/-/toposort-1.0.7.tgz#2e68442d9f64ec720b8cc89e6443ac6caa950029"

0 commit comments

Comments
 (0)