Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ESLintプラグインでAST入門

oodemi
May 28, 2016
1k

 ESLintプラグインでAST入門

oodemi

May 28, 2016
Tweet

Transcript

  1. ! ?

  2. AST { "range": [ 0, 25 ], "type": "Program", "body":

    [ { "range": [ 0, 25 ], "type": "VariableDeclaration", "declarations": [ { "range": [ 4, 25 ], "type": "VariableDeclarator", "id": { "range": [ 4, 11 ], "type": "Identifier", "name": "message" }, "init": { "range": [ 14, 25 ], "type": "Literal", "value": "Hello AST", "raw": "\"Hello AST\"" } } ], "kind": "var" } ], "sourceType": "module" }
  3. !

  4. Կͱͳ͘෼͔Δ 4 Ұ෦ൈਮ "type": "VariableDeclarator" // ม਺એݴ "kind": "var" //

    "var"Ͱએݴ͞ΕͯΔ "name": "message" // ม਺໊͸"message" "range": [4, 11] // 4จࣈ໨~11จࣈ໨ "value": "Hello AST" // த਎͸"Hello AST"
  5. no-var.js "use strict"; module.exports = { create: function(context) { return

    { // 1. VariableDeclarationͷnodeʹ౸ୡͨ͠Β࣮ߦ͞ΕΔ VariableDeclaration: function(node) { // 2. var͕࢖ΘΕ͍ͯͨΒΤϥʔϝοηʔδΛදࣔ͢Δ if (node.kind === "var") { context.report(node, "Unexpected var, use let or const instead."); } } }; } };
  6. { "range": [ 6, 27 ], "type": "Program", "body": [

    { "range": [ 6, 27 ], "type": "VariableDeclaration", "declarations": [ { "range": [ 10, 26 ], "type": "VariableDeclarator", "id": { "range": [ 10, 16 ], "type": "Identifier", "name": "status" }, "init": { "range": [ 19, 26 ], "type": "Literal", "value": "Nemui", "raw": "\"Nemui\"" } } ], "kind": "var" } ], "sourceType": "module", "comments": [ { "type": "Line", "value": " ຾͍", "range": [ 0, 5 ] } ] }
  7. no-nemui-comment.js 'use strict'; module.exports = { create: (context) => {

    return { // 1. LineCommentͷ࣌ʹ "LineComment": (node) => { // 2. த਎͕ʮ຾͍ʯͩͬͨΒ if (node.value === "຾͍") { context.report(node, "✘ ;͚ͨ͟ίϝϯτ͸ॻ͔ͳ͍ͰԼ͍͞."); } } }; } };
  8. !