New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sass-parser

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sass-parser

A simple npm package to parse a Sass file into a consumable json data

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Sass Parser

A simple npm package to parse a sass file into a consumable json data

Build Status Coverage Status npm version Greenkeeper badge MIT License

What it does

This is a simple package that does a simple task of going through a sass file and extracting relevant details of variables, mixins, and functions contained within that file.

Currently it only supports sass old syntax; meaning, it does NOT handle the newer scss syntax. For more details about this, have a look at Sass Syntax.

The extracted data is useful for usage in documentation or any other similar needs.

Install

$ npm install sass-parser

Usage

Parsing a file

const parse = require('sass-parser')();

parse.file('index.sass').then( data => console.log( data ) );
Output of parse.file
{
  variables: {
    var_1: {
      id: 'var_1',
      name: '$var_1',
      value: '#000'
    },
    var_2: ...,
    var_3: ...
  },
  mixins: {
    mix_1: {
      name: 'mix_1',
      parameters: [
        '$param_1',
        ...
      ]
    },
    mix_2: ...,
    mix_3: ...
  },
  functions: {
    fn_1: {
      name: 'fn_1',
      parameters: [
        '$param_1',
        ...
      ]
    },
    fn_2: ...,
    fn_3: ...
  }
}

Parsing a variable

parse.variable( '$white: #fff !default' );

// output
{
  id: 'white',
  name: '$white',
  value: '#fff'
}

Parsing a mixin

parse.mixin( '=mix( $color )' );

// output
{
  name: 'mix',
  parameters: [
    '$color'
  ]
}

Parsing a function

parse.function( '@function do_something( $this, $that )' );

// output
{
  name: 'do_something',
  parameters: [
    '$this',
    '$that'
  ]
}

Options

Default options:

{
  regex: {
    variable: {
      pattern: '^\\$(.*?):\\s*([^;]+)\\s*([^;]+)\\!(global|default)',
      flags  : 'g',
    },
    mixin_name: {
      pattern: '^=((?!\\d)[\\w_-][\\w\\d_-]*)',
      flags  : 'gi',
    },
    mixin: {
      pattern: '^=((?!\\d)[\\w_-][\\w\\d_-]*)\\s*\\(\\s*([^\\)"]+)?.',
      flags  : 'gi',
    },
    function_name: {
      pattern: '^@function\\s*?((?!\\d)[\\w_-][\\w\\d_-]*)',
      flags  : 'gi',
    },
    function: {
      pattern: '^@function\\s*?((?!\\d)[\\w_-][\\w\\d_-]*)\\s*\\(\\s*([^\\)"]+)?.',
      flags  : 'gi',
    },
  },

  // ---------------------------------------------------------------------------
  
  line_by_line: {
    skipEmptyLines: true,
  },

  // ---------------------------------------------------------------------------
  
  keys: {
    
    variables: 'variables',
    variable : {
      id    : 'id',
      name  : 'name',
      value : 'value',
    },

    // -------------------------------------------------------------------------
    
    mixins: 'mixins',
    mixin : {
      name      : 'name',
      parameters: 'parameters',
    },

    // -------------------------------------------------------------------------
    
    functions: 'functions',
    function : {
      name      : 'name',
      parameters: 'parameters',
    },
    
  }
}

Example of using custom options:

const parser = require('sass-parser');
const parse  = parser({
  keys: {
    variables: 'vars',
    mixin: {
      parameters: 'param',
    },
    function: {
      name: 'fn',
    }
  }
});

parse.file('index.sass').then( data => console.log( data ) );
Output after applying custom options
{
  vars: {           // <== now it's 'vars' instead of 'variables'
    var_1: {
      id: 'var_1',
      name: '$var_1',
      value: '#000'
    },
    var_2: ...,
    var_3: ...
  },
  mixins: {
    mix_1: {
      name: 'mix_1',
      param: [      // <== now it's 'param' instead of 'parameters'
        '$param_1',
        ...
      ]
    },
    mix_2: ...,
    mix_3: ...
  },
  functions: {
    fn_1: {
      fn: 'fn_1',   // <== now it's 'fn' instead of 'name'
      parameters: [
        '$param_1',
        ...
      ]
    },
    fn_2: ...,
    fn_3: ...
  }
}

API

options

Default options are set here.

Can be extended when requiring the module by passing a custom options object.

variable

Parses line to get sass variable

Parameters

  • line string

Returns object

chunk

Parses line to get sass mixin or function

Parameters

Returns object

function

Parses line to get sass function

Parameters

Returns object

mixin

Parses line to get sass mixin

Parameters

Returns object

file

Parses a sass file line by line and populates returned data with variables, mixins and functions

Parameters

Returns promise

License

MIT © bstashio

Keywords

FAQs

Package last updated on 07 Nov 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc