@textlint/ast-traverse
TypeScript icon, indicating that this package has built-in type declarations

14.3.0 • Public • Published

@textlint/ast-traverse

@textlint/ast-traverse provide traversal functions for TxtAST.

This traverse function is a fork of estraverse for @textlint/markdown-to-ast.

This library is a part of textlint/textlint.

Installation

npm install @textlint/ast-traverse

Usage

var parse = require("@textlint/markdown-to-ast").parse,
    Syntax = require("@textlint/markdown-to-ast").Syntax;
var traverse = require("@textlint/ast-traverse").traverse,
    VisitorOption = require("@textlint/ast-traverse").VisitorOption;
var AST = parse("# Header\nHello*world*");
traverse(AST, {
    enter(node) {
        console.log("enter", node.type);
        if (node.type === Syntax.Strong) {
            return VisitorOption.Skip;
        }
    },
    leave(node) {
        console.log("leave", node.type);
    }
});

Traversal rule is the same with Estraverse.

Example

Markdown:

Hello *world*

AST:

{
    "start_line": 1,
    "start_column": 1,
    "end_line": 0,
    "children": [
        {
            "start_line": 1,
            "start_column": 1,
            "end_line": 0,
            "inline_content": [
                {
                    "c": "Hello",
                    "raw": "Hello",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 0
                        },
                        "end": {
                            "line": 1,
                            "column": 5
                        }
                    },
                    "range": [
                        0,
                        5
                    ],
                    "type": "Str"
                },
                {
                    "c": " ",
                    "raw": " ",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 5
                        },
                        "end": {
                            "line": 1,
                            "column": 6
                        }
                    },
                    "range": [
                        5,
                        6
                    ],
                    "type": "Str"
                },
                {
                    "c": [
                        {
                            "c": "world",
                            "raw": "world",
                            "loc": {
                                "start": {
                                    "line": 1,
                                    "column": 0
                                },
                                "end": {
                                    "line": 1,
                                    "column": 5
                                }
                            },
                            "range": [
                                0,
                                5
                            ],
                            "type": "Str"
                        }
                    ],
                    "raw": "*world*",
                    "loc": {
                        "start": {
                            "line": 1,
                            "column": 6
                        },
                        "end": {
                            "line": 1,
                            "column": 13
                        }
                    },
                    "range": [
                        6,
                        13
                    ],
                    "type": "Emphasis"
                }
            ],
            "children": [],
            "raw": "Hello *world*",
            "loc": {
                "start": {
                    "line": 1,
                    "column": 0
                },
                "end": {
                    "line": 1,
                    "column": 13
                }
            },
            "range": [
                0,
                13
            ],
            "type": "Paragraph"
        }
    ],
    "raw": "Hello *world*",
    "loc": {
        "start": {
            "line": 1,
            "column": 0
        },
        "end": {
            "line": 1,
            "column": 13
        }
    },
    "range": [
        0,
        13
    ],
    "type": "Document"
}

Traversal all from Root(Document node):

[enter, Syntax.Document],
// # Header
[enter, Syntax.Header],
[enter, Syntax.Str],
[leave, Syntax.Str],
[leave, Syntax.Header],
// => Paragraph
[enter, Syntax.Paragraph],
[enter, Syntax.Str],
[leave, Syntax.Str],
// *world*
[enter, Syntax.Emphasis],
[enter, Syntax.Str],
[leave, Syntax.Str],
[leave, Syntax.Emphasis],
// <= Paragraph
[leave, Syntax.Paragraph],
// End
[leave, Syntax.Document]

NOTE

You want to set property on Node.

Bad example:

var TraverseController = require("@textlint/ast-traverse").Controller;
var controller = new TraverseController();
controller.traverse(ast, {
    enter: function (node, parent) {
        node.parent = parent;// it cause a circular reference!
        // do something
        something(node);
    }
});

node.parent = parent; cause a circular reference!

Correct example:

var TraverseController = require("@textlint/ast-traverse").Controller;
var controller = new TraverseController();
controller.traverse(ast, {
    enter: function (node, parent) {
        // set property as non-enumerable value
        Object.defineProperty(node, "parent", {
            value: parent
        });
        // do something
        something(node);
    }
});

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT

and

Includes Estraverse

Copyright (C) 2012-2013 Yusuke Suzuki
https://github.com/estools/estraverse/blob/master/LICENSE.BSD

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.1.0-alpha.0f2fd6f90canary
12.0.0-beta.30beta
14.3.020,476latest
13.4.2-next.07next

Version History

VersionDownloads (Last 7 Days)Published
14.3.020,476
14.2.13,413
14.2.06,580
14.1.00
14.0.51,335
14.0.44,293
14.0.3508
14.0.217
14.0.151
14.0.0101
13.4.2-next.07
13.4.124,399
13.4.037
13.3.38,118
13.3.2356
13.3.180
13.3.010
13.2.069
13.1.40
13.1.38
13.1.20
13.1.187
13.1.0110
13.0.52
13.0.40
13.0.30
13.0.29
13.0.10
13.0.00
12.6.17,198
12.6.00
12.5.20
12.5.016
12.3.01
12.2.3196
12.2.21,176
12.2.1497
12.2.00
12.1.11,524
12.1.01,286
12.0.290
12.0.01,204
12.0.0-beta.30
12.0.0-beta.20
12.0.0-beta.10
12.0.0-beta.00
2.3.52,164
2.3.4309
2.3.30
2.3.233
2.3.10
2.3.00
2.2.612
2.2.5110
2.2.40
2.2.30
2.2.20
2.2.10
2.2.00
2.1.781
2.1.617
2.1.513
2.1.40
2.1.390
2.1.278
2.1.10
2.1.00
2.0.95
2.1.0-alpha.0f2fd6f90
2.1.0-alpha.bbfc8f6d0
2.0.80
2.0.70
2.0.60
2.0.50
2.0.40
2.0.30

Package Sidebar

Install

npm i @textlint/ast-traverse

Weekly Downloads

71,519

Version

14.3.0

License

MIT

Unpacked Size

123 kB

Total Files

15

Last publish

Collaborators

  • textlint-user
  • azu
  • 0x6b