Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track using violations #1

Merged
merged 2 commits into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,13 @@ describe("remove-undefined-if-possible-plugin", () => {
expect(transform(source)).toBe(expected);
});

it("should remove nested var-assignments if not referenced before", () => {
it("idk what this is", () => {
const source = unpad(`
function foo() {
a = 3;
var { a: aa, b: bb } = undefined;
}`);
const expected = unpad(`
function foo() {
a = 3;
var { a: aa, b: bb };
}`);
expect(transform(source)).toBe(expected);
expect(transform(source)).toBe(source);
});

it("should remove let-assignments in inner blocks", () => {
Expand All @@ -88,11 +83,25 @@ describe("remove-undefined-if-possible-plugin", () => {
expect(transform(source)).toBe(source);
});

it("should not remove var-assignments in loops", () => {
it("should remove var-assignments in loops", () => {
const source = unpad(`
for (var a = undefined;;) {
var b = undefined;
}`);
const expected = unpad(`
for (var a;;) {
var b;
}`);
expect(transform(source)).toBe(expected);
});

it("should not remove var-assignments in loops 2", () => {
const source = unpad(`
for (var a;;) {
var b = undefined;
console.log(b);
b = 3;
}`);
expect(transform(source)).toBe(source);
});

Expand All @@ -113,4 +122,63 @@ describe("remove-undefined-if-possible-plugin", () => {
}`);
expect(transform(source)).toBe(source);
});

it("should not remove ...", () => {
const source = unpad(`
function foo() {
bar();
var x = undefined;
console.log(x);
function bar() {
x = 3;
}
}`);
expect(transform(source)).toBe(source);
});

it("should remove ...", () => {
const source = unpad(`
function foo() {
var x = undefined;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
const expected = unpad(`
function foo() {
var x;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
expect(transform(source)).toBe(expected);
});

it("should remove ...", () => {
const source = unpad(`
foo();
function foo() {
var x = undefined;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
const expected = unpad(`
foo();
function foo() {
var x;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
expect(transform(source)).toBe(expected);
});
});
85 changes: 50 additions & 35 deletions packages/babel-plugin-transform-remove-undefined/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ function removeRvalIfUndefined(declaratorPath) {
}
}

function areAllBindingsNotSeen(declaratorPath, seenNames, t) {
const id = declaratorPath.node.id;
const names = t.getBindingIdentifiers(id);
for (const name in names) {
if (seenNames.has(name)) {
return false;
}
}
return true;
function getLoopParent(path, scopeParent) {
const parent = path.findParent((p) => p.isLoop() || p === scopeParent);
// don't traverse higher than the function the var is defined in.
return parent === scopeParent ? null : parent;
}

function getFunctionParent(path, scopeParent) {
const parent = path.findParent((p) => p.isFunction());
// don't traverse higher than the function the var is defined in.
return parent === scopeParent ? null : parent;
}

module.exports = function({ types: t }) {
Expand All @@ -25,27 +26,6 @@ module.exports = function({ types: t }) {
return {
name: "remove-undefined-if-possible",
visitor: {
Program: {
enter() {
names = new Set();
functionNesting = 0;
},
exit() {
names = null;
functionNesting = 0;
},
},
Function: {
enter() {
functionNesting++;
},
exit() {
functionNesting--;
},
},
Identifier(path) {
names.add(path.node.name);
},
ReturnStatement(path) {
if (path.node.argument !== null) {
const rval = path.get("argument")
Expand All @@ -55,6 +35,7 @@ module.exports = function({ types: t }) {
}
}
},

VariableDeclaration(path) {
switch (path.node.kind) {
case "const":
Expand All @@ -65,12 +46,46 @@ module.exports = function({ types: t }) {
}
break;
case "var":
if (functionNesting === 0) {
// ignore global vars
break;
}
const { node, scope } = path;
for (const declarator of path.get("declarations")) {
if (areAllBindingsNotSeen(declarator, names, t)) {
const binding = scope.getBinding(declarator.node.id.name);
if (!binding) {
continue;
}

const { start } = node;
Copy link
Owner

@shinew shinew Oct 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the start and end attributes won't work for nodes that are runtime-generated: https://astexplorer.net/#/Pl6ASeZRlL. This is still probably better than any other way of determining relative ordering though, if we add some existence checks for these attributes for safety.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I thought doing a #replaceWith auto-inherited the location. Looks like you have to t.inherits(newNode, oldNode) in order to get that.

const scopeParent = declarator.getFunctionParent();

const violation = binding.constantViolations.some(v => {
const violationStart = v.node.start;
if (violationStart < start) {
return true;
}

for (let func = getFunctionParent(v, scopeParent); func; func = getFunctionParent(func, scopeParent)) {
const id = func.node.id;
const binding = id && scope.getBinding(id.name);

if (!binding) {
continue;
}

const funcViolation = binding.referencePaths.some((p) => {
return p.node.start < start;
Copy link
Owner

@shinew shinew Oct 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this fail for references through multiple levels of indirection?

e.g.

function foo() {
  bar();
  var a = undefined;
  console.log(a);
  function bar() {
    g();
  }
  function g() {
    a = 4;
  }
}

We cannot remove the = undefined in this case. As I understand this, it only checks for 1 level of referencing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll be easy enough to add.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, but it's unoptimized.

});
if (funcViolation) {
return true;
}
}

for (let loop = getLoopParent(declarator, scopeParent); loop; loop = getLoopParent(loop, scopeParent)) {
if (loop.node.end > violationStart) {
return true;
}
}
});

if (!violation) {
removeRvalIfUndefined(declarator);
}
}
Expand Down