JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1262
  • Score
    100M100P100Q116171F
  • License MIT

Extract variables from pug template

Package Exports

  • pug-uses-variables

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pug-uses-variables) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

pug-uses-variables circleci

Find all JavaScript variables used in pug template.

Usage

$ npm install --save pug-uses-variables
const { findVariablesInTemplate } = require('pug-uses-variables')

const pugTemplate = `

  ReactComponent(property=object[item])
    = content

    if condition === matcher
      p Truthy

`.trimRight()

const variables = findVariablesInTemplate(pugTemplate)
// `variables` is equal:
[{
  value: 'ReactComponent',
  loc: {
    start: { line: 3,  column: 2 },
    end:   { line: 3,  column: 14 }
  }
}, {
  value: 'object',
  loc: {
    start: { line: 3,  column: 24 },
    end:   { line: 3,  column: 30 }
  }
}, {
  value: 'item',
  loc: {
    start: { line: 3,  column: 31 },
    end:   { line: 3,  column: 35 }
  }
}, {
  value: 'content',
  loc: {
    start: { line: 4,  column: 4 },
    end:   { line: 4,  column: 11 }
  }
}, {
  value: 'condition',
  loc: {
    start: { line: 6,  column: 5 },
    end:   { line: 6,  column: 14 }
  }
}, {
  value: 'condition',
  loc: {
    start: { line: 6,  column: 19 },
    end:   { line: 6,  column: 26 }
  }
}]

Important: templates should be right-trimmed. Usually it means that you have to execute .trimRight on your template-string as in the example above.

Get variable names only

const variableNames = variables.map(variable => variable.value)
// > ['ReactComponent', 'object', 'item', 'content', 'condition', 'matcher']

Value

This plugin helps us to integrate babel-plugin-transform-react-pug and eslint.

Development

  • yarn test
  • yarn lint

License

MIT