Monaco-editor: Strict mode in Javascript

Created on 24 Feb 2018  路  4Comments  路  Source: microsoft/monaco-editor


monaco-editor version: 0.10.1
Browser: All
OS: Windows
I so want the editor to recognise undeclared variables. So I looked through the API docs and thought. Mmmm, simple:

monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
    target: monaco.languages.typescript.ScriptTarget.ES6,
    allowNonTsExtensions: true,
/* but the settings below seem to be ignored */
    alwaysStrict: true,  
    noImplicitUseStrict:true,
    noUnusedLocals:true 
});

How can I set the editor to strict mode? In the past this has been simple using ES6Lint and another editor. A solution to this would be gratefully received. Thanks in advance. David

Most helpful comment

@David-Carty The following code works fine for me in the Monaco Editor Playground.

monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
  noSemanticValidation: false,
  noSyntaxValidation: false
});

monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
  target: monaco.languages.typescript.ScriptTarget.ES6,
  allowNonTsExtensions: true,
  alwaysStrict: true,
  noUnusedParameters: true,
  noImplicitUseStrict: true,
  noUnusedLocals: true
});

var jsCode = [
  '"use strict";',
  'function x(unusedParam) {',
  '\tvar unusedVar = 0;',
  '}'
].join('\n');

monaco.editor.create(document.getElementById("container"), {
  value: jsCode,
  language: "javascript"
});

All 4 comments

@David-Carty The following code works fine for me in the Monaco Editor Playground.

monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
  noSemanticValidation: false,
  noSyntaxValidation: false
});

monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
  target: monaco.languages.typescript.ScriptTarget.ES6,
  allowNonTsExtensions: true,
  alwaysStrict: true,
  noUnusedParameters: true,
  noImplicitUseStrict: true,
  noUnusedLocals: true
});

var jsCode = [
  '"use strict";',
  'function x(unusedParam) {',
  '\tvar unusedVar = 0;',
  '}'
].join('\n');

monaco.editor.create(document.getElementById("container"), {
  value: jsCode,
  language: "javascript"
});

@rcjsuen Thank you so much for your solution. So utterly grateful to this community. Have a fab day!

@David-Carty Happy to help. In the future, please ask your question on Stack Overflow instead of opening an issue on GitHub.

If you have no further questions with your current issue, please close this issue. Thank you!

Thanks again. Will post on Stack moving fwd.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

akosyakov picture akosyakov  路  3Comments

zeegin picture zeegin  路  3Comments

fabiospampinato picture fabiospampinato  路  3Comments

inf9144 picture inf9144  路  3Comments

aarinsmith picture aarinsmith  路  3Comments