pax_global_header00006660000000000000000000000064130327734070014520gustar00rootroot0000000000000052 comment=1387dd324c5e23fd5aee40f7949268505c0e555a rollup-pluginutils-2.0.1/000077500000000000000000000000001303277340700154125ustar00rootroot00000000000000rollup-pluginutils-2.0.1/.eslintrc000066400000000000000000000012301303277340700172320ustar00rootroot00000000000000{ "rules": { "indent": [ 2, "tab", { "SwitchCase": 1 } ], "quotes": [ 2, "single" ], "linebreak-style": [ 2, "unix" ], "semi": [ 2, "always" ], "space-after-keywords": [ 2, "always" ], "space-before-blocks": [ 2, "always" ], "space-before-function-paren": [ 2, "always" ], "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], "no-cond-assign": [ 0 ] }, "env": { "es6": true, "browser": true, "mocha": true, "node": true }, "extends": "eslint:recommended", "parserOptions": { "ecmaVersion": 8, "sourceType": "module" } } rollup-pluginutils-2.0.1/.gitignore000066400000000000000000000000451303277340700174010ustar00rootroot00000000000000.DS_Store node_modules dist .gobble* rollup-pluginutils-2.0.1/.travis.yml000066400000000000000000000002151303277340700175210ustar00rootroot00000000000000sudo: false language: node_js node_js: - "0.12" - "4" env: global: - BUILD_TIMEOUT=10000 install: npm install # script: npm run ci rollup-pluginutils-2.0.1/CHANGELOG.md000066400000000000000000000020301303277340700172160ustar00rootroot00000000000000# rollup-pluginutils changelog ## 2.0.1 * Don't add extension to file with trailing dot ([#14](https://github.com/rollup/rollup-pluginutils/issues/14)) ## 2.0.0 * Use `micromatch` instead of `minimatch` ([#19](https://github.com/rollup/rollup-pluginutils/issues/19)) * Allow `createFilter` to take regexes ([#5](https://github.com/rollup/rollup-pluginutils/issues/5)) ## 1.5.2 * Treat `arguments` as a reserved word ([#10](https://github.com/rollup/rollup-pluginutils/issues/10)) ## 1.5.1 * Add all declarators in a var declaration to scope, not just the first ## 1.5.0 * Exclude IDs with null character (`\0`) ## 1.4.0 * Workaround minimatch issue ([#6](https://github.com/rollup/rollup-pluginutils/pull/6)) * Exclude non-string IDs in `createFilter` ## 1.3.1 * Build with Rollup directly, rather than via Gobble ## 1.3.0 * Use correct path separator on Windows ## 1.2.0 * Add `attachScopes` and `makeLegalIdentifier` ## 1.1.0 * Add `addExtension` function ## 1.0.1 * Include dist files in package ## 1.0.0 * First release rollup-pluginutils-2.0.1/README.md000066400000000000000000000051141303277340700166720ustar00rootroot00000000000000# rollup-pluginutils A set of functions commonly used by Rollup plugins. ## Installation ```bash npm install --save rollup-pluginutils ``` ## Usage ### addExtension ```js import { addExtension } from 'rollup-pluginutils'; export default function myPlugin ( options = {} ) { return { resolveId ( code, id ) { // only adds an extension if there isn't one already id = addExtension( id ); // `foo` -> `foo.js`, `foo.js -> foo.js` id = addExtension( id, '.myext' ); // `foo` -> `foo.myext`, `foo.js -> `foo.js` } }; } ``` ### 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 ```js import { createFilter } from 'rollup-pluginutils'; export default function myPlugin ( options = {} ) { // `options.include` and `options.exclude` can each be a minimatch // pattern, or an array of minimatch patterns, relative to process.cwd() var filter = createFilter( options.include, options.exclude ); return { transform ( code, id ) { // if `options.include` is omitted or has zero length, filter // will return `true` by default. Otherwise, an ID must match // one or more of the minimatch patterns, and must not match // any of the `options.exclude` patterns. if ( !filter( id ) ) return; // proceed with the transformation... } }; } ``` ### makeLegalIdentifier ```js import { makeLegalIdentifier } from 'rollup-pluginutils'; makeLegalIdentifier( 'foo-bar' ); // 'foo_bar' makeLegalIdentifier( 'typeof' ); // '_typeof' ``` ## License MIT rollup-pluginutils-2.0.1/appveyor.yml000066400000000000000000000011071303277340700200010ustar00rootroot00000000000000# http://www.appveyor.com/docs/appveyor-yml version: "{build}" clone_depth: 10 init: - git config --global core.autocrlf false environment: matrix: # node.js - nodejs_version: 0.12 - nodejs_version: 4 install: - ps: Install-Product node $env:nodejs_version - npm install build: off test_script: - node --version && npm --version - npm test matrix: fast_finish: false # cache: # - C:\Users\appveyor\AppData\Roaming\npm-cache -> package.json # npm cache # - node_modules -> package.json # local npm modules rollup-pluginutils-2.0.1/package.json000066400000000000000000000017111303277340700177000ustar00rootroot00000000000000{ "name": "rollup-pluginutils", "description": "Functionality commonly needed by Rollup plugins", "version": "2.0.1", "main": "dist/pluginutils.cjs.js", "module": "dist/pluginutils.es.js", "jsnext:main": "dist/pluginutils.es.js", "files": [ "src", "dist", "README.md" ], "devDependencies": { "eslint": "^3.12.2", "mocha": "^3.2.0", "rollup": "^0.40.0", "rollup-plugin-buble": "^0.15.0" }, "scripts": { "test": "mocha", "build": "rollup -c", "pretest": "npm run build", "prepublish": "npm test" }, "dependencies": { "estree-walker": "^0.3.0", "micromatch": "^2.3.11" }, "repository": "rollup/rollup-pluginutils", "keywords": [ "rollup", "utils" ], "author": "Rich Harris ", "license": "MIT", "bugs": { "url": "https://github.com/rollup/rollup-pluginutils/issues" }, "homepage": "https://github.com/rollup/rollup-pluginutils#readme" } rollup-pluginutils-2.0.1/rollup.config.js000066400000000000000000000004701303277340700205320ustar00rootroot00000000000000var pkg = require('./package.json'); import buble from 'rollup-plugin-buble'; export default { entry: 'src/index.js', plugins: [ buble() ], external: [ 'path', 'estree-walker', 'micromatch' ], targets: [ { format: 'cjs', dest: pkg['main'] }, { format: 'es', dest: pkg['module'] } ] }; rollup-pluginutils-2.0.1/src/000077500000000000000000000000001303277340700162015ustar00rootroot00000000000000rollup-pluginutils-2.0.1/src/addExtension.js000066400000000000000000000002441303277340700211640ustar00rootroot00000000000000import { extname } from 'path'; export default function addExtension ( filename, ext = '.js' ) { if ( !extname( filename ) ) filename += ext; return filename; } rollup-pluginutils-2.0.1/src/attachScopes.js000066400000000000000000000062601303277340700211640ustar00rootroot00000000000000import { walk } from 'estree-walker'; const blockDeclarations = { 'const': true, 'let': true }; const extractors = { Identifier ( names, param ) { names.push( param.name ); }, ObjectPattern ( names, param ) { param.properties.forEach( prop => { extractors[ prop.key.type ]( names, prop.key ); }); }, ArrayPattern ( names, param ) { param.elements.forEach( element => { if ( element ) extractors[ element.type ]( names, element ); }); }, RestElement ( names, param ) { extractors[ param.argument.type ]( names, param.argument ); }, AssignmentPattern ( names, param ) { return extractors[ param.left.type ]( names, param.left ); } }; function extractNames ( param ) { let names = []; extractors[ param.type ]( names, param ); return names; } class Scope { constructor ( options ) { options = options || {}; this.parent = options.parent; this.isBlockScope = !!options.block; this.declarations = Object.create( null ); if ( options.params ) { options.params.forEach( param => { extractNames( param ).forEach( name => { this.declarations[ name ] = true; }); }); } } addDeclaration ( node, isBlockDeclaration, isVar ) { 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( name => { this.declarations[ name ] = true; }); } } contains ( name ) { return this.declarations[ name ] || ( this.parent ? this.parent.contains( name ) : false ); } } export default function attachScopes ( ast, propertyName = 'scope' ) { let scope = new Scope(); walk( ast, { 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' ) { const isBlockDeclaration = blockDeclarations[ node.kind ]; node.declarations.forEach( declaration => { scope.addDeclaration( declaration, isBlockDeclaration, true ); }); } let newScope; // 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 ( node ) { if ( node[ propertyName ] ) scope = scope.parent; } }); return scope; } rollup-pluginutils-2.0.1/src/createFilter.js000066400000000000000000000015431303277340700211530ustar00rootroot00000000000000import { resolve, sep } from 'path'; import mm from 'micromatch'; import ensureArray from './utils/ensureArray'; export default function createFilter ( include, exclude ) { const getMatcher = id => ( isRegexp( id ) ? id : { test: mm.matcher( resolve( id ) ) } ); include = ensureArray( include ).map( getMatcher ); exclude = ensureArray( exclude ).map( getMatcher ); return function ( id ) { if ( typeof id !== 'string' ) return false; if ( /\0/.test( id ) ) return false; id = id.split( sep ).join( '/' ); for ( let i = 0; i < exclude.length; ++i ) { const matcher = exclude[i]; if ( matcher.test( id ) ) return false; } for ( let i = 0; i < include.length; ++i ) { const matcher = include[i]; if ( matcher.test( id ) ) return true; } return !include.length; }; } function isRegexp ( val ) { return val instanceof RegExp; } rollup-pluginutils-2.0.1/src/index.js000066400000000000000000000003661303277340700176530ustar00rootroot00000000000000export { default as addExtension } from './addExtension'; export { default as attachScopes } from './attachScopes'; export { default as createFilter } from './createFilter'; export { default as makeLegalIdentifier } from './makeLegalIdentifier'; rollup-pluginutils-2.0.1/src/makeLegalIdentifier.js000066400000000000000000000023631303277340700224300ustar00rootroot00000000000000const 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( ' ' ); const builtins = 'arguments 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( ' ' ); let blacklisted = Object.create( null ); reservedWords.concat( builtins ).forEach( word => blacklisted[ word ] = true ); export default function makeLegalIdentifier ( str ) { str = str .replace( /-(\w)/g, ( _, letter ) => letter.toUpperCase() ) .replace( /[^$_a-zA-Z0-9]/g, '_' ); if ( /\d/.test( str[0] ) || blacklisted[ str ] ) str = `_${str}`; return str; } rollup-pluginutils-2.0.1/src/utils/000077500000000000000000000000001303277340700173415ustar00rootroot00000000000000rollup-pluginutils-2.0.1/src/utils/ensureArray.js000066400000000000000000000002301303277340700221720ustar00rootroot00000000000000export default function ensureArray ( thing ) { if ( Array.isArray( thing ) ) return thing; if ( thing == undefined ) return []; return [ thing ]; } rollup-pluginutils-2.0.1/test/000077500000000000000000000000001303277340700163715ustar00rootroot00000000000000rollup-pluginutils-2.0.1/test/test.js000066400000000000000000000122631303277340700177120ustar00rootroot00000000000000var path = require( 'path' ); var assert = require( 'assert' ); var utils = require( '..' ); describe( 'rollup-pluginutils', function () { describe( 'createFilter', function () { var createFilter = utils.createFilter; it( 'includes by default', function () { var filter = createFilter(); assert.ok( filter( path.resolve( 'x' ) ) ); }); it( 'excludes IDs that are not included, if include.length > 0', function () { var filter = createFilter([ 'y' ]); assert.ok( !filter( path.resolve( 'x' ) ) ); assert.ok( filter( path.resolve( 'y' ) ) ); }); it( 'excludes IDs explicitly', function () { var filter = createFilter( null, [ 'y' ]); assert.ok( filter( path.resolve( 'x' ) ) ); assert.ok( !filter( path.resolve( 'y' ) ) ); }); it( 'handles non-array arguments', function () { var filter = createFilter( 'foo/*', 'foo/baz' ); assert.ok( filter( path.resolve( 'foo/bar' ) ) ); assert.ok( !filter( path.resolve( 'foo/baz' ) ) ); }); it( 'negation patterns', function () { var filter = createFilter([ 'a/!(b)/c' ]); assert.ok( filter( path.resolve( 'a/d/c' ) ) ); assert.ok( !filter( path.resolve( 'a/b/c' ) ) ); }); it( 'excludes non-string IDs', function () { var filter = createFilter( null, null ); assert.ok( !filter({}) ); }); it( 'excludes strings beginning with NUL', function () { var filter = createFilter( null, null ); assert.ok( !filter( '\0someid' ) ); }); it( 'includes with regexp', function () { var filter = createFilter(['a/!(b)/c' , /\.js$/ ]); assert.ok( filter( path.resolve( 'a/d/c' ) ) ); assert.ok( !filter( path.resolve( 'a/b/c' ) ) ); assert.ok( filter( path.resolve( 'a.js' ) ) ); assert.ok( filter( path.resolve( 'a/b.js' ) ) ); assert.ok( !filter( path.resolve( 'a/b.jsx' ) ) ); }) it ('excludes with regexp', function () { var filter = createFilter(['a/!(b)/c' , /\.js$/ ], /\.js$/); assert.ok( filter( path.resolve( 'a/d/c' ) ) ); assert.ok( !filter( path.resolve( 'a/b/c' ) ) ); assert.ok( !filter( path.resolve( 'a.js' ) ) ); assert.ok( !filter( path.resolve( 'a/b.js' ) ) ); assert.ok( !filter( path.resolve( 'a/b.jsx' ) ) ); }) }); describe( 'addExtension', function () { var addExtension = utils.addExtension; it( 'adds .js to an ID without an extension', function () { assert.equal( addExtension( 'foo' ), 'foo.js' ); }); it( 'ignores file with existing extension', function () { assert.equal( addExtension( 'foo.js' ), 'foo.js' ); assert.equal( addExtension( 'foo.json' ), 'foo.json' ); }); it( 'ignores file with trailing dot', function () { assert.equal( addExtension( 'foo.' ), 'foo.' ); }); it( 'ignores leading .', function () { assert.equal( addExtension( './foo' ), './foo.js' ); assert.equal( addExtension( './foo.js' ), './foo.js' ); }); it( 'adds a custom extension', function () { assert.equal( addExtension( 'foo', '.wut' ), 'foo.wut' ); assert.equal( addExtension( 'foo.lol', '.wut' ), 'foo.lol' ); }); }); describe( 'attachScopes', function () { var attachScopes = utils.attachScopes; it( 'attaches a scope to the top level', function () { var ast = { "type": "Program", "start": 0, "end": 8, "body": [ { "type": "VariableDeclaration", "start": 0, "end": 8, "declarations": [ { "type": "VariableDeclarator", "start": 4, "end": 7, "id": { "type": "Identifier", "start": 4, "end": 7, "name": "foo" }, "init": null } ], "kind": "var" } ], "sourceType": "module" }; var scope = attachScopes( ast, 'scope' ); assert.ok( scope.contains( 'foo' ) ); assert.ok( !scope.contains( 'bar' ) ); }); it( 'adds multiple declarators from a single var declaration', function () { var ast = { "type": "Program", "start": 0, "end": 13, "body": [ { "type": "VariableDeclaration", "start": 0, "end": 13, "declarations": [ { "type": "VariableDeclarator", "start": 4, "end": 7, "id": { "type": "Identifier", "start": 4, "end": 7, "name": "foo" }, "init": null }, { "type": "VariableDeclarator", "start": 9, "end": 12, "id": { "type": "Identifier", "start": 9, "end": 12, "name": "bar" }, "init": null } ], "kind": "var" } ], "sourceType": "module" }; var scope = attachScopes( ast, 'scope' ); assert.ok( scope.contains( 'foo' ) ); assert.ok( scope.contains( 'bar' ) ); }); // TODO more tests }); describe( 'makeLegalIdentifier', function () { var makeLegalIdentifier = utils.makeLegalIdentifier; it( 'camel-cases names', function () { assert.equal( makeLegalIdentifier( 'foo-bar' ), 'fooBar' ); }); it( 'replaces keywords', function () { assert.equal( makeLegalIdentifier( 'typeof' ), '_typeof' ); }); it( 'blacklists arguments (https://github.com/rollup/rollup/issues/871)', function () { assert.equal( makeLegalIdentifier( 'arguments' ), '_arguments' ); }); }); });