Skip to content

replace: Should property accessors be replaced or not? #2011

Description

@GrygrFlzr

Expected Behavior

Given this test added in #1938 merged into 6.0.3:

// Should be replaced - typeof window followed by dot
if (typeof window.document !== 'undefined') {
console.log('has document');
}

description: 'should not replace when followed by valid identifier characters',
options: {
values: {
'typeof window': '"undefined"'
},
preventAssignment: true
}

Comment strongly suggests it should produce the following replacement:

// Should be replaced - typeof window followed by dot
if ("undefined".document !== 'undefined') {
  console.log('has document');
}

Actual Behavior

The snapshot stored by vitest does not actually replace this:

// Should be replaced - "undefined" followed by dot␊
if (typeof window.document !== 'undefined') {␊
console.log('has document');␊
}`

And, there is an older fixture + snapshot which directly conflicts with this interpretation, under replace-nothing:

console.log('as-it'); // eslint-disable-line
console.log(typeof window.document); // eslint-disable-line

description: 'replaces nothing',
options: {
'typeof window': `"object"`
}

Vitest Snapshot:
## replace-nothing: replaces nothing
> Snapshot 1
`console.log('as-it'); // eslint-disable-line␊
console.log(typeof window.document); // eslint-disable-line`

Additional Information

The older PR which introduced the replace-nothing test was #903 which had the following comment:

Personally, I believe it's dangerous to replace VERSION in the case you highlighted by default:

if (VERSION.startsWith('foo')) {
  // things
}

In the case above with the previous default to match VERSION when used with a . or nested access, you would need to know all of your dependencies' code, not to mess up when replacing.

Originally posted by @o-alexandrov in #903 (comment)

What is the current intended behavior? I don't think these two tests can co-exist unless they're testing differently configured delimiters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions