rollup-pluginutils
Advanced tools
Comparing version 2.5.0 to 2.6.0
# rollup-pluginutils changelog | ||
## 2.6.0 | ||
*2019-04-04* | ||
* Add `extractAssignedNames` ([#59](https://github.com/rollup/rollup-pluginutils/issues/59)) | ||
* Provide dedicated TypeScript typings file ([#58](https://github.com/rollup/rollup-pluginutils/issues/58)) | ||
## 2.5.0 | ||
@@ -4,0 +9,0 @@ *2019-03-18* |
@@ -9,3 +9,3 @@ 'use strict'; | ||
function addExtension(filename, ext) { | ||
var addExtension = function addExtension(filename, ext) { | ||
if (ext === void 0) { ext = '.js'; } | ||
@@ -15,17 +15,22 @@ if (!path.extname(filename)) | ||
return filename; | ||
} | ||
}; | ||
var blockDeclarations = { | ||
const: true, | ||
let: true | ||
}; | ||
var extractors = { | ||
Literal: function (names, param) { | ||
names.push(param.value); | ||
ArrayPattern: function (names, param) { | ||
for (var _i = 0, _a = param.elements; _i < _a.length; _i++) { | ||
var element = _a[_i]; | ||
if (element) | ||
extractors[element.type](names, element); | ||
} | ||
}, | ||
AssignmentPattern: function (names, param) { | ||
extractors[param.left.type](names, param.left); | ||
}, | ||
Identifier: function (names, param) { | ||
names.push(param.name); | ||
}, | ||
MemberExpression: function () { }, | ||
ObjectPattern: function (names, param) { | ||
param.properties.forEach(function (prop) { | ||
for (var _i = 0, _a = param.properties; _i < _a.length; _i++) { | ||
var prop = _a[_i]; | ||
if (prop.type === 'RestElement') { | ||
@@ -35,28 +40,24 @@ extractors.RestElement(names, prop); | ||
else { | ||
extractors[(prop.value || prop.key).type](names, prop.value || prop.key); | ||
extractors[prop.value.type](names, prop.value); | ||
} | ||
}); | ||
} | ||
}, | ||
ArrayPattern: function (names, param) { | ||
param.elements.forEach(function (element) { | ||
if (element) | ||
extractors[element.type](names, element); | ||
}); | ||
}, | ||
RestElement: function (names, param) { | ||
extractors[param.argument.type](names, param.argument); | ||
}, | ||
AssignmentPattern: function (names, param) { | ||
return extractors[param.left.type](names, param.left); | ||
} | ||
}; | ||
function extractNames(param) { | ||
var extractAssignedNames = function extractAssignedNames(param) { | ||
var names = []; | ||
extractors[param.type](names, param); | ||
return names; | ||
} | ||
}; | ||
var blockDeclarations = { | ||
const: true, | ||
let: true | ||
}; | ||
var Scope = /** @class */ (function () { | ||
function Scope(options) { | ||
var _this = this; | ||
if (options === void 0) { options = {}; } | ||
var _this = this; | ||
this.parent = options.parent; | ||
@@ -67,3 +68,3 @@ this.isBlockScope = !!options.block; | ||
options.params.forEach(function (param) { | ||
extractNames(param).forEach(function (name) { | ||
extractAssignedNames(param).forEach(function (name) { | ||
_this.declarations[name] = true; | ||
@@ -82,3 +83,3 @@ }); | ||
else if (node.id) { | ||
extractNames(node.id).forEach(function (name) { | ||
extractAssignedNames(node.id).forEach(function (name) { | ||
_this.declarations[name] = true; | ||
@@ -93,3 +94,3 @@ }); | ||
}()); | ||
function attachScopes(ast, propertyName) { | ||
var attachScopes = function attachScopes(ast, propertyName) { | ||
if (propertyName === void 0) { propertyName = 'scope'; } | ||
@@ -155,3 +156,3 @@ var scope = new Scope(); | ||
return scope; | ||
} | ||
}; | ||
@@ -166,5 +167,5 @@ function ensureArray(thing) { | ||
function createFilter(include, exclude) { | ||
var createFilter = function createFilter(include, exclude) { | ||
var getMatcher = function (id) { | ||
return isRegexp(id) | ||
return id instanceof RegExp | ||
? id | ||
@@ -197,6 +198,3 @@ : { | ||
}; | ||
} | ||
function isRegexp(val) { | ||
return val instanceof RegExp; | ||
} | ||
}; | ||
@@ -207,3 +205,3 @@ 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'; | ||
forbiddenIdentifiers.add(''); | ||
function makeLegalIdentifier(str) { | ||
var makeLegalIdentifier = function makeLegalIdentifier(str) { | ||
str = str.replace(/-(\w)/g, function (_, letter) { return letter.toUpperCase(); }).replace(/[^$_a-zA-Z0-9]/g, '_'); | ||
@@ -214,3 +212,3 @@ if (/\d/.test(str[0]) || forbiddenIdentifiers.has(str)) { | ||
return str; | ||
} | ||
}; | ||
@@ -254,4 +252,3 @@ function serializeArray(arr, indent, baseIndent) { | ||
} | ||
// convert data object into separate named exports (and default) | ||
function dataToNamedExports(data, options) { | ||
var dataToEsm = function dataToEsm(data, options) { | ||
if (options === void 0) { options = {}; } | ||
@@ -284,3 +281,3 @@ var t = options.compact ? '' : 'indent' in options ? options.indent : '\t'; | ||
return (namedExportCode + ("export default" + _ + "{" + n + t + defaultExportRows.join("," + n + t) + n + "};" + n)); | ||
} | ||
}; | ||
@@ -290,3 +287,4 @@ exports.addExtension = addExtension; | ||
exports.createFilter = createFilter; | ||
exports.dataToEsm = dataToEsm; | ||
exports.extractAssignedNames = extractAssignedNames; | ||
exports.makeLegalIdentifier = makeLegalIdentifier; | ||
exports.dataToEsm = dataToNamedExports; |
@@ -5,3 +5,3 @@ import { extname, sep, resolve } from 'path'; | ||
function addExtension(filename, ext) { | ||
var addExtension = function addExtension(filename, ext) { | ||
if (ext === void 0) { ext = '.js'; } | ||
@@ -11,17 +11,22 @@ if (!extname(filename)) | ||
return filename; | ||
} | ||
}; | ||
var blockDeclarations = { | ||
const: true, | ||
let: true | ||
}; | ||
var extractors = { | ||
Literal: function (names, param) { | ||
names.push(param.value); | ||
ArrayPattern: function (names, param) { | ||
for (var _i = 0, _a = param.elements; _i < _a.length; _i++) { | ||
var element = _a[_i]; | ||
if (element) | ||
extractors[element.type](names, element); | ||
} | ||
}, | ||
AssignmentPattern: function (names, param) { | ||
extractors[param.left.type](names, param.left); | ||
}, | ||
Identifier: function (names, param) { | ||
names.push(param.name); | ||
}, | ||
MemberExpression: function () { }, | ||
ObjectPattern: function (names, param) { | ||
param.properties.forEach(function (prop) { | ||
for (var _i = 0, _a = param.properties; _i < _a.length; _i++) { | ||
var prop = _a[_i]; | ||
if (prop.type === 'RestElement') { | ||
@@ -31,28 +36,24 @@ extractors.RestElement(names, prop); | ||
else { | ||
extractors[(prop.value || prop.key).type](names, prop.value || prop.key); | ||
extractors[prop.value.type](names, prop.value); | ||
} | ||
}); | ||
} | ||
}, | ||
ArrayPattern: function (names, param) { | ||
param.elements.forEach(function (element) { | ||
if (element) | ||
extractors[element.type](names, element); | ||
}); | ||
}, | ||
RestElement: function (names, param) { | ||
extractors[param.argument.type](names, param.argument); | ||
}, | ||
AssignmentPattern: function (names, param) { | ||
return extractors[param.left.type](names, param.left); | ||
} | ||
}; | ||
function extractNames(param) { | ||
var extractAssignedNames = function extractAssignedNames(param) { | ||
var names = []; | ||
extractors[param.type](names, param); | ||
return names; | ||
} | ||
}; | ||
var blockDeclarations = { | ||
const: true, | ||
let: true | ||
}; | ||
var Scope = /** @class */ (function () { | ||
function Scope(options) { | ||
var _this = this; | ||
if (options === void 0) { options = {}; } | ||
var _this = this; | ||
this.parent = options.parent; | ||
@@ -63,3 +64,3 @@ this.isBlockScope = !!options.block; | ||
options.params.forEach(function (param) { | ||
extractNames(param).forEach(function (name) { | ||
extractAssignedNames(param).forEach(function (name) { | ||
_this.declarations[name] = true; | ||
@@ -78,3 +79,3 @@ }); | ||
else if (node.id) { | ||
extractNames(node.id).forEach(function (name) { | ||
extractAssignedNames(node.id).forEach(function (name) { | ||
_this.declarations[name] = true; | ||
@@ -89,3 +90,3 @@ }); | ||
}()); | ||
function attachScopes(ast, propertyName) { | ||
var attachScopes = function attachScopes(ast, propertyName) { | ||
if (propertyName === void 0) { propertyName = 'scope'; } | ||
@@ -151,3 +152,3 @@ var scope = new Scope(); | ||
return scope; | ||
} | ||
}; | ||
@@ -162,5 +163,5 @@ function ensureArray(thing) { | ||
function createFilter(include, exclude) { | ||
var createFilter = function createFilter(include, exclude) { | ||
var getMatcher = function (id) { | ||
return isRegexp(id) | ||
return id instanceof RegExp | ||
? id | ||
@@ -193,6 +194,3 @@ : { | ||
}; | ||
} | ||
function isRegexp(val) { | ||
return val instanceof RegExp; | ||
} | ||
}; | ||
@@ -203,3 +201,3 @@ 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'; | ||
forbiddenIdentifiers.add(''); | ||
function makeLegalIdentifier(str) { | ||
var makeLegalIdentifier = function makeLegalIdentifier(str) { | ||
str = str.replace(/-(\w)/g, function (_, letter) { return letter.toUpperCase(); }).replace(/[^$_a-zA-Z0-9]/g, '_'); | ||
@@ -210,3 +208,3 @@ if (/\d/.test(str[0]) || forbiddenIdentifiers.has(str)) { | ||
return str; | ||
} | ||
}; | ||
@@ -250,4 +248,3 @@ function serializeArray(arr, indent, baseIndent) { | ||
} | ||
// convert data object into separate named exports (and default) | ||
function dataToNamedExports(data, options) { | ||
var dataToEsm = function dataToEsm(data, options) { | ||
if (options === void 0) { options = {}; } | ||
@@ -280,4 +277,4 @@ var t = options.compact ? '' : 'indent' in options ? options.indent : '\t'; | ||
return (namedExportCode + ("export default" + _ + "{" + n + t + defaultExportRows.join("," + n + t) + n + "};" + n)); | ||
} | ||
}; | ||
export { addExtension, attachScopes, createFilter, makeLegalIdentifier, dataToNamedExports as dataToEsm }; | ||
export { addExtension, attachScopes, createFilter, dataToEsm, extractAssignedNames, makeLegalIdentifier }; |
{ | ||
"name": "rollup-pluginutils", | ||
"description": "Functionality commonly needed by Rollup plugins", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"main": "dist/pluginutils.cjs.js", | ||
"module": "dist/pluginutils.es.js", | ||
"jsnext:main": "dist/pluginutils.es.js", | ||
"typings": "src/index.d.ts", | ||
"typings": "dist/pluginutils.d.ts", | ||
"files": [ | ||
@@ -18,12 +18,13 @@ "src", | ||
"@types/micromatch": "^3.1.0", | ||
"@types/node": "^11.11.3", | ||
"@types/node": "^11.13.0", | ||
"husky": "^1.3.1", | ||
"jest": "^24.5.0", | ||
"jest": "^24.7.0", | ||
"lint-staged": "^8.1.5", | ||
"prettier": "^1.16.4", | ||
"rollup": "^1.6.0", | ||
"rollup-plugin-typescript": "^1.0.0", | ||
"ts-jest": "^24.0.0", | ||
"tslint": "^5.14.0", | ||
"typescript": "^3.3.3333", | ||
"rollup": "^1.8.0", | ||
"rollup-plugin-typescript": "^1.0.1", | ||
"shx": "^0.3.2", | ||
"ts-jest": "^24.0.1", | ||
"tslint": "^5.15.0", | ||
"typescript": "^3.4.1", | ||
"typescript-eslint-parser": "^22.0.0" | ||
@@ -33,3 +34,3 @@ }, | ||
"test": "jest", | ||
"build": "rollup -c", | ||
"build": "rollup -c && shx cp src/pluginutils.d.ts dist/pluginutils.d.ts", | ||
"lint": "npm run lint:nofix -- --fix", | ||
@@ -36,0 +37,0 @@ "lint:nofix": "tslint --project .", |
@@ -40,3 +40,2 @@ # rollup-pluginutils | ||
import { attachScopes } from 'rollup-pluginutils'; | ||
import { parse } from 'acorn'; | ||
import { walk } from 'estree-walker'; | ||
@@ -47,6 +46,3 @@ | ||
transform ( code ) { | ||
const ast = parse( code, { | ||
ecmaVersion: 6, | ||
sourceType: 'module' | ||
}); | ||
const ast = this.parse( code ); | ||
@@ -118,3 +114,3 @@ let scope = attachScopes( ast, 'scope' ); | ||
to: ['treeshake'] | ||
}, options = { | ||
}, { | ||
compact: false, | ||
@@ -134,5 +130,32 @@ indent: '\t', | ||
### extractAssignedNames | ||
Extract the names of all assignment targets from patterns. | ||
```js | ||
import { extractAssignedNames } from 'rollup-pluginutils'; | ||
import { walk } from 'estree-walker'; | ||
export default function myPlugin ( options = {} ) { | ||
return { | ||
transform ( code ) { | ||
const ast = this.parse( code ); | ||
walk( ast, { | ||
enter ( node ) { | ||
if ( node.type === 'VariableDeclarator' ) { | ||
const declaredNames = extractAssignedNames(node.id); | ||
// do something with the declared names | ||
// e.g. for `const {x, y: z} = ... => declaredNames = ['x', 'z'] | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
``` | ||
## License | ||
MIT |
import { extname } from 'path'; | ||
import { AddExtension } from './pluginutils'; | ||
export default function addExtension(filename: string, ext: string = '.js'): string { | ||
const addExtension: AddExtension = function addExtension(filename, ext = '.js') { | ||
if (!extname(filename)) filename += ext; | ||
return filename; | ||
} | ||
}; | ||
export { addExtension as default }; |
import { Node, walk } from 'estree-walker'; | ||
import extractAssignedNames from './extractAssignedNames'; | ||
import { AttachedScope, AttachScopes } from './pluginutils'; | ||
@@ -8,49 +10,4 @@ const blockDeclarations = { | ||
interface Extractors { | ||
[key: string]: (names: Array<string>, param: Node) => void; | ||
} | ||
const extractors: Extractors = { | ||
Literal(names: Array<string>, param: Node) { | ||
names.push(param.value as string); | ||
}, | ||
Identifier(names: Array<string>, param: Node) { | ||
names.push(param.name); | ||
}, | ||
ObjectPattern(names: Array<string>, param: Node) { | ||
param.properties.forEach((prop: Node) => { | ||
if (prop.type === 'RestElement') { | ||
extractors.RestElement(names, prop); | ||
} else { | ||
extractors[(prop.value || prop.key).type](names, prop.value || prop.key); | ||
} | ||
}); | ||
}, | ||
ArrayPattern(names: Array<string>, param: Node) { | ||
param.elements.forEach((element: Node) => { | ||
if (element) extractors[element.type](names, element); | ||
}); | ||
}, | ||
RestElement(names: Array<string>, param: Node) { | ||
extractors[param.argument.type](names, param.argument); | ||
}, | ||
AssignmentPattern(names: Array<string>, param: Node) { | ||
return extractors[param.left.type](names, param.left); | ||
} | ||
}; | ||
function extractNames(param: Node): Array<string> { | ||
const names: Array<string> = []; | ||
extractors[param.type](names, param); | ||
return names; | ||
} | ||
interface ScopeOptions { | ||
parent?: Scope; | ||
parent?: AttachedScope; | ||
block?: boolean; | ||
@@ -60,4 +17,4 @@ params?: Array<Node>; | ||
class Scope { | ||
parent?: Scope; | ||
class Scope implements AttachedScope { | ||
parent?: AttachedScope; | ||
isBlockScope: boolean; | ||
@@ -74,3 +31,3 @@ declarations: { [key: string]: boolean }; | ||
options.params.forEach(param => { | ||
extractNames(param).forEach(name => { | ||
extractAssignedNames(param).forEach(name => { | ||
this.declarations[name] = true; | ||
@@ -88,3 +45,3 @@ }); | ||
} else if (node.id) { | ||
extractNames(node.id).forEach(name => { | ||
extractAssignedNames(node.id).forEach(name => { | ||
this.declarations[name] = true; | ||
@@ -100,3 +57,3 @@ }); | ||
export default function attachScopes(ast: Node, propertyName: string = 'scope'): Scope { | ||
const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'scope') { | ||
let scope = new Scope(); | ||
@@ -122,3 +79,3 @@ | ||
let newScope: Scope | undefined; | ||
let newScope: AttachedScope | undefined; | ||
@@ -172,2 +129,4 @@ // create new function scope | ||
return scope; | ||
} | ||
}; | ||
export { attachScopes as default }; |
import * as mm from 'micromatch'; | ||
import { resolve, sep } from 'path'; | ||
import { CreateFilter } from './pluginutils'; | ||
import ensureArray from './utils/ensureArray'; | ||
export default function createFilter( | ||
include?: Array<string | RegExp> | string | RegExp | null, | ||
exclude?: Array<string | RegExp> | string | RegExp | null | ||
): (id: string | any) => boolean { | ||
const createFilter: CreateFilter = function createFilter(include?, exclude?) { | ||
const getMatcher = (id: string | RegExp) => | ||
isRegexp(id) | ||
id instanceof RegExp | ||
? id | ||
@@ -41,6 +39,4 @@ : { | ||
}; | ||
} | ||
}; | ||
function isRegexp(val: any): val is RegExp { | ||
return val instanceof RegExp; | ||
} | ||
export { createFilter as default }; |
import makeLegalIdentifier from './makeLegalIdentifier'; | ||
import { DataToEsm } from './pluginutils'; | ||
@@ -42,12 +43,3 @@ export type Indent = string | null | undefined; | ||
export interface Options { | ||
compact?: boolean; | ||
indent?: string; | ||
namedExports?: boolean; | ||
objectShorthand?: boolean; | ||
preferConst?: boolean; | ||
} | ||
// convert data object into separate named exports (and default) | ||
export default function dataToNamedExports(data: any, options: Options = {}): string { | ||
const dataToEsm: DataToEsm = function dataToEsm(data, options = {}) { | ||
const t = options.compact ? '' : 'indent' in options ? options.indent : '\t'; | ||
@@ -88,2 +80,4 @@ const _ = options.compact ? '' : ' '; | ||
); | ||
} | ||
}; | ||
export { dataToEsm as default }; |
@@ -6,1 +6,2 @@ export { default as addExtension } from './addExtension'; | ||
export { default as dataToEsm } from './dataToEsm'; | ||
export { default as extractAssignedNames } from './extractAssignedNames'; |
@@ -0,1 +1,3 @@ | ||
import { MakeLegalIdentifier } from './pluginutils'; | ||
const reservedWords = | ||
@@ -9,3 +11,3 @@ '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'; | ||
export default function makeLegalIdentifier(str: string): string { | ||
export const makeLegalIdentifier: MakeLegalIdentifier = function makeLegalIdentifier(str) { | ||
str = str.replace(/-(\w)/g, (_, letter) => letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, '_'); | ||
@@ -18,2 +20,4 @@ | ||
return str; | ||
} | ||
}; | ||
export { makeLegalIdentifier as default }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42217
15
867
158
15