Skip to content

Commit

Permalink
Merge pull request #19 from ChristianMurphy/type/node-16-esm-types
Browse files Browse the repository at this point in the history
types: add export to package.json, test with strict mode and node16 resolution
  • Loading branch information
Rich-Harris authored Jan 25, 2023
2 parents 2ff43ec + 208a178 commit 14eb54c
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 88 deletions.
179 changes: 108 additions & 71 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"module": "src/index.js",
"type": "module",
"exports": {
"types": "./types/index.js",
"import": "./src/index.js"
},
"types": "types/index.d.ts",
Expand All @@ -15,9 +16,8 @@
"types"
],
"devDependencies": {
"@types/estree": "0.0.39",
"acorn": "^7.0.0",
"typescript": "^4.1.2",
"acorn": "^8.0.0",
"typescript": "^4.9.0",
"uvu": "^0.5.1"
},
"scripts": {
Expand All @@ -26,6 +26,7 @@
},
"license": "MIT",
"dependencies": {
"@types/estree": "^1.0.0",
"estree-walker": "^3.0.0",
"is-reference": "^3.0.0"
}
Expand Down
29 changes: 18 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ export function analyze(expression) {

/** @type {[Scope, import('estree').Identifier][]} */
const references = [];
/** @type {Scope} */
let current_scope = scope;

walk(expression, {
/**
* @param {Node} node
* @param {any} parent
*/
enter(node, parent) {
switch (node.type) {
case 'Identifier':
if (is_reference(node, parent)) {
if (parent && is_reference(node, parent)) {
references.push([current_scope, node]);
}
break;
Expand Down Expand Up @@ -80,16 +77,17 @@ export function analyze(expression) {

if (node.param) {
extract_names(node.param).forEach(name => {
current_scope.declarations.set(name, node.param);
if (node.param) {
current_scope.declarations.set(name, node.param);
}
});
}
break;
}
},

/** @param {Node} node */
leave(node) {
if (map.has(node)) {
if (map.has(node) && current_scope !== null && current_scope.parent) {
current_scope = current_scope.parent;
}
}
Expand All @@ -110,7 +108,6 @@ export function analyze(expression) {
}

/**
*
* @param {Scope} scope
* @param {string} name
*/
Expand All @@ -120,6 +117,10 @@ function add_reference(scope, name) {
}

export class Scope {
/**
* @param {Scope | null} parent
* @param {boolean} block
*/
constructor(parent, block) {
/** @type {Scope | null} */
this.parent = parent;
Expand All @@ -137,7 +138,9 @@ export class Scope {
this.references = new Set();
}

/** @param {import('estree').VariableDeclaration | import('estree').ClassDeclaration} node */
/**
* @param {import('estree').VariableDeclaration | import('estree').ClassDeclaration} node
*/
add_declaration(node) {
if (node.type === 'VariableDeclaration') {
if (node.kind === 'var' && this.block && this.parent) {
Expand Down Expand Up @@ -224,7 +227,11 @@ export function extract_identifiers(param, nodes = []) {
if (element) extract_identifiers(element, nodes);
};

param.elements.forEach(handle_element);
param.elements.forEach((element) => {
if (element) {
handle_element(element)
}
});
break;

case 'RestElement':
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as uvu from 'uvu';
import * as assert from 'uvu/assert';
import acorn from 'acorn';
import * as acorn from 'acorn';
import { analyze, extract_identifiers, extract_names } from '../src/index.js';
import { walk } from 'estree-walker';

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"strict": true,
"module": "node16",
"target": "es2017",
"declaration": true,
"emitDeclarationOnly": true,
"moduleResolution": "node",
"outDir": "types"
},

Expand Down

0 comments on commit 14eb54c

Please sign in to comment.