{"library":"shift-parser","title":"Shift Parser","description":"shift-parser is an ECMAScript parser that generates an Abstract Syntax Tree (AST) conforming to the Shift format. Currently at version 8.0.0, the library provides distinct functions for parsing ECMAScript scripts and modules, offering capabilities for static analysis and code transformation. Although its README suggests support for ECMA-262, version 6 (ES2015), the underlying Shift AST Specification, which `shift-parser` implements, states support for ECMAScript 2019. This indicates the parser likely handles features up to ES2019. The package is generally stable, with major versions released periodically rather than on a strict cadence. A key differentiator is its adherence to the machine-readable Shift AST format, which promotes consistency across various JavaScript tooling. It also offers an extended interface for capturing detailed location and comment information using `WeakMap`.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install shift-parser"],"cli":null},"imports":["import parse from 'shift-parser';","import { parseScript, parseModule } from 'shift-parser';","import { parseScriptWithLocation, parseModuleWithLocation } from 'shift-parser';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parseModuleWithLocation } from 'shift-parser';\n\nconst code = `\n  // This is a comment\n  import { add } from './math.js';\n  function calculate(a, b) {\n    const result = add(a, b);\n    return result; // Another comment\n  }\n  console.log(calculate(5, 3));\n`;\n\ntry {\n  const { tree, locations, comments } = parseModuleWithLocation(code);\n\n  console.log('Parsed AST root type:', tree.type);\n  console.log('Number of top-level statements:', tree.statements.length);\n\n  // Accessing a specific node and its location\n  const functionDeclaration = tree.statements[2]; // Assuming 'function calculate' is the third statement\n  if (functionDeclaration && locations.has(functionDeclaration)) {\n    const loc = locations.get(functionDeclaration);\n    console.log(`'calculate' function starts at line ${loc.start.line}, column ${loc.start.column}`);\n  }\n\n  // Logging comments\n  console.log('Found comments:', comments.map(c => c.text));\n\n} catch (e) {\n  if (e instanceof SyntaxError) {\n    console.error('Parsing failed:', e.message);\n  } else {\n    console.error('An unexpected error occurred:', e);\n  }\n}","lang":"typescript","description":"Demonstrates parsing an ECMAScript module, accessing the AST, retrieving location data for nodes, and extracting comments.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}