Skip to content

droooney/generate-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

generate-code

The plugin exports a class that helps generate code and sourcemaps.

Installation

npm install --save generate-code

Usage

const CodeGenerator = require('generate-code');

const code = new CodeGenerator(options);

code.add('var a, b;\n');
code.addWithMapping(
  'var c;',
  { line: 1, column: 10 },
  'c'
);

const generatedCode = code.toString();

// var a, b;
// var c;

const generatedMap = code.generateMap();

// {
//   version: 3,
//   ...
// }

API

CodeGenerator
new CodeGenerator(options: Options)
  • options.filename (required): filename for the sourcemap.
  • options.sourceContent (required): source content for the sourcemap.
  • options.sourceMap (default: true): if it is needed to generate a sourcemap.
  • options.inputSourceMap (default: null): input sourcemap.
CodeGenerator#add
add(chunk: string): this

Adds a chunk of code to the generated code.

Example:

code.add('fun(1, 2);');
CodeGenerator#addWithMap
addWithMap(
  chunk: string,
  map: SourceMap,
  position?: { line: number, column: number } | number
): this

Applies the map to the existing map (shifting it according to the current position in the generated code) and then adds the chunk of code.

Third optional parameter is the position of the mappings relatively to the source. Can be a number or a { line, column } object with 0-indexed line.

Example:

const code = new CodeGenerator({
  filename: 'index.js',
  sourceContent: `var a = 10;
var b = {
  c: 1,
  d: 2
};
var c = [];`
});

code.add(`var a = 10;
var b = `);

const {
  code: generated,
  map
} = addUnderscoreToProps(`{
  c: 1,
  d: 2
}`);

code.addWithMap(
  generated,
  map,
  { line: 1, column: 8 } // where '{' is located
);
code.add(`;
var c = [];`);

console.log(code.toString());

// var a = 10;
// var b = {
//   _c: 1,
//   _d: 2,
// };
CodeGenerator#addWithMapping
addWithMapping(
  chunk: string,
  position?: { line: number, column: number } | number,
  name?: string
): this

Applies the mapping to the existing map (shifting it according to the current position in the generated code) and then adds the chunk of code.

Second optional parameter is the position of the mapping relatively to the source. Can be a number or a { line, column } object with 0-indexed line.

Third optional argument describes the mapping name.

Example:

const code = new CodeGenerator({
  filename: 'index.js',
  sourceContent: `var a = 10;
var b = {
  c: 1,
  d: 2
};
var c = [];`
});

code.add(`var a = 10;
var b = {
  `);

code.addWithMapping(
  '_c',
  { line: 2, column: 2 }, // where 'c' is located
  'c'
);

code.add(`: 1,
  `);

code.addWithMapping(
  '_d',
  { line: 3, column: 2 }, // where 'd' is located
  'd'
);

code.add(`: 2
};
var c = [];`);

console.log(code.toString());

// var a = 10;
// var b = {
//   _c: 1,
//   _d: 2,
// };
CodeGenerator#getCurrentIndent()
getCurrentIndent(): string

Returns current code indent.

Example:

const code = new CodeGenerator({
  filename: 'index.js',
  sourceContent: 'abc'
});

code.add('\na');

code.toString();         // '\na'
code.getCurrentIndent(); // ''

code.add('\n  b');

code.toString();         // '\na\n  b'
code.getCurrentIndent(); // '  '

code.add('\n\tc');

code.toString();         // '\na\n  b\n\tc'
code.getCurrentIndent(); // '\t'
CodeGenerator#toString()
toString(): string

Returns the generated code.

CodeGenerator#generateMap()
generateMap(): SourceMap

Returns the generated sourcemap.

About

Small and simple library for generating a code and sourcemaps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published