rollup-pluginutils
Advanced tools
Comparing version 1.1.0 to 1.2.0
# rollup-pluginutils changelog | ||
## 1.2.0 | ||
* Add `attachScopes` and `makeLegalIdentifier` | ||
## 1.1.0 | ||
@@ -4,0 +8,0 @@ |
'use strict'; | ||
var estreeWalker = require('estree-walker'); | ||
var path = require('path'); | ||
var minimatch = require('minimatch'); | ||
var babelHelpers = {}; | ||
babelHelpers.createClass = (function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
})(); | ||
babelHelpers.classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
function basename(path) { | ||
return path.split(/(\/|\\)/).pop(); | ||
} | ||
function extname(path) { | ||
var match = /\.[^\.]+$/.exec(basename(path)); | ||
if (!match) return ''; | ||
return match[0]; | ||
} | ||
function addExtension(filename) { | ||
var ext = arguments.length <= 1 || arguments[1] === undefined ? '.js' : arguments[1]; | ||
if (!extname(filename)) filename += ext; | ||
return filename; | ||
} | ||
var blockDeclarations = { | ||
'const': true, | ||
'let': true | ||
}; | ||
var extractors = { | ||
Identifier: function Identifier(names, param) { | ||
names.push(param.name); | ||
}, | ||
ObjectPattern: function ObjectPattern(names, param) { | ||
param.properties.forEach(function (prop) { | ||
extractors[prop.key.type](names, prop.key); | ||
}); | ||
}, | ||
ArrayPattern: function ArrayPattern(names, param) { | ||
param.elements.forEach(function (element) { | ||
if (element) extractors[element.type](names, element); | ||
}); | ||
}, | ||
RestElement: function RestElement(names, param) { | ||
extractors[param.argument.type](names, param.argument); | ||
}, | ||
AssignmentPattern: function AssignmentPattern(names, param) { | ||
return extractors[param.left.type](names, param.left); | ||
} | ||
}; | ||
function extractNames(param) { | ||
var names = []; | ||
extractors[param.type](names, param); | ||
return names; | ||
} | ||
var Scope = (function () { | ||
function Scope(options) { | ||
var _this = this; | ||
babelHelpers.classCallCheck(this, Scope); | ||
options = options || {}; | ||
this.parent = options.parent; | ||
this.isBlockScope = !!options.block; | ||
this.declarations = Object.create(null); | ||
if (options.params) { | ||
options.params.forEach(function (param) { | ||
extractNames(param).forEach(function (name) { | ||
_this.declarations[name] = true; | ||
}); | ||
}); | ||
} | ||
} | ||
babelHelpers.createClass(Scope, [{ | ||
key: 'addDeclaration', | ||
value: function addDeclaration(node, isBlockDeclaration, isVar) { | ||
var _this2 = this; | ||
if (!isBlockDeclaration && this.isBlockScope) { | ||
// it's a `var` or function node, and this | ||
// is a block scope, so we need to go up | ||
this.parent.addDeclaration(node, isBlockDeclaration, isVar); | ||
} else { | ||
extractNames(node.id).forEach(function (name) { | ||
_this2.declarations[name] = true; | ||
}); | ||
} | ||
} | ||
}, { | ||
key: 'contains', | ||
value: function contains(name) { | ||
return this.declarations[name] || (this.parent ? this.parent.contains(name) : false); | ||
} | ||
}]); | ||
return Scope; | ||
})(); | ||
function attachScopes(ast) { | ||
var propertyName = arguments.length <= 1 || arguments[1] === undefined ? 'scope' : arguments[1]; | ||
var scope = new Scope(); | ||
estreeWalker.walk(ast, { | ||
enter: function enter(node, parent) { | ||
// function foo () {...} | ||
// class Foo {...} | ||
if (/(Function|Class)Declaration/.test(node.type)) { | ||
scope.addDeclaration(node, false, false); | ||
} | ||
// var foo = 1 | ||
if (node.type === 'VariableDeclaration') { | ||
var isBlockDeclaration = blockDeclarations[node.kind]; | ||
// only one declarator per block, because we split them up already | ||
scope.addDeclaration(node.declarations[0], isBlockDeclaration, true); | ||
} | ||
var newScope = undefined; | ||
// create new function scope | ||
if (/Function/.test(node.type)) { | ||
newScope = new Scope({ | ||
parent: scope, | ||
block: false, | ||
params: node.params | ||
}); | ||
// named function expressions - the name is considered | ||
// part of the function's scope | ||
if (node.type === 'FunctionExpression' && node.id) { | ||
newScope.addDeclaration(node, false, false); | ||
} | ||
} | ||
// create new block scope | ||
if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) { | ||
newScope = new Scope({ | ||
parent: scope, | ||
block: true | ||
}); | ||
} | ||
// catch clause has its own block scope | ||
if (node.type === 'CatchClause') { | ||
newScope = new Scope({ | ||
parent: scope, | ||
params: [node.param], | ||
block: true | ||
}); | ||
} | ||
if (newScope) { | ||
Object.defineProperty(node, propertyName, { | ||
value: newScope, | ||
configurable: true | ||
}); | ||
scope = newScope; | ||
} | ||
}, | ||
leave: function leave(node) { | ||
if (node[propertyName]) scope = scope.parent; | ||
} | ||
}); | ||
return scope; | ||
} | ||
function ensureArray(thing) { | ||
@@ -35,20 +232,22 @@ if (Array.isArray(thing)) return thing; | ||
function basename(path) { | ||
return path.split(/(\/|\\)/).pop(); | ||
} | ||
var reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'.split(' '); | ||
var builtins = 'Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl'.split(' '); | ||
function extname(path) { | ||
var match = /\.[^\.]+$/.exec(basename(path)); | ||
if (!match) return ''; | ||
return match[0]; | ||
} | ||
function addExtension(filename) { | ||
var ext = arguments.length <= 1 || arguments[1] === undefined ? '.js' : arguments[1]; | ||
var blacklisted = Object.create(null); | ||
reservedWords.concat(builtins).forEach(function (word) { | ||
return blacklisted[word] = true; | ||
}); | ||
function makeLegalIdentifier(str) { | ||
str = str.replace(/-(\w)/g, function (_, letter) { | ||
return letter.toUpperCase(); | ||
}).replace(/[^$_a-zA-Z0-9]/g, '_'); | ||
if (!extname(filename)) filename += ext; | ||
return filename; | ||
if (/\d/.test(str[0]) || blacklisted[str]) str = '_' + str; | ||
return str; | ||
} | ||
exports.addExtension = addExtension; | ||
exports.attachScopes = attachScopes; | ||
exports.createFilter = createFilter; | ||
exports.addExtension = addExtension; | ||
//# sourceMappingURL=pluginutils.cjs.js.map | ||
exports.makeLegalIdentifier = makeLegalIdentifier; |
@@ -0,4 +1,201 @@ | ||
import { walk } from 'estree-walker'; | ||
import { resolve } from 'path'; | ||
import { makeRe } from 'minimatch'; | ||
var babelHelpers = {}; | ||
babelHelpers.createClass = (function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
})(); | ||
babelHelpers.classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
function basename(path) { | ||
return path.split(/(\/|\\)/).pop(); | ||
} | ||
function extname(path) { | ||
var match = /\.[^\.]+$/.exec(basename(path)); | ||
if (!match) return ''; | ||
return match[0]; | ||
} | ||
function addExtension(filename) { | ||
var ext = arguments.length <= 1 || arguments[1] === undefined ? '.js' : arguments[1]; | ||
if (!extname(filename)) filename += ext; | ||
return filename; | ||
} | ||
var blockDeclarations = { | ||
'const': true, | ||
'let': true | ||
}; | ||
var extractors = { | ||
Identifier: function Identifier(names, param) { | ||
names.push(param.name); | ||
}, | ||
ObjectPattern: function ObjectPattern(names, param) { | ||
param.properties.forEach(function (prop) { | ||
extractors[prop.key.type](names, prop.key); | ||
}); | ||
}, | ||
ArrayPattern: function ArrayPattern(names, param) { | ||
param.elements.forEach(function (element) { | ||
if (element) extractors[element.type](names, element); | ||
}); | ||
}, | ||
RestElement: function RestElement(names, param) { | ||
extractors[param.argument.type](names, param.argument); | ||
}, | ||
AssignmentPattern: function AssignmentPattern(names, param) { | ||
return extractors[param.left.type](names, param.left); | ||
} | ||
}; | ||
function extractNames(param) { | ||
var names = []; | ||
extractors[param.type](names, param); | ||
return names; | ||
} | ||
var Scope = (function () { | ||
function Scope(options) { | ||
var _this = this; | ||
babelHelpers.classCallCheck(this, Scope); | ||
options = options || {}; | ||
this.parent = options.parent; | ||
this.isBlockScope = !!options.block; | ||
this.declarations = Object.create(null); | ||
if (options.params) { | ||
options.params.forEach(function (param) { | ||
extractNames(param).forEach(function (name) { | ||
_this.declarations[name] = true; | ||
}); | ||
}); | ||
} | ||
} | ||
babelHelpers.createClass(Scope, [{ | ||
key: 'addDeclaration', | ||
value: function addDeclaration(node, isBlockDeclaration, isVar) { | ||
var _this2 = this; | ||
if (!isBlockDeclaration && this.isBlockScope) { | ||
// it's a `var` or function node, and this | ||
// is a block scope, so we need to go up | ||
this.parent.addDeclaration(node, isBlockDeclaration, isVar); | ||
} else { | ||
extractNames(node.id).forEach(function (name) { | ||
_this2.declarations[name] = true; | ||
}); | ||
} | ||
} | ||
}, { | ||
key: 'contains', | ||
value: function contains(name) { | ||
return this.declarations[name] || (this.parent ? this.parent.contains(name) : false); | ||
} | ||
}]); | ||
return Scope; | ||
})(); | ||
function attachScopes(ast) { | ||
var propertyName = arguments.length <= 1 || arguments[1] === undefined ? 'scope' : arguments[1]; | ||
var scope = new Scope(); | ||
walk(ast, { | ||
enter: function enter(node, parent) { | ||
// function foo () {...} | ||
// class Foo {...} | ||
if (/(Function|Class)Declaration/.test(node.type)) { | ||
scope.addDeclaration(node, false, false); | ||
} | ||
// var foo = 1 | ||
if (node.type === 'VariableDeclaration') { | ||
var isBlockDeclaration = blockDeclarations[node.kind]; | ||
// only one declarator per block, because we split them up already | ||
scope.addDeclaration(node.declarations[0], isBlockDeclaration, true); | ||
} | ||
var newScope = undefined; | ||
// create new function scope | ||
if (/Function/.test(node.type)) { | ||
newScope = new Scope({ | ||
parent: scope, | ||
block: false, | ||
params: node.params | ||
}); | ||
// named function expressions - the name is considered | ||
// part of the function's scope | ||
if (node.type === 'FunctionExpression' && node.id) { | ||
newScope.addDeclaration(node, false, false); | ||
} | ||
} | ||
// create new block scope | ||
if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) { | ||
newScope = new Scope({ | ||
parent: scope, | ||
block: true | ||
}); | ||
} | ||
// catch clause has its own block scope | ||
if (node.type === 'CatchClause') { | ||
newScope = new Scope({ | ||
parent: scope, | ||
params: [node.param], | ||
block: true | ||
}); | ||
} | ||
if (newScope) { | ||
Object.defineProperty(node, propertyName, { | ||
value: newScope, | ||
configurable: true | ||
}); | ||
scope = newScope; | ||
} | ||
}, | ||
leave: function leave(node) { | ||
if (node[propertyName]) scope = scope.parent; | ||
} | ||
}); | ||
return scope; | ||
} | ||
function ensureArray(thing) { | ||
@@ -33,19 +230,19 @@ if (Array.isArray(thing)) return thing; | ||
function basename(path) { | ||
return path.split(/(\/|\\)/).pop(); | ||
} | ||
var reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public'.split(' '); | ||
var builtins = 'Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl'.split(' '); | ||
function extname(path) { | ||
var match = /\.[^\.]+$/.exec(basename(path)); | ||
if (!match) return ''; | ||
return match[0]; | ||
} | ||
function addExtension(filename) { | ||
var ext = arguments.length <= 1 || arguments[1] === undefined ? '.js' : arguments[1]; | ||
var blacklisted = Object.create(null); | ||
reservedWords.concat(builtins).forEach(function (word) { | ||
return blacklisted[word] = true; | ||
}); | ||
function makeLegalIdentifier(str) { | ||
str = str.replace(/-(\w)/g, function (_, letter) { | ||
return letter.toUpperCase(); | ||
}).replace(/[^$_a-zA-Z0-9]/g, '_'); | ||
if (!extname(filename)) filename += ext; | ||
return filename; | ||
if (/\d/.test(str[0]) || blacklisted[str]) str = '_' + str; | ||
return str; | ||
} | ||
export { createFilter, addExtension }; | ||
//# sourceMappingURL=pluginutils.es6.js.map | ||
export { addExtension, attachScopes, createFilter, makeLegalIdentifier }; |
{ | ||
"name": "rollup-pluginutils", | ||
"description": "Functionality commonly needed by Rollup plugins", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"main": "dist/pluginutils.cjs.js", | ||
"jsnext:main": "dist/pluginutils.es6.js", | ||
"devDependencies": { | ||
"eslint": "^1.7.3", | ||
"eslint": "^1.8.0", | ||
"gobble": "^0.10.2", | ||
"gobble-cli": "^0.6.0", | ||
"gobble-rollup-babel": "^0.6.1", | ||
"mocha": "^2.3.3" | ||
"gobble-rollup": "^0.11.0", | ||
"mocha": "^2.3.3", | ||
"rollup-plugin-babel": "^1.0.0" | ||
}, | ||
@@ -21,2 +22,3 @@ "scripts": { | ||
"dependencies": { | ||
"estree-walker": "^0.2.0", | ||
"minimatch": "^3.0.0" | ||
@@ -23,0 +25,0 @@ }, |
@@ -32,2 +32,42 @@ # rollup-pluginutils | ||
### attachScopes | ||
This function attaches `Scope` objects to the relevant nodes of an AST. Each `Scope` object has a `scope.contains(name)` method that returns `true` if a given name is defined in the current scope or a parent scope. | ||
See [rollup-plugin-inject](https://github.com/rollup/rollup-plugin-inject) or [rollup-plugin-commonjs](https://github.com/rollup/rollup-plugin-inject) for an example of usage. | ||
```js | ||
import { attachScopes } from 'rollup-pluginutils'; | ||
import { parse } from 'acorn'; | ||
import { walk } from 'estree-walker'; | ||
export default function myPlugin ( options = {} ) { | ||
return { | ||
transform ( code ) { | ||
const ast = parse( ast, { | ||
ecmaVersion: 6, | ||
sourceType: 'module' | ||
}); | ||
let scope = attachScopes( ast, 'scope' ); | ||
walk( ast, { | ||
enter ( node ) { | ||
if ( node.scope ) scope = node.scope; | ||
if ( !scope.contains( 'foo' ) ) { | ||
// `foo` is not defined, so if we encounter it, | ||
// we assume it's a global | ||
} | ||
}, | ||
leave ( node ) { | ||
if ( node.scope ) scope = scope.parent; | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
``` | ||
### createFilter | ||
@@ -58,4 +98,14 @@ | ||
### makeLegalIdentifier | ||
```js | ||
import { makeLegalIdentifier } from 'rollup-pluginutils'; | ||
makeLegalIdentifier( 'foo-bar' ); // 'foo_bar' | ||
makeLegalIdentifier( 'typeof' ); // '_typeof' | ||
``` | ||
## License | ||
MIT |
@@ -0,2 +1,4 @@ | ||
export { default as addExtension } from './addExtension'; | ||
export { default as attachScopes } from './attachScopes'; | ||
export { default as createFilter } from './createFilter'; | ||
export { default as addExtension } from './addExtension'; | ||
export { default as makeLegalIdentifier } from './makeLegalIdentifier'; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23236
580
110
2
6
1
+ Addedestree-walker@^0.2.0
+ Addedestree-walker@0.2.1(transitive)