Skip to content

Commit e045391

Browse files
committed
fix(linter/plugins): error on JS plugin with reserved name (#14226)
Raise an error if a JS plugin has same name as a built-in plugin e.g. `import`. It'd be impossible to know whether `import/rule-name` was referring to the built-in rule, or the JS plugin's rule of same name.
1 parent d30159b commit e045391

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

apps/oxlint/test/e2e.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ describe('oxlint CLI', () => {
6565
await testFixture('missing_custom_plugin');
6666
});
6767

68+
it('should report an error if a custom plugin has a reserved name', async () => {
69+
await testFixture('reserved_name');
70+
});
71+
6872
it('should report an error if a custom plugin throws an error during import', async () => {
6973
await testFixture('custom_plugin_import_error');
7074
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"jsPlugins": ["./plugin.ts"]
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Exit code
2+
1
3+
4+
# stdout
5+
```
6+
Failed to parse configuration file.
7+
8+
x Plugin name 'import' is reserved, and cannot be used for JS plugins
9+
```
10+
11+
# stderr
12+
```
13+
WARNING: JS plugins are experimental and not subject to semver.
14+
Breaking changes are possible while JS plugins support is under development.
15+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { Plugin } from '../../../dist/index.js';
2+
3+
const plugin: Plugin = {
4+
meta: {
5+
name: 'import', // Reserved name
6+
},
7+
rules: {},
8+
};
9+
10+
export default plugin;

crates/oxc_linter/src/config/config_builder.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,12 @@ impl ConfigStoreBuilder {
532532

533533
match result {
534534
PluginLoadResult::Success { name, offset, rule_names } => {
535-
external_plugin_store.register_plugin(plugin_path, name, offset, rule_names);
536-
Ok(())
535+
if name != "eslint" && LintPlugins::from(name.as_str()) == LintPlugins::empty() {
536+
external_plugin_store.register_plugin(plugin_path, name, offset, rule_names);
537+
Ok(())
538+
} else {
539+
Err(ConfigBuilderError::ReservedExternalPluginName { plugin_name: name })
540+
}
537541
}
538542
PluginLoadResult::Failure(e) => Err(ConfigBuilderError::PluginLoadFailed {
539543
plugin_specifier: plugin_specifier.to_string(),
@@ -580,6 +584,9 @@ pub enum ConfigBuilderError {
580584
NoExternalLinterConfigured {
581585
plugin_specifier: String,
582586
},
587+
ReservedExternalPluginName {
588+
plugin_name: String,
589+
},
583590
}
584591

585592
impl Display for ConfigBuilderError {
@@ -610,6 +617,13 @@ impl Display for ConfigBuilderError {
610617
)?;
611618
Ok(())
612619
}
620+
ConfigBuilderError::ReservedExternalPluginName { plugin_name } => {
621+
write!(
622+
f,
623+
"Plugin name '{plugin_name}' is reserved, and cannot be used for JS plugins",
624+
)?;
625+
Ok(())
626+
}
613627
ConfigBuilderError::ExternalRuleLookupError(e) => std::fmt::Display::fmt(&e, f),
614628
}
615629
}

0 commit comments

Comments
 (0)